HelloWorld3

Now we will add two buttons to the applet, so we can change the color of the text:
 

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
Here we import the java.awt.event package, this is necesary so that we can invoke an event when the buttons are pressed.
public class HelloWorld3 extends Applet implements ActionListener{

 String text;
 Font font;
 Button button1,button2;
 Color color;

Here we declare two objects of the class Button, we are going to have one for each color. We also declare a Color object which we will use for the String color.   public void init(){
 text="Hello World";
 font=new Font("Helvetica",Font.BOLD,24);

 setBackground(Color.black);

 //Initialise the buttons, add actionListener, and add them to the panel
 button1=new Button("COLOR GREEN");
 button2=new Button("COLOR WHITE");
 button1.addActionListener(this);
 button2.addActionListener(this);
 button1.setActionCommand("green");
 button2.setActionCommand("white");
 add("North",button1);
 add("South",button2);

 //Sets the text color to default white
 color=Color.white;
 }

The buttons is here initialized, the string COLOR GREEN and COLOR WHITE are the labels of the buttons. Then we add an actionListener, this class implements the interface ActionListener, that means that we can add it (the method takes an object of type ActionListener) with the refrence 'this'. Then we set the action command, this is necesary so that we will know what button is pressed, when we come to the actionPerformed method (later).
public void actionPerformed(ActionEvent evt){

    if(evt.getActionCommand().equals("green"))
        color=Color.green;
    else if(evt.getActionCommand().equals("white"))
        color=Color.white;

 repaint();
 }

Here we make the actionPerformed method, this must be done when we implements the ActionListener interface. The action command will read 'green' if button1 is pressed and 'white' if button2 is pressed, this is the reason for setActionCommand in the init method. We change the color according to the button pressed. At last we invoke the repaint method, this will call the paint method, but we don't need to pass on a Graphics object.
   public void paint(Graphics g){

     //Draws a nice frame for the String
     g.setColor(Color.blue);
     g.fillRect(70,45,160,60);
     g.setColor(Color.red);
     g.fillRect(75,50,150,50);
 
     //Sets the font which the applet will use to draw the String
     g.setFont(font);

     //actually paints the string in position 33,34
     g.setColor(color);
     g.drawString(text,83,83);

     }
}

Not much new here, only new coordinates and sizes. Since we add two buttons we must increase the size of the applet, the new size is: width=300 height=150. Also we use the the color object we made to set the color before the drawString method, this is so the string may be drawn in the right color after the actionPerformed method is invoked. Now write the html file or check out the applet  here.

 RETURN TO THE HOMEPAGE