Java Applet Background color

In our earlier post we have seen Introduction to applet in Java.In today’s post we shall learn about changing the background of the applet when a particular button in selected.

To make this program we need to learn about buttons and action listener in applets. To add a button to a applet we need to declare a button using button class in the applet package.After declaring we need to add that button the applet window.For this we use add() function.Just adding of the button to the applet window doesn’t make it function,we need to add action listener’s to each button to make it work.It is done by addActionListener() method.

Button color;                  //Declaration of button
color = new Button("Red");    //Initialization of button
add(color);                  //Adding button to the window applet
color.addActionListener(this);//Adding action Listener to the button

All the above work should be done in init() function of the applet class.Now that the button is added to the applet window.Now we need to handle the action of the button when button is clicked. For this we use a function actionPerformed(ActionEvent) to perform respective action on the window applet.

public void actionPerformed(ActionEvent ae)
      //body of the function

In the actionPerformed() function we try to find which button is clicked by the user and the corresponding value of the button.For this we use getActionCommand() function which is present in ActionEvent class.Then the control is passed to the paint method to change the background color.

In paint method we use setBackground() method to set the background which takes color class constants as parameters.Entire program is as follows….

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
<applet code="colordemo" height=300 width=300>
public class colordemo extends Applet implements ActionListener
		Button bcolor1,bcolor2,bcolor3,bcolor4;
		Label bcolor;
		String str;
		public void init()
			bcolor = new Label("Select any of the following button to change the background colour");
			bcolor1 = new Button("Red");    //Declaring buttons
			bcolor2 = new Button("Blue");
			bcolor3 = new Button("Yellow");
			bcolor4 = new Button("Black");
			add(bcolor1);                  //Adding buttons to the applet window

			bcolor1.addActionListener(this); //Adding action listener to the button
		public void actionPerformed(ActionEvent ae)
			str = ae.getActionCommand();
		public void paint(Graphics g)
			else if(str.equals("Blue"))
			else if(str.equals("Yellow"))
			else if(str.equals("Black"))

Explanation:In the above program I defined four buttons for red,blue,yellow,black and changed the background color according to it.



Download the source code


About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s