Changing drawing colors. - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Changing drawing colors.

Demo Code

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;

class ColorJPanel extends JPanel 
{
   // draw rectangles and Strings in different colors
   @Override/*from  ww w. j  a v a2 s .  co  m*/
   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);
   } 
}

public class Main
{
   // 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);
   } 
}

Related Tutorials