用GeneralPath类就能画了 不用算法,确定好点就行了
import java.awt.geom.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Shapes2 extends JFrame{
public Shapes2(){
super("Drawing 2D Shapes");
setSize(400,400);
setBackground(Color.red);
setVisible(true);
}
public void paint(Graphics g){
int xPoint[]={55,67,109,73,83,55,27,37,1, 43};
int yPoint[]={0, 36,36, 54,96,72,96,54,36,36};
Graphics2D g2d=(Graphics2D) g;
GeneralPath star=new GeneralPath();
star.moveTo(xPoint[0],yPoint[0]);//设置画的起始点
for(int k=1;k<xPoint.length;k++){
star.lineTo(xPoint[k],yPoint[k]);//连接当前点和前一个点
}
star.closePath();//连接最后一个点和第一个点
g2d.translate(200,200);
g2d.setColor(new Color(20,40,130));
g2d.fill(star);
}
public static void main(String[] args){
new Shapes2();
}
}