Java by API/java.awt/GradientPaint
new GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic)
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp1 = new GradientPaint(5, 5, Color.red, 20, 20, Color.yellow, true);
g2d.setPaint(gp1);
g2d.fillRect(20, 20, 300, 40);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GradientsRedYellow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Main());
frame.setSize(350, 350);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
new GradientPaint(Point2D pt1, Color color1, Point2D pt2, Color color2, boolean cyclic)
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public MainClass() {
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Point2D.Float p1 = new Point2D.Float(150.f, 75.f);
Point2D.Float p2 = 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);
g2D.setPaint(g1);
g2D.fill(rect1);
g2D.setPaint(Color.BLACK);
g2D.draw(rect1);
g2D.draw(new Line2D.Float(p1, p2));
g2D.drawString("Cyclic Gradient Paint", p1.x - 100, p1.y - 50);
g2D.drawString("p1", p1.x - 20, p1.y);
g2D.drawString("p2", p2.x + 10, p2.y);
p1.setLocation(150, 200);
p2.setLocation(250, 200);
GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY,
false);
rect1.setRect(p1.x - 100, p1.y - 25, width, height);
g2D.setPaint(g2);
g2D.fill(rect1);
g2D.setPaint(Color.BLACK);
g2D.draw(rect1);
g2D.draw(new Line2D.Float(p1, p2));
g2D.drawString("Acyclic Gradient Paint", p1.x - 100, p1.y - 50);
g2D.drawString("p1", p1.x - 20, p1.y);
g2D.drawString("p2", p2.x + 10, p2.y);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}