Color Class in Java
Submitted by donbermoy on Tuesday, October 20, 2015 - 18:24.
Today, I will teach you the basics of having colors in Java. Colors may add life to your GUI Programs.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of colorClass.java.
2. Import javax.swing and java.awt to have GUI components such as frames and buttons.
3. Initialize a panel and a button for your global variable as we will call this to the constructor.
Now, create a constructor named colorClass that will have the color class.
We will try to create a class for the RGB (Red, Green, Blue) color with a variable name color. And put it as the background color of our panel.
Where red, green, and blue are a integer between 0 and 255 to indicate the amount of red, green, and blue respectively.
Now, we will create a background color of our button and we will have its color as static. Static means we will have a direct call of the color. Have and interpret this code below:
The cyan there is the color of the button. And there are 13 constant colors in java namely, black, blue, cyan, dark gray, gray, green, yellow, light gray, magenta, orange, pink, red, and white.
Then put this code in the Main to have the GUI form.
- import javax.swing.*;
- import java.awt.*;
Output:
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Add new comment
- 200 views