Lesson 4 – Change Backgroup Colour

It has been a few days since I posted anything and that is simply because I started working on a new little project/application now that the CalculatorOrangeLite is finished and uploaded.

I was going to work on the Calculator Pro (scientific) but decided to give that a rest for a while and decided to start working on my new idea, more details about this new project will follow soon(ish) 🙂

Anyway for today I will show you how to automatically update the backgroup colour of your SimpleGUI once it’s been launched and to make it even more interesting we will auto-generate a random colour every time you press “Change Backgroup”

Changes for today…

Import Random utility.
import java.util.Random;

 

Update the JButton label.
JButton exitButton = new JButton( "Change Background");

 

Finalise your container so the sub-class can access it and reset the backgroup
final Container c = frame.getContentPane();

 

Create MyColor random generator.
Create a floating point and assign a random colour for “r” RED
Create a floating point and assign a random colour for “g” GREEN
Create a floating point and assign a random colour for “b” BLUE
Create MyRandomColor and assign a random “r,g,b” value
Re-assign the background color of your container using “MyRandomColor”

Random MyColor = new Random();
float r = MyColor.nextFloat();
float g = MyColor.nextFloat();
float b = MyColor.nextFloat();
Color MyRandomColor = new Color(r, g, b);
c.setBackground(MyRandomColor);

 

Now ever time you click on you “Change Background” you will get a new random colour and update your SimpleGUI background.

Full code attached here.
SimpleGUI-Lesson4

Enjoy it.