java实现2048代码的简单介绍( 二 )


41 public voidpaintComponent(Graphics g1){42 doublex,y;43 super.paintComponent(g1);44 g =(Graphics2D) g1;45 //反锯齿开关开
46 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);47
48 //画表盘
49 g.setPaint(new GradientPaint(5,40,Color.blue,15,50,Color.yellow,true));50 g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));51 g.drawOval(75, 40, 250, 250);52 g.fillOval(195, 160, 10, 10);53 g.setColor(Color.black);54
55 //画60个点
56 for(int i = 0;i60;i++)57 {58 double[] co = new double[2];59 co = paint_Dot(i * 2 * PI / 60);60 x = co[0];61 y = co[1];62 if(i == 0 || i == 15 || i == 30 || i == 45)//画3,6,9,12四个大点
63 {64 g.fillOval((int)(x - 5 + 200),(int)(y - 5 + 165),10,10);65 }66 else//其他小点
67 {68 g.fillOval((int)(x - 2.5 + 200),(int)(y - 2.5 + 165),5,5);69 }70 }71
72 //画四个数字
73 g.setFont(f2);74 g.drawString("3", 300, 171);75 g.drawString("6", 195, 273);76 g.drawString("9", 91, 171);77 g.drawString("12", 190, 68);78
79 //画时针,分针,秒针
80 paint_HourPointer(hour*3600 + min*60 + sec,g);//时针走过的秒数
81 paint_MinutePointer(min*60 + sec,g);//分针走过的秒数
82 paint_SecondPointer(sec,g);//秒针走过的秒数
83 }84
85 public voidshowUI(){86 newThread() {87 @SuppressWarnings("deprecation")88 public voidrun() {89 while (true)90 {91 now = newDate();92 hour =now.getHours();93 min =now.getMinutes();94 sec =now.getSeconds();95 try{96 Thread.sleep(1000);97 } catch(InterruptedException ex) {98 ex.printStackTrace();99 }100 showTime();101 repaint();102 }103 }104 }.start();105 }106
107 public void paint_HourPointer(int second,Graphics2D g){//second表示当前时间的时针相对00:00:00走了多少秒
108 doublex,y,angle;109 angle = second * PI / 21600;//时针的速度为PI/21600 (rad/s)
110 x = 200 + 60 *Math.sin(angle);111 y = 165 - 60 *Math.cos(angle);112 g.setStroke( new BasicStroke(5,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));113 g.setPaint(new GradientPaint(200,165,Color.red,260,165,Color.blue,true));114 g.drawLine(200, 165, (int)x, (int)y);115 }116
117 public void paint_MinutePointer(int second,Graphics2D g){//second表示当前时间的分针相对00:00:00走了多少秒
118 doublex,y,angle;119 angle = second * PI / 1800;//分针的速度为PI/1800 (rad/s)
120 x = 200 + 80 *Math.sin(angle);121 y = 165 - 80 *Math.cos(angle);122 g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));123 g.setPaint(new GradientPaint(200,165,Color.magenta,280,165,Color.blue,true));124 g.drawLine(200, 165, (int)x, (int)y);125 }126
127 public void paint_SecondPointer(int second,Graphics2D g){//second表示当前时间的秒针相对00:00:00走了多少秒
128 doublex,y,x1,y1,x2,y2,x3,y3,angle;129 double cos = 90 / Math.sqrt(8125);//90*90+5*5
130 double sin = 5 / Math.sqrt(8125);131 angle = second * PI / 30;//时针的速度为PI/30 (rad/s)
132 x = 200 + 95 *Math.sin(angle);133 y = 165 - 95 *Math.cos(angle);134 x1 = 200 + 20 * Math.sin(angle +PI);135 y1 = 165 - 20 * Math.cos(angle +PI);136 x2 = 200 + Math.sqrt(8125)* ( Math.sin(angle)*cos - Math.cos(angle)*sin ); //sin(a-b)
137 y2 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cos + Math.sin(angle)*sin ); //cos(a-b)
138 x3 = 200 + Math.sqrt(8125)* ( Math.sin(angle)*cos + Math.cos(angle)*sin ); //sin(a+b)
139 y3 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cos - Math.sin(angle)*sin ); //cos(a+b)
140 g.setStroke( new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));141 g.setPaint(new GradientPaint(180,165,Color.CYAN,295,165,Color.MAGENTA,true));142 g.drawLine((int)x1, (int)y1, (int)x, (int)y);143 g.drawLine((int)x2, (int)y2, (int)x, (int)y);144 g.drawLine((int)x3, (int)y3, (int)x, (int)y);145 }146
147 public double[] paint_Dot(doubleangle){148 double[] co = new double[2];149 co[0] = 115 * Math.cos(angle);//横坐标
150 co[1] = 115 * Math.sin(angle);//纵坐标

推荐阅读