Java Program to Change the Applet Background Colour when Button Clicked

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.

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 :

advertisement
advertisement
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.

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  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.

advertisement
Runtime Test Cases

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

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

advertisement

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.