Java AWT GradientPaint

Description

Java AWT GradientPaint

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

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

public class Main extends JPanel {
  @Override/*  w  w w. ja v  a  2  s . c o m*/
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g; // cast g to Graphics2D

    // draw 2D ellipse filled with a blue-yellow gradient
    g2d.setPaint(new GradientPaint(5, 30, Color.BLUE, 35, 100, Color.YELLOW, true));
    g2d.fill(new Ellipse2D.Double(5, 30, 65, 100));
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Drawing 2D shapes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Main Main = new Main();
    frame.add(Main);
    frame.setSize(425, 200);
    frame.setVisible(true);
  }
}



PreviousNext

Related