import java.awt.Color; import javax.swing.JFrame; public class MyFrame extends JFrame { public MyFrame() { setTitle("My Frame"); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setBackground(Color.BLUE); // setting background color to blue setVisible(true); } public static void main(String[] args) { new MyFrame(); } }
import java.awt.Color; public class MyColor { public static void main(String[] args) { Color myColor = new Color(128, 0, 128); // creating a color with RGB values (128, 0, 128) System.out.println("Red value: " + myColor.getRed()); // prints the red value of the color (128) System.out.println("Green value: " + myColor.getGreen()); // prints the green value of the color (0) System.out.println("Blue value: " + myColor.getBlue()); // prints the blue value of the color (128) } }In both examples, the java.awt Color class is used. The first example sets the background color of a JFrame to blue, while the second example creates a custom color with RGB values (128, 0, 128). Both examples are a part of the java.awt package library.