java--Draw Pentagram

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.