- <font color="#333333"><img src="http://bbs.itheima.com/forum.php?mod=image&aid=54232&size=300x300&key=6ced43ee332f1de1&nocache=yes&type=fixnone" aid="attachimg_54232" alt="" border="0">
- //时钟。
- import java.awt.*;
- import java.util.*;
- import javax.swing.*;
- class Coordinate
- {
- public int x;
- public int y;
- public void set(int x,int y)
- {
- this.x=x;
- this.y=y;
- }
- public Coordinate(int x,int y)
- {
- set(x,y);
- }
- }
-
-
- public class ChenWin12 extends JApplet implements Runnable{
- Graphics g;
- FontMetrics font_metrics;
- Thread my_thread;
- static final double pi=Math.PI;
- static final double _2pi=2*pi;
-
- int width;
- int height;
- Date current_time;
- Coordinate center;
- int radius=85;
- int inside_radius;
- int h_len;
- int m_len;
- int s_len;
- public void init()
- {
- g=getGraphics();
- center=new Coordinate(100,100);
- current_time=new Date();
- set_defaults();
- paint(g);
-
- }
-
- void set_defaults()
- {
- width=getSize().width;
- height=getSize().height;
- if(width<height)
- width=height;
- else
- width=height;
- center.set(width/2,height/2);
- radius=(width*8)/20;
- h_len=(radius*3)/10;
- m_len=(radius*4)/10;
- s_len=(radius*6)/10;
- inside_radius=(radius*7)/10;
- Font font=new Font("TimesRoman",Font.BOLD,width/8);
- g.setFont(font);
- font_metrics=g.getFontMetrics();
- }
-
- public void start()
- {
- if(my_thread==null)
- {
- my_thread=new Thread(this);
- my_thread.start();
- }
- }
- public void stop()
- {
- if(my_thread!=null)
- my_thread=null;
- }
- public void run()
- {
- while(my_thread!=null)
- {
- repaint();
- try
- {
- Thread.sleep(1000);
- }
- catch(InterruptedException e){}
- }
- }
-
- public void update(Graphics g)
- {
- g.setColor(Color.yellow);
- g.fillOval(center.x-inside_radius,center.y-inside_radius,
- inside_radius*2,inside_radius*2);
- current_time=new Date();
- GregorianCalendar calendar=new GregorianCalendar();
- calendar.setTime(current_time);
- int hours=calendar.get(Calendar.SECOND);
- int minutes=calendar.get(Calendar.MINUTE);
- int seconds=calendar.get(Calendar.HOUR);
-
- // int hours=current_time.getHours();
- // int minutes=current_time.getMinutes();
- // int seconds=current_time.getSeconds();
- g.setColor(Color.black);
- draw_hands(hours,minutes,seconds);
- }
-
- void draw_hands(int h,int m,int s)
- {
- int x=center.x;
- int y=center.y;
- double hour_value=h+m/60.0+s/3600.0;
- hour_value*=_2pi/12.0;
- g.drawLine(x,y,(int)(x+h_len*Math.sin(hour_value)),
- (int)(y-h_len*Math.cos(hour_value)));
- g.drawLine(x,y-1,(int)(x+h_len*Math.sin(hour_value)),
- (int)(y-h_len*Math.cos(hour_value)));
-
- double minute_value=m+s/60.0;
- minute_value*=_2pi/60.0;
- g.drawLine(x,y,(int)(x+m_len*Math.sin(minute_value)),
- (int)(y-m_len*Math.cos(minute_value)));
- g.drawLine(x,y-1,(int)(x+m_len*Math.sin(minute_value)),
- (int)(y-m_len*Math.cos(minute_value)));
-
- double second_value=s*_2pi/60.0;
- g.drawLine(x,y,(int)(x+s_len*Math.sin(second_value)),
- (int)(y-s_len*Math.cos(second_value)));
- g.drawLine(x,y-1,(int)(x+s_len*Math.sin(second_value)),
- (int)(y-s_len*Math.cos(second_value)));
-
-
- }
- void draw_string(String string,int x,int y)
- {
- int string_width=font_metrics.stringWidth(string);
- int string_height=font_metrics.getAscent();
- g.drawString(string,x-string_width/2,y+string_height/2);
- }
-
- public void paint(Graphics g)
- {
- set_defaults();
- g.setColor(Color.black);
- g.fillRect(0,0,getSize().width,getSize().height);
- g.setColor(Color.lightGray);
- g.fillOval(0,0,width,height);
- g.setColor(Color.black);
- for(int i=1;i<=12;i++)
- draw_string(""+i,(int)(center.x+(radius-8)*Math.sin(_2pi*
- i/12)),
- (int)(center.y-(radius-8)*Math.cos(_2pi*i/12)));
- for(int j=0;j<60;j++)
- draw_string(".",(int)(center.x+(radius+23)*Math.sin(_2pi*
- j/60)),
- (int)(center.y-8-(radius+23)*Math.cos(_2pi*j/60)));
- }
-
- }
- </font><p></p>
复制代码
|
|