Java Programming

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

Java Color class:

Java Color Class- There are situations when you need to set the foreground and background colors while designing an application in the Java. The Color class is utilized to encapsulate colors in the default sRGB”Standard Red, Green, and Blue” color space or colors in self-assertive color spaces recognized by a ColorSpace. Each color has a verifiable alpha value of 1.0 or an explicit one gave in the constructor. The alpha value defines the transparency of a color and can be spoken to by a float value in the range 0.0 – 1.0 or 0 – 255. An alpha value of 1.0 or 255 implies that the color is totally opaque and an alpha value of 0 or 0.0 implies that the color is totally transparent. While constructing a Color with an explicit alpha or getting the color/alpha components of a Color, the color components are never premultiplied by the alpha segment.

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

creates a java color class object according to the specified RGB values.

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

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


 The float RGB values ​​in each case ​​can be determined by 255. To simplify the use of Standard colors the constants in the Color class (final class variables) BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE and YELLOW the corresponding commonly used 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 (it is well known, yellow is created by mixing red and green), you can use the prefabricated Color.YELLOW object directly.

Our program example

package testing;

/**

 *

 * @author Fawad khan

 */

import java.awt. *;

import javax.swing. *;




public class Testing extends JFrame {

Container c; // container of this frame

label_class blacklabel; // Label that should appear in the frame

public Testing() {// constructor

 c = getContentPane(); // determine container

 c.setLayout (new FlowLayout ()); // set layout

 // Create the label object with the transfer of the label text

blacklabel = new label_class("black label Testing",  new Color (255,255,255), Color.BLACK);

// Add the label to the frame

 c.add (blacklabel);

}

 public static void main (String [] args) {

 Testing window = new Testing();

 window.setTitle ("Frame with black label");

 window.setSize (300,200);

 window.setVisible (true);

 window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

}

}




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

package testing;

import java.awt.*;

 import javax.swing.*;

 public class label_class extends JLabel {

public label_class(String text,Color fG,Color bG) { // constructor

// Transfer of the label text to the super constructor

super(text);

// Set the background of the label to opaque

setOpaque(true);

// Set the color of the text on the label

 setForeground(fG);

// Set the background color of the label

 setBackground(bG);

 }

}


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 is standard not opaque, i.e. transparent. Therefore we need to call the method setOpaque.

java color class

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button