ColorJPanel.java// Fig. 13.5: ColorJPanel.java
// Changing drawing colors.
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class ColorJPanel extends JPanel
{
// draw rectangles and Strings in different colors
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
// set new drawing color using integers
g.setColor(new Color(255, 0, 0));
g.fillRect(15, 25, 100, 20);
g.drawString("Current RGB: " + g.getColor(), 130, 40);
// set new drawing color using floats
g.setColor(new Color(0.50f, 0.75f, 0.0f));
g.fillRect(15, 50, 100, 20);
g.drawString("Current RGB: " + g.getColor(), 130, 65);
// set new drawing color using static Color objects
g.setColor(Color.BLUE);
g.fillRect(15, 75, 100, 20);
g.drawString("Current RGB: " + g.getColor(), 130, 90);
// display individual RGB values
Color color = Color.MAGENTA;
g.setColor(color);
g.fillRect(15, 100, 100, 20);
g.drawString("RGB values: " + color.getRed() + ", " +
color.getGreen() + ", " + color.getBlue(), 130, 115);
}
// execute application
public static void main(String[] args)
{
// create frame for ColorJPanel
JFrame frame = new JFrame("Using colors");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ColorJPanel colorJPanel = new ColorJPanel();
frame.add(colorJPanel);
frame.setSize(400, 180);
frame.setVisible(true);
}
} // end class ColorJPanel
Maintained by John Loomis, updated Wed Feb 08 12:04:48 2017