Creating Custom Colors

You can create custom colors in Java by specifying their Standard Red Green Blue (sRGB) value. sRGB defines a color by the amount of red, green, and blue present in the color. Each value ranges from 0 (none of that color) to 255 (the maximum amount).

The constructor Color(int, int, int) takes arguments representing the red, green, and blue values. The following code draws a panel that displays light orange text (230 red, 220 green, 0 blue) on a dark red (235 red, 50 green, 50 blue) background:

import java.awt.*;import javax.swing.*;public class GoBucs extends JPanel {    Color lightOrange = new Color(230, 220, 0);    Color darkRed = new Color(235, 50, 50);    public void paintComponent(Graphics comp) {        Graphics2D ...

Get Sams Teach Yourself Java™ in 24 Hours, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.