Java Program to Change the Background Colour of Applet by Clicking on Control Button

«
»

This is a Java Program to Change the Background Colour of Applet by Clicking on Control Button

Problem Description

We have to write a program in Java such that it changes the background color of the frame to a random color every time the control key on the keyboard is pressed.

Expected Input and Output

For changing the background color of frame, we have the following different sets of input and output.

advertisement

1. Initial Background Color of Applet :

When the program is executed,
it is expected that the applet initially has background color white.

2. To Change the Background Color of Applet :

Everytime the Control key on the keyboard is pressed,
it is expected that the background color changes.
Problem Solution

1. Initially, set the background color as white.
2. Add KeyListener to the applet.
3. When the control key is pressed, change the background color of applet to a random color.

advertisement
Program/Source Code

Here is source code of the Java Program to change background color of applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Program to Change Background Color of Applet*/
  2. import java.applet.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. public class Change_Background extends Applet implements KeyListener
  7. {
  8.     //Function to initialize the applet
  9.     public void init()
  10.     {
  11. 	setBackground(Color.white);
  12. 	addKeyListener(this);
  13.     }
  14.     //Function to change background color of applet
  15.     public void keyPressed(KeyEvent e)
  16.     {
  17. 	if(e.getKeyCode()==KeyEvent.VK_CONTROL)
  18. 	{
  19.   	    int R = (int)(Math.random()*100) % 255;
  20. 	    int G = (int)(Math.random()*100) % 255;
  21. 	    int B = (int)(Math.random()*100) % 255;
  22. 	    Color mycolor = new Color(R,G,B);
  23. 	    this.setBackground(mycolor);
  24. 	}
  25.     }
  26.     //Empty function
  27.     public void keyReleased(KeyEvent e)
  28.     {
  29.     }
  30.     //Empty function
  31.     public void keyTyped(KeyEvent e)
  32.     {
  33.     }
  34. }
  35. /*
  36. <applet code=Change_Background.class width=400 height=400>
  37. </applet>
  38. */

To compile and run the program use the following commands :

>>>javac Change_Background.java
>>>appletviewer Change_Background.java
Program Explanation

1. Add KeyListener to the applet.
2. When any key is pressed, the function keyPressed is called.
3. The key code of Control key is available as KeyEvent.VK_CONTROL. Compare it with the key code of the key pressed.
4. If the Control key is pressed, generate three random integer whose value are in the range 0 to 255 and use them as the RGB value to create a color.
5. Set the color created by the random values as the background color of the applet.

Runtime Test Cases

Here’s the run time test cases for changing background color of applet.

advertisement

Test case 1 – To View the Initial Color of Applet.
java-applet-color-change-initial

Test case 2 – To View the Color of Applet on Click of Control Button.
java-applet-color-change-1

Test case 3 – To View the Color of Applet on Click of Control Button.
java-applet-color-change-2

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn