But in reality, it isn't very useful to write an applet that always does the same thing. For example, if our applet draws a yellow rectangle on the screen, why bother writing an applet? Why not just create a file with a yellow rectangle and include it in a web page directly?
Useful or interesting programs are ones that provide different results depending on information provided to them. For example, a more interesting rectangle-drawing applet would be one in which the person running the applet (the user) could specify what color rectangle should be drawn. More ambitious examples include things like programs to calculate income taxes (obviously the results depend on information like salary, number of dependents, etc. which mmust be provided by the user) and interactive games, where the game program responds to moves selected by the user.
Information provided to a program is called input. You are probably familiar with many methods of providing input to programs, using text or mouse clicks.
In order for a Java applet to respond to user input, the Java programmer has two tasks to perform:
We have already learned how to write an applet that displays a message on the screen (Lesson 2). We even know how to have the message drawn on the screen in color. But the applet we wrote will always display exactly the same message!
What if today we want the applet to display the message "Hello", but tomorrow we decide that the message should say "Shalom"? We would have to go back to the original Java instructions and change the details of the drawString command.
But what if this applet is already included in a Web page? The person viewing the Web page wouldn't have access to the original Java instructions and wouldn't be able to change them.
Let's add a text field to the applet, so that when the applet runs, the user can enter a message in the text field and that message would then be displayed (in color, if we like) on the screen. This way the same applet can be used to display different messages.
The first step is to add a text field to the applet. In Java, a text field is modelled by a TextField object. (no surprise!) An applet doesn't automatically include TextField objects. (in fact, we haven't seen any until now). If we want to include a TextField object, we must explicitly allocate one. We might say something like:
TextField text = new TextField(10);This allocates a TextField object and gives it a name - text - that we can use to refer to it.
But simply allocating a TextField object doesn't actually put the TextField object on the screen. In order to do that, we must explicitly tell the applet to add this object to the screen layout. This is done by calling a method called add:
add(text);
Until now, we have put all of our instructions inside the paint method. The paint method is invoked anytime the applet window has to be displayed -- either the first time, or if another window was covering the applet window and then the applet window is brought to the front and has to be redrawn.
We don't really want to put the instruction
add(text);in the paint method, because adding the TextField object to the layout is a one-time command. Once this object is part of the layout, it will automatically be redrawn whenever the applet window is painted.
There is another method that is used for initially setting up an applet, called - appropriately - init. The init method is invoked only once, when the applet is first started.
So, we would write the method like this:
public void init(){
add(text);
}
But wait a minute -- what about the line
TextField text = new TextField(10);where does that go? Doesn't that belong inside the init method? Well, the problem is that the object text is used both by the init method and also (as we'll see shortly) by the paint method. Therefore, it should not be allocated within either of those two methods, but rather should be allocated inside the applet, but outside both of those methods.
At this point, we have the following sketchy outline of our applet:
import java.awt.*;
import java.applet.*;
public class Tryinput extends Applet {
TextField text = new TextField(10);
public void init(){
add(text);
}
public void paint (Graphics g) {
.... instructions go here ...
}
}
Now, to continue with our applet we have to provide the instructions that will read the text typed into the TextField by the user and will display that text in color. In order to extract the text from the TextField, we use the getText()method. The result of the getText() method is a string, which we can immediately use as a parameter to the drawString method.
g.setColor(Color.red); g.drawString(text.getText(),50,50);Now, if we put it all together, the applet looks like:
import java.awt.*;
import java.applet.*;
public class Tryinput extends Applet {
TextField text = new TextField(10);
public void init(){
add(text);
}
public void paint (Graphics g) {
g.setColor(Color.red);
g.drawString(text.getText(),50,50);
}
}
Before you read any further, try out the
applet
What happened? It didn't work!
Why?
Well, the problem is that when the paint method was invoked when the applet started, there was nothing there in the TextField, so nothing was displayed. But if paint would be invoked again, then the message would appear. Try it again -- this time, after you enter a message in the text field, minimize the browser window, and then restore it. That will cause the paint method to redraw the screen. Try it right now.
OK, so that works. But that's very clumsy - do you expect that every time you have input you would have to minimize and maximize the window!?
What we really need is some mechanism that would tell the applet that whenever text is typed into that text field, it should automatically redraw the screen.
In order for a Java applet to respond to an event, the object that can trigger events must designate a class that will respond to the events. That class is called a listener, since it "waits" and "listens" for the event to happen. The listener class must also implement a method that will respond to the event that the listener class is listening for.
In our simple example, the applet itself is the listener class. We indicate this in the first line of the applet, by writing
public class Tryinput extends Applet implements ActionListener {
The next step is to add an instruction to the init method. When the initmethod adds the text field object to the applet's layout, it should also designate the applet class as the listener to events triggered by this text field object.
public void init(){
add(text);
text.addActionListener(this);
}
Finally, the applet must include a method called actionPerformed
which is invoked when the event happens. In our case, when the user
types in the text field and triggers the event by pressing "Enter",
we want the screen to be redrawn. We do that by invoking the
repaint method, which causes paint to be re-invoked.
public void actionPerformed(ActionEvent e){
repaint();
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Tryinput2 extends Applet implements ActionListener{
TextField text = new TextField(10);
public void init(){
add(text);
text.addActionListener(this);
}
public void paint (Graphics g) {
g.setColor(Color.red);
g.drawString(text.getText(),50,50);
}
public void actionPerformed(ActionEvent e){
repaint();
}
}
Try it out for yourself