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:
This routine is called when the mouse button is pressed. (x,y) is location of mouse when the button is pressed.
Since Java is usable on systems w/ 2 and 3-button mouses, can pick up click of middle and right mouse buttons by testing for modifier keys (like the way the Mac simulates a 3-button mouse in Exodus).
This routine is called when the mouse button is released. (x,y) is location of mouse when the button is released. Good user-interface programming generally mandates recognizing a mouse click with mouseUp rather than mouseDown.
This routine is called when the mouse is moved and no button is down. (x,y) is location of mouse when the mouse is moved. This event is usually ignored.
This routine is called when the mouse is moved with a button depressed. (x,y) is location of mouse when the button is pressed. This routine is sometimes used in drawing programs to "drag out" graphical objects.
Called when a key is pressed. The parameter key is the unicode value of the key pressed, it can be coerced to a char by writing (char) key. You can check for modifiers (shift key, control key, etc.) when you handle the event.
Called when a key is released. The parameter key is the unicode value of the key pressed. This event is usually ignored.
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.
--------------------------------------
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 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].
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.
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.
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 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 thereIf want updatable strings, see StringBuffer class.
String is not an array of char, but can convert from one to other.
Create two-dim'l grid of colored rectangles, be able to determine where clicked and change color of rectangles.