//package paint;
/*
* GradientDemo.java
*/
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
class GradientDemo extends JFrame {
public GradientDemo() {
super("Gradient Demo");
setSize(1000,350);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new Gpanel());
setVisible(true);
}
public static void main(String arg[]) {new GradientDemo();}
}
class Gpanel extends JPanel {
Gpanel() {
gradient1 = new GradientPaint(30,55,Color.blue,100,100,Color.red,true);
gradient2 = new GradientPaint(30,55,Color.green,100,100,Color.red,true);
gradient3 = new GradientPaint(30,55,Color.green,100,100,Color.magenta,true);
gradient4 = new GradientPaint(30,57,Color.blue,100,70,Color.red,true);
gradient5 = new GradientPaint(30,57,Color.blue,100,70,Color.green,true);
gradient6 = new GradientPaint(30,57,Color.blue,100,70,Color.ORANGE,true);
stroke3 = new BasicStroke(3);
bgd = new Color(240,200,100);
rect1 = new Rectangle(30,50,870,100);
rect2 = new Rectangle(30,180,900,100);
areaRect2 = new Area(rect2);
oval = new Ellipse2D.Float(30,180,800,100);
areaOval = new Area(oval);
areaRect2.subtract(areaOval);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setStroke(stroke3);
g2D.setColor(bgd);
g2D.fill(getBounds());
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//Rectangle:
g2D.setPaint(gradient1);
g2D.fillRect(30,50,300,100);
g2D.setPaint(gradient2);
g2D.fillRect(300,50,300,100);
g2D.setPaint(gradient3);
g2D.fillRect(600,50,300,100);
g2D.setColor(Color.black);
g2D.draw(rect1);
//Oval:
g2D.setPaint(gradient4);
g2D.fillRect(30,180,300,100);
g2D.setPaint(gradient5);
g2D.fillRect(300,180,300,100);
g2D.setPaint(gradient6);
g2D.fillRect(600,180,300,100);
g2D.setColor(bgd);
g2D.fill(areaRect2);
g2D.setColor(Color.black);
g2D.draw(areaOval);
}
private GradientPaint gradient1, gradient2, gradient3, gradient4, gradient5, gradient6;
private Rectangle rect2, rect1;
private Ellipse2D.Float oval;
private Area areaRect2, areaOval;
private BasicStroke stroke3;
private Color bgd;
}