安好如初
回答时间:-12-05
向TA提问
public interface IDrawStar {
void setColor(Color c);
void moveStar(int x,int y);
void draw(Graphics g);
}
public class Star implements IDrawStar{
double star_x[]= new double[5];
double star_y[]= new double[5]; //5个点
Color myClor = Color.orange;
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
int move_x =0; //位移量
int move_y =0;
public Star(){
//确定五角星的点
for(int i=0; i<5;i++){
star_x[i] = Math.cos((i*2)*Math.PI/5.0)*100;
star_y[i] = Math.sin((i*2)*Math.PI/5.0)*100;
}
//设定图形五角星
path.moveTo(star_x[2], star_y[2]);
path.lineTo(star_x[0], star_y[0]);
path.lineTo(star_x[3], star_y[3]);
path.lineTo(star_x[1], star_y[1]);
path.lineTo(star_x[4], star_y[4]);
path.closePath();
path.setWindingRule(GeneralPath.WIND_NON_ZERO); //设定填充规则,非零和零,填1或0也行
}
@Override
public void setColor(Color c) {
myClor = c;
}
@Override
public void moveStar(int x, int y) {
move_x =x;
move_y =y;
}
@Override
public void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(myClor);
g2d.translate(move_x, move_y); //平移
g2d.fill(path); //填充
}
}
public class myJF extends JFrame{
Star star ;
public myJF(){
star = new Star();
this.init();
this.repaint();
}
private void init(){
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setVisible(true);
}
@Override
public void paint(Graphics g){
super.paint(g);
star.setColor(Color.GREEN);
star.moveStar(120, 120);
star.draw(g);
star.setColor(Color.orange);
star.moveStar(120, 60);
star.draw(g);
star.setColor(Color.red);
star.moveStar(120, -60);
star.draw(g);
}
public static void main(String args[]){
new myJF();
}
}
netBeans7下运行正常 俺没eclipse的
收起
阅读全文
如果觉得《java绘制五角星_JAVA 五角星小程序》对你有帮助,请点赞、收藏,并留下你的观点哦!