Drawing with a cyclic and non-cyclic Gradient Color - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Drawing with a cyclic and non-cyclic Gradient Color

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

import javax.swing.JComponent;
import javax.swing.JFrame;


class MyComponent extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    Color startColor = Color.red;
    Color endColor = Color.blue;

    // A non-cyclic gradient
    GradientPaint gradient = new GradientPaint(startX, startY, startColor,
        endX, endY, endColor);
    g2d.setPaint(gradient);
    // Draw shapes...;

    // A cyclic gradient
    gradient = new GradientPaint(startX, startY, startColor, endX, endY,
        endColor, true);
    g2d.setPaint(gradient);
    // Draw shapes...;
  }
}

Related Tutorials