Create shape with GeneralPath in Java
Description
The following code shows how to create shape with GeneralPath.
Example
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
/*from ww w .ja va 2 s .co m*/
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
GeneralPath gp1, gp2, gp3, gp4, gp5, gp6, gp7, gp8;
gp1 = new GeneralPath();
gp1.moveTo(50, 10);
gp1.lineTo(70, 80);
gp1.lineTo(90, 40);
gp1.lineTo(10, 40);
gp1.lineTo(50, 80);
gp1.closePath();
g2D.draw(gp1);
gp2 = new GeneralPath();
gp2.moveTo(120, 20);
gp2.lineTo(180, 20);
gp2.lineTo(120, 80);
gp2.lineTo(180, 80);
gp2.closePath();
g2D.draw(gp2);
gp3 = new GeneralPath();
gp3.moveTo(220, 20);
gp3.lineTo(280, 20);
gp3.lineTo(280, 60);
gp3.lineTo(240, 60);
gp3.lineTo(240, 40);
gp3.lineTo(260, 40);
gp3.lineTo(260, 80);
gp3.lineTo(220, 80);
gp3.closePath();
g2D.draw(gp3);
gp4 = new GeneralPath();
gp4.moveTo(310, 20);
gp4.lineTo(380, 20);
gp4.lineTo(380, 80);
gp4.lineTo(320, 80);
gp4.lineTo(320, 10);
gp4.lineTo(340, 10);
gp4.lineTo(340, 60);
gp4.lineTo(360, 60);
gp4.lineTo(360, 40);
gp4.lineTo(310, 40);
gp4.closePath();
g2D.draw(gp4);
gp5 = new GeneralPath();
gp5.moveTo(50, 120);
gp5.lineTo(70, 180);
gp5.lineTo(20, 140);
gp5.lineTo(80, 140);
gp5.lineTo(30, 180);
gp5.closePath();
g2D.draw(gp5);
gp6 = new GeneralPath();
gp6.moveTo(120, 180);
gp6.quadTo(150, 120, 180, 180);
gp6.closePath();
g2D.draw(gp6);
gp7 = new GeneralPath();
gp7.moveTo(220, 150);
gp7.curveTo(240, 130, 280, 160, 300, 140);
gp7.lineTo(300, 180);
gp7.quadTo(260, 160, 220, 180);
gp7.closePath();
g2D.draw(gp7);
gp8 = new GeneralPath();
gp8.moveTo(360, 100);
gp8.lineTo(360, 200);
gp8.lineTo(400, 140);
gp8.lineTo(320, 120);
gp8.lineTo(400, 180);
gp8.lineTo(320, 180);
gp8.closePath();
g2D.draw(gp8);
}
}
public class Main {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 450, 450);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}
The code above generates the following result.