当前位置: 代码迷 >> Java相关 >> [求助]如何做时钟的指针
  详细解决方案

[求助]如何做时钟的指针

热度:155   发布时间:2006-03-28 13:07:00.0
[求助]如何做时钟的指针

画一个圆后,我想用一条指针就是手表上的秒针一样绕圆走一圈,应该怎么做了
给我一个时钟范例最好
我要代码
谢了,

搜索更多相关的解决方案: 时钟  指针  

----------------解决方案--------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class Clock extends JPanel implements Runnable{
private JLabel jl;
private DateFormat df;
public Clock(){
jl=new JLabel();
jl.setHorizontalAlignment(JLabel.CENTER);
df=DateFormat.getDateTimeInstance();
new Thread(this).start();
this.setLayout(new BorderLayout());
this.add(jl,BorderLayout.SOUTH);
}
public void run(){
while(true){
try{
Thread.sleep(1000);
}
catch(InterruptedException ie){
ie.printStackTrace();
}
jl.setText(df.format(new Date()));
repaint();
}

}
public void paintComponent(Graphics g){
super.paintComponent(g);
Calendar cal=Calendar.getInstance();
//得到当前的时间信息
int hour=cal.get(Calendar.HOUR);
int minute=cal.get(Calendar.MINUTE);
int second=cal.get(Calendar.SECOND);
//得到当前的面板的大小信息
int width=this.getWidth();
int height=this.getHeight();
//钟的那个圆盘取两个之中小的那个
int small=width<height?width:height;
int diameter=(int)(small*0.8);
int radius=diameter/2;
//确定中心点
Point center=new Point(width/2,height/2);
//确定时针,分钟,秒针的长度
int secondLength=(int)(radius*0.8);
int minuteLength=(int)(secondLength*0.8);
int hourLength=(int)(minuteLength*0.8);
//确定时针,分针,秒针的另一端坐标
int secondX=center.x+(int)(secondLength*Math.sin(second*2*Math.PI/60.0));
int secondY=center.y-(int)(secondLength*Math.cos(second*2*Math.PI/60.0));
int minuteX=center.x+(int)(minuteLength*Math.sin(minute*2*Math.PI/60.0));
int minuteY=center.y-(int)(minuteLength*Math.cos(minute*2*Math.PI/60.0));
int hourX=center.x+(int)(hourLength*Math.sin((minute/60.0+hour)*Math.PI/6.0));
int hourY=center.y-(int)(hourLength*Math.cos((minute/60.0+hour)*Math.PI/6.0));
Graphics2D g2d=(Graphics2D)g;
//画表盘和刻度
g.drawOval(center.x-radius,center.y-radius,diameter,diameter);
for(int i=0;i<60;i++){
int x2=center.x+(int)(radius*Math.sin(i*2*Math.PI/60.0));
int y2=center.y-(int)(radius*Math.cos(i*2*Math.PI/60.0));
if(i%5==0){
int x1=center.x+(int)((secondLength+1)*Math.sin(i*2*Math.PI/60.0));
int y1=center.y-(int)((secondLength+1)*Math.cos(i*2*Math.PI/60));
g2d.setStroke(new BasicStroke(2.5f));
g2d.drawLine(x1,y1,x2,y2);
}
else{
int x1=center.x+(int)((secondLength+10)*Math.sin(i*2*Math.PI/60.0));
int y1=center.y-(int)((secondLength+10)*Math.cos(i*2*Math.PI/60));
g2d.setStroke(new BasicStroke(0.8f));
g2d.drawLine(x1,y1,x2,y2);
}
}
//画时针,分针,秒针

g2d.setColor(Color.RED);
g2d.setStroke(new BasicStroke(3.0f));
g2d.drawLine(center.x,center.y,hourX,hourY);
g2d.setColor(Color.BLUE);
g2d.setStroke(new BasicStroke(1.5f));
g2d.drawLine(center.x,center.y,minuteX,minuteY);
g2d.setColor(Color.MAGENTA);
g2d.setStroke(new BasicStroke(1.0f));
g2d.drawLine(center.x,center.y,secondX,secondY);



}
public static void main(String args[]){
JFrame jf=new JFrame("时钟");
jf.getContentPane().add(new Clock(),BorderLayout.CENTER);
jf.setBounds(300,300,300,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

运行一下
----------------解决方案--------------------------------------------------------
老大,太感谢你了

----------------解决方案--------------------------------------------------------

感谢不就用了
了希望你能真正掌握那些代码


----------------解决方案--------------------------------------------------------
  相关解决方案