CS136, Lecture 6

    1. Mouse & Keyboard Events
    2. Miscellaneous Java constructs:
      1. Parameter Passing
      2. Arrays:
      3. Strings:
    3. Next assignment:

Mouse & Keyboard Events

Events are also generated every time the user moves the mouse or presses a key on the keyboard.

Back up for moment to talk about event handling. All events go to a default method handleEvent(Event evt)

This default method automatically calls the method action if the event concerns a GUI component.

Other events are routed to other methods (makes it easier to override one without touching others).

No matter how handled, the methods return true to indicate that they were successfully handled, and false otherwise.

Some other events and methods which handle them:

Constants are declared in class Event for some special keys: Notice that constants are declared "final static". Final means they cannot be redefined, while static means there is only one copy for the class (i.e., each object of the class doesn't have to carry a copy).

Constants can be accessed by prefixing the class name, e.g., Event.UP.

Warning: Events can only be handled in Java when nothing else in the applet is being executed. In particular, a mouse click on a "stop" button won't be recognized if other routines are being executed. To do this kind of programming, you need to learn how to spawn "threads" to perform actions while applet is waiting for events to handle.

See "Pusher" for sample code using mouse and key events.

--------------------------------------

Miscellaneous Java constructs:

Parameter Passing

All parameters in Java are passed by value. That is, any assignment to a parameter inside a method body is forgotten when the method returns.

Luckily, can send messages to object parameters. If this changes the state (fields) of an object, this will be remembered when return.

Common restriction in pure OO languages (but not C++). Necessary in order to use subtyping in a type-safe way.

Result is use more functions in OO languages or procedures which send messages changing state of parameters (without assigning to them).

Arrays:

Arrays do not have a special class, but can be built from base types and object types.

All arrays begin at 0, and referred to through familiar a[index] notation.

Declare arrays:

	int[] scores;
	Button[] controls;
Java also allows "[]" to be put after variable name, i.e.
	int scores[]
but we'll prefer the first notation because it makes it clearer that the type of scores is an array of integers.

Never include the size of the array in the declaration:

	int[10] scores 			// Illegal in Java!
Instead, arrays are objects and must be instantiated (created) like other objects:
	int[] scores = new int[10];
or, if already declared, just
	scores = new int[10];
Results in scores[0], scores[1],..., scores[9].
Notice difference between size of array and last subscript!

Array declarations specify the type of element held in the array, but not the number of such elements. At different parts of the program, the same array may be different sizes!

Arrays of objects are a little trickier than arrays of base type:

	Button[] controls = new Button[5];
declares an array of 5 buttons, but does not initialize any of the elements of the array.
Normally they will have value null, but shouldn't count on it!

Must now instantiate each value individually:

	for (int buttonNum = 0; buttonNum < 5; buttonNum++)
		controls[buttonNum] = new Button("Button "+buttonNum);
Every array knows its size, thus controls.length will return 5.
-- length is a field, not a method, so needs no "()" after it).

Thus could have used buttonNum < controls.length in if statement.

Java supports array expressions, which can be used to initialize arrays:

	int a[] = {1,2,3,4,13};
initializes a to be an array of 5 integers (with indices 0 to 4).

Like Pascal, Java does not support multi-dimensional arrays.

Like Pascal, Java does support arrays of arrays (though with less syntactic sugar).

	int[][] table;
is equivalent to
	(int[])[] table;
That is, table is an array of arrays of integers.

Unlike Pascal cannot write subscripts separated by ",", must write table[3][5].

	table = new int[10][15];
creates table as 2-dim'l array.

Can also do trickier things like create 2-dim'l array of elts where successive rows have different numbers of elements. See text for examples.

Clarification on "call-by-value": If pass an array as a parameter, can't assign to the array, but can assign to the values and they will be remembered when return!

Strings:

Strings are delimited by "..." as usual, e.g., "Hello there"
may contain any unicode characters - not just ascii.

Strings are objects and are constants (not updatable!).

Writing

	x = "hello";
	y = "hello";
results in x and y pointing to the same string.

If want a fresh copy, can use one of String constructors:

	public String(String oldString);
Now
	z = new String("hello");
will result in z != x, though z.equals(x) is true.

Many built-in string functions (see web page), such as

	public int length();
	public char charAt(int index);  
		// Require 0 <= index <= length, o'wise raise exception
 	public boolean equals(Object anObject);   	
	public boolean equalsIgnoreCase(String anotherString);  
	public int compareTo(String anotherString);  	
		// returns neg, 0 or pos int depending on comparison 
	public int indexOf(int ch)  
		// index of first occurrence of ch or -1 if not there
If want updatable strings, see StringBuffer class.

String is not an array of char, but can convert from one to other.

Next assignment:

RectGrid class:

Create two-dim'l grid of colored rectangles, be able to determine where clicked and change color of rectangles.