Hi friend,
I hope something below code will solve your confusion about the GradientPaint class:
Code:
import java.awt.GradientPaint;
public class GradientPainMain extends JPanel {
public GradientPainMain() {
}
public void paint(Graphics g) {
Graphics2D zdv = (Graphics2D) g;
Point2D.Float p1m = new Point2D.Float(150.f, 75.f);
Point2D.Float p2m = new Point2D.Float(250.f, 75.f);
float width = 300;
float height = 50;
GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY,
true);
Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25,
width, height);
zdv.setPaint(g1);
zdv.fill(rect1);
zdv.setPaint(Color.BLACK);
zdv.draw(rect1);
zdv.draw(new Line2D.Float(p1, p2));
zdv.drawString("Cyclic Gradient Paint", p1.x - 100, p1.y - 50);
zdv.drawString("p1", p1.x - 20, p1.y);
p2m.setLocation(250, 200);
}
public static void main(String[] ahrgs) {
JFrame frameB = new JFrame();
frameB.getContentPane().add(new MainClass());
frameB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameB.setSize(300, 200);
frameB.setVisible(true);
}
}
Bookmarks