Java AWT Color class

Introduction

The AWT color system can specify any color you want.

Three commonly used constructors are shown here:

Color(int red, int green, int blue)  
Color(int rgbValue)  
Color(float red, float green, float blue) 

For example:

new Color(255, 100, 100); // light red 

int newRed = (0xff000000 | (0xc0 << 16) | (0x00 << 8) | 0x00);  
Color darkRed = new Color(newRed); 
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

class DrawPanel extends JPanel {
   public void paintComponent(Graphics g) {
      Color c1 = new Color(255, 100, 100);
      Color c2 = new Color(100, 255, 100);
      Color c3 = new Color(100, 100, 255);

      g.setColor(c1);//from w  w  w .  j a  v a 2 s .c o m
      g.drawLine(0, 0, 100, 100);
      g.drawLine(0, 100, 100, 0);

      g.setColor(c2);
      g.drawLine(40, 25, 250, 180);
      g.drawLine(75, 90, 400, 400);

      g.setColor(c3);
      g.drawLine(20, 150, 400, 40);
      g.drawLine(5, 290, 80, 19);

   }
}

public class Main {
   public static void main(String[] args) {
      DrawPanel panel = new DrawPanel();

      JFrame application = new JFrame();

      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      application.add(panel);
      application.setSize(250, 250);
      application.setVisible(true);
   }
}



PreviousNext

Related