Java Color Class: How to set Foreground and Background in Java using netbeans

Java Color class:

Java Color Class:- In the previous article, we saw that in the abstract class Component methods are provided that allow for Edit the background color of components. These methods work with Objects of the java Color class. Such a java color object places a color through its Proportions of red (R), green (G), and blue (B) fixed. One, therefore, speaks of RGB values or from the RGB color model. The red, green, and blue Shares as three int values ​​in the range 0 to 255 or alternatively as three float values ​​in the range 0.0 to 1.0 specified. Accordingly, the two can Constructors

public Color (int r, int g, int b)

creates a  class object according to the specified RGB values.

public Color (float r, float g, float b)

creates a  class object according to the specified RGB values are used.

 the float RGB values ​​in each case by dividing the int RGB values ​​can be determined by 255. To simplify the use of Standard colors are the constants in the java Color class (final class variables) BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, and YELLOW for the corresponding commonly used Java color class provided. For example, if we use the java color class and we need yellow, we do not need a color object with RGB values ​​255, 255, and generate 0 (as is well known, yellow is created by mixing red and green), you can use the prefabricated Color.YELLOW object directly.



program example: How to set Foreground and Background of a JLabel in Java using Netbeans


works with the colors white (generated with the RGB values ​​255, 255 and 255) and black (using the predefined Color object) to create a Create and display labels with white font on a black background. We use the self-written label_class


which inherits from JLabel and has a constructor that supports in addition to the labeling, the foreground, and background color of the label Gets passed. Note that a label in Swing is standard, not opaque, i.e. transparent. Therefore we need to call the method setOpaque.

java color class

 

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: