Java2d supports drawing arbitrary geometries through the GeneralPath implementation.
Step: 1) Instantiate the GeneralPath object
2) Call MoveTo () method anchorage start point coordinates
3) Call the LineTo () or Curveto () method to draw the connector
4) Call the Closepath () method to finish drawing the geometry
Package chapter1;
Import javax.swing.*;
Import java.awt.*;
Import Java.awt.geom.GeneralPath;
public class Generalpathdemo extends JPanel {
Private static final long serialversionuid = 1L;
Public Generalpathdemo () {
Super ();
}
public void Paintcomponent (Graphics g) {
Graphics2D g2d = (graphics2d) g.create ();
G2d.setrenderinghint (renderinghints.key_antialiasing,renderinghints.value_antialias_on);
int x1 = This.getwidth ()/5;
int y1 = This.getheight ()-20;
int x2 = This.getwidth ()/2;
int y2 = 20;
int x3 = this.getwidth ()-20;
int y3 = This.getheight ()-20;
int x4 = 20;
int y4 = This.getheight ()/3;
int x5 = This.getwidth ()-20;
int y5 = Y4;
int x1points[] = {X1,X2,X3,X4,X5};
int y1points[] = {Y1,y2,y3,y4,y5};
G2d.setpaint (color.red);
GeneralPath polygon = new GeneralPath (generalpath.wind_even_odd,x1points.length);
Polygon.moveto (X1points[0],y1points[0]);
Draw the other dots in order
for (int i=1; i<x1points.length; i++) {
Polygon.lineto (X1points[i],y1points[i]);
}
Polygon.closepath ();//Call Closepath to form a closed geometric shape
G2d.draw (polygon);//Draw
G2d.dispose ();//Release resources
}
public static void Main (String args[]) {
JFrame JF = new JFrame ("Demo Graphics");
Jf.setdefaultcloseoperation (Jframe.exit_on_close);
Jf.getcontentpane (). setlayout (New BorderLayout ());
Jf.getcontentpane (). Add (New Generalpathdemo (), borderlayout.center);
Jf.setpreferredsize (New Dimension (380, 380));
Jf.pack ();
Jf.setvisible (TRUE);
}
}
java--Draw Pentagram