import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.*; public class Rotates extends Frame { public void lanch() { this.setSize(640, 480); this.setVisible(true); this.addWindowListener(new MyWindowMonitor()); } class MyWindowMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) { setVisible(false); System.exit(0); } } public void paint(Graphics g) { g.translate(320, 240); int[] A={0,0,20}; int[] B={0,100,50}; for(int n=0;n<720;n+=40){ double[][] r={ {Math.cos(n*3.14/180),Math.sin(n*3.14/180),0}, {-Math.sin(n*3.14/180),Math.cos(n*3.14/180),0}, {0, 0, 1} }; int[][] a; a=new int[][]{{0,0,1},{0,100,1},{20,50,1}}; if(n>=360){ a=new int[][]{{0,0,1},{0,100,1},{-20,50,1}}; } int[][] zz; zz=new int[3][3]; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ for(int k=0;k<3;k++){ zz[i][j]+=a[i][k]*r[k][j]; } } } int[] aa=new int[3]; int[] bb=new int[3]; for(int i=0;i<3;i++){ aa[i]=(int)zz[i][0]; bb[i]=(int)zz[i][1]; } if(n>=360){ try { Thread.sleep(1000);//延时函数 } catch (InterruptedException e) { e.printStackTrace(); } g.setColor(Color.red); g.fillPolygon(aa, bb,3); }else{ try { Thread.sleep(1000);//延时函数 } catch (InterruptedException e) { e.printStackTrace(); } g.setColor(Color.BLUE); g.fillPolygon(aa, bb, 3); } } } public static void main(String[] args) { new Rotates().lanch(); } }
运行效果图: