java绘制五角星_JAVA 五角星小程序
安好如初
回答時間:2019-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 五角星小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Tools 在线更新SD
- 下一篇: 极客星球 | 图像技术在上亿规模实拍图片