|
6. Responding to a button
So far, once our applet had been started, we could exert very little
influence on it. We are going to change this with the Button class.
Buttons are usually declared in the first part of the applet (the declaration
part). As we saw earlier, a variable declared in this part
is known in every part of the program. Declarations of buttons could look like
this:
Button clicker;
Button switch;
Button button;
The third declaration may look somewhat strange, but is still correct, for Java
distinguishes between upper and lower case letters.
Our button must, however, still be initialized. This is normally done in the
method init().
This initialization also enables us to put a text on the button.
clicker = new Button
("click here");
switch = new Button ("click");
button = new Button ("please click");
Iinitialization and declaration can be
combined, for instance like this:
Button clicker = new Button ("click here");
Button switch = new Button ("click");
Button button = new Button ("please click");
The last thing we have to do is to add the button a
place to the applet. This happens with the statement
add, which we normally use in the init method, as follows:
add (clicker);
add (switch);
add (button);
Now we should be able to make an applet with a
button:
// A button that doesn't do anything
import java.applet.*;
import java.awt.*;
public class EmptyButton extends Applet {
Button button = new Button ("click");
public void init () {
add(button);
}
}
This button responds when we click on it, but it doesn't really do
anything. One way of checking whether a button has been clicked is by using a method called action.
Arguments
The variables that we find in parentheses after the
name of a method are called "the arguments" of the method. The purpose
of these arguments is to import values into the method. The standard methods of
the applet have standard arguments: we know that paint has an object of the Graphics
class as standard argument. And the standard for init is no argument. The method
action has two standard objects, the first of the class Event
and the second of the class
Object. The first line (the "header") of the method action
looks like this:
public boolean action (Event e, Object o) {
Return value
If an argument is meant to import a value into a method, you can imagine that
there must also be a mechanism to export values out of methods. Well such a
mechanism does exist. A value can be exported from a method by means of the return
statement. The type of the value that is returned must be the same as the type
of the method.
Some methods (like paint and init)
do not export any types they are of the void
type. The method action is, however, of the boolean type, which means that it has to return the
value true or false, for instance like this:
return true;
Normally, the method action returns true when the button is
clicked and
false otherwise. Please have a good look at the following applet and see
if you can figure out how it works:
// A button which changes the background colour
// of the applet window when a button is clicked
import java.applet.*;
import java.awt.*;
public class ChangeColor extends Applet {
Button switch = new Button ("click");
boolean light = true;
public void init () {
add (switch);
}
public void paint (Graphics g) {
if (light)
setBackground (Color.lightGray);
else
setBackground (Color.darkGray);
}
public boolean action (Event e, Object o) {
if (e.target == switch) {
light = !light;
repaint();
return true;
}
return false;
}
}
The variable light is
declared 'globally', which
is in the declaration part of the applet.
After all, it is used in two methods (in paint and in
action), so in both
methods its value must be known.
boolean light = true;
The method action checks if
the button has been clicked, with:
if (e.target == switch) {
When the button is clicked, a number of
statements must be executed. In order to to have all these statements
executed, they must be surrounded by curly braces to make them into a
block.
The value of light is inverted after every
click with:
light = !light;
We saw the same sort of expression in our
shooting-target program in chapter 4. It means that the value of light
is inverted.
When we want to have the applet window painted (again), we issue the
command repaint() - which (again) calls the method
paint().
A 'pulsating' circle
Let's now combine a few of the things we learned by making a nice-looking
applet.
We are going to make a program that responds to button clicks by making a circle
alternately bigger and smaller.
// Pulsating circle as demonstration of
// the co-operation between the method
// action, a button and the paint method.
import java.applet.*;
import java.awt.*;
public class PulsatingCircle extends Applet {
Button button = new Button ("click here");
int increment = 10, diameter = 5, maxSize;
public void init() {
add (button);
setBackground(Color.yellow);
}
public void paint (Graphics g) {
Dimension d = getSize();
int height = d.height, width = d.width;
if (width < height)
maxSize = width;
else
maxSize = height;
g.setColor (Color.red);
int x = (width - diameter) / 2;
int y = (height - diameter) / 2;
g.fillOval (x, y, diameter, diameter);
}
public boolean action (Event e, Object o) {
if (e.target==button) {
diameter = diameter + increment;
if (diameter<10 || diameter>=maxSize)
increment = -increment;
repaint();
return true;
}
return false;
}
}
Whether the window is "lying" or
"standing" (in other words: whether height
is greater than width, or the other way round) is detected
by these lines:
Dimension d = getSize();
int height = d.height, width = d.width;
if (width < height)
maxSize = width;
else
maxSize= height;
When the program is executed you'll see the circle
get bigger and smaller alternately. The change between increase and decrease is
brought about by these lines:
if (diameter<10 || diameter>=maxSize)
increase = - increase;
The operator || is pronounced or
and the whole statement means that the value of increase is inverted if the diameter is smaller than 10
or greater than (or equal to) maxSize.
About = and ==
Finally, something about the operators = and ==,
which we have seen several times before. First the =
operator:
a = 7;
This expression means: a becomes 7, or: a gets the value 7. This operator is
called the assignment operator.
The operator == is used in comparisons (for
instance with
if or while) and
means that the variables at either side of
== have the same value, for instance like this:
if (a==b) . . .
while (x==y) . . .
Exercise 6.1
Write an applet in which a text tells us how often the button has been
clicked, e.g. like this:
The button has been changed 0 x.
After clicking, the text changes to:
The
button has been changed 1 x.
Of course, this number is increased after every click.
Exercise 6.2
Write an applet that starts with a dot in the left of the window. Every
time the button is clicked, the dot will move a little bit to the
right, and when it has reached the right side, it will go back, etc.
Chapter 7
Main Menu Java Tutorial
To homepage
(c) Thomas J.H. Luif
| |