Drawing with Color - Java 2D Graphics

Java examples for 2D Graphics:Color

Introduction

Use a predefined color

g2d.setColor(Color.red);

Use a custom color

int red = 230;
int green = 45;
int blue = 67;
g2d.setColor(new Color(red, green, blue));

Demo Code

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Main {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    // Use a predefined color
    g2d.setColor(Color.red);/*from w  ww  .  j  a v  a2s .  com*/
    // Draw shapes...;

    // Use a custom color
    int red = 230;
    int green = 45;
    int blue = 67;
    g2d.setColor(new Color(red, green, blue));
    // Draw shapes...;
  }
}

Related Tutorials