闹钟安卓java源代码 java闹钟小程序( 五 )


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);//纵坐标
151 returnco;152 }153
154 @SuppressWarnings("deprecation")155 private voidshowTime(){156 String date;157 int hour_temp = hour,min_temp = min,sec_temp =sec;158 sec_temp += 1;159 if(sec_temp = 60)160 {161 sec_temp = 0;162 min_temp += 1;163 }164 if(min_temp=60){165 min_temp=0;166 hour_temp+=1;167 }168 if(hour_temp10)169 strTime = "0" + hour_temp + ":";170 else
171 strTime = "" + hour_temp + ":";172
173 if(min_temp10)174 strTime = strTime + "0" + min_temp + ":";175 else
176 strTime = strTime + "" + min_temp + ":";177
178 if(sec10)179 strTime = strTime + "0" +sec_temp;180 else
181 strTime = strTime + "" +sec_temp;182 //在窗体上设置显示时间
183 date = " " + (now.getYear()+1900) + "年" + (now.getMonth()+1) + "月" + now.getDate() + "日 " + "星期";184 switch(now.getDay()) {185 case 1:186 date += "一";187 break;188 case 2:189 date += "二";190 break;191 case 3:192 date += "三";193 break;194 case 4:195 date += "四";196 break;197 case 5:198 date += "五";199 break;200 case 6:201 date += "六";202 break;203 case 7:204 date += "日";205 break;206 }207 date += " CST";208 strTime = " " +strTime;209 display.setText(strTime);210 display2.setText(date);211 }212

推荐阅读