In addition to specifying colors throughout a program, the super.paintComponent(g); will also allow you to set foreground and background colors in the JPanel.
To set the background color of the JPanel: setBackground(Color.WHITE);
To set the foreground color of the JPanel: setForeground(Color.BLUE);
Getting More Colors:
Since Java works with the RGB color system, you can fine tune your colors by controlling the RGB
(red/green/blue) density values. Each of these 3 colors has 256
shades. It is possible to "mix" a new shade of color by selecting an
integer from 0 to 255 and passing it to a Color constructor. The
value 0 indicates the absence of a color in the mixture, and the value 255
indicates the maximum saturation of that color. The color black has
RGB(0,0,0) and the color white has RGB(255,255,255).
There are 256 * 256 *256 = 224 possible
colors in this scheme. WOW!
To create your
own color:
Access the PAINT program (or a similar graphic based program) on your
computer to help you decide upon your new color.
START
Programs
Accessories
Paint - Double click on any
color at the bottom of the screen.
- Choose "Define Custom Colors".
- Select a color and/or use the arrows to achieve the
desired color.
- Copy down the RED, GREEN, BLUE numbers indicated.
These
are the numbers needed to create your new Java color.
Create your new color by using the following code.
Replace the R, G and B with the numbers pertaining to your new color.
Color myNewBlue = new Color (R, G, B); //creates your new color
//create a new color by picking R, G, and B values between 0 and 255)
g.setColor(myNewBlue); //accesses your
new color (NO dot operator)
Creating Random Colors:
Create a random color with RGB values: |