import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Calendar;
public class ClockPanel extends Canvas{ Calendar pcal;
int x,y,x0,y0,r,h,olds_x,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,ss,mm,hh,old_m,old_h,ang;
final double RAD=Math.PI/180;
public ClockPanel(Calendar cal)
{
this.pcal=cal;
}
public void paint(Graphics g)
{
Graphics2D g2D = (Graphics2D)g;
g.setXORMode(this.getBackground());
h = getSize().height;
g.setColor(Color.BLACK);
r=h/2-30;
x0=this.getWidth()/2;
y0=this.getHeight()/2;
ang=60; //绘制时钟上的12个数字
for(int i=1;i<=12;i++)
{ x=(int)((r-10)*Math.cos(RAD*ang)+x0);
y=(int)((r-10)*Math.sin(RAD*ang)+y0);
g.drawString(""+i,x,h-y);
ang-=30;
} //获得当前系统时间
//int nowh= now.get(Calendar.HOUR_OF_DAY);
int nowh= this.pcal.get(Calendar.HOUR)%12;
int nowm= this.pcal.get(Calendar.MINUTE);
int nows= this.pcal.get(Calendar.SECOND);
ss=90-nows*6;
mm=90-nowm*6;
hh=90-nowh*30-nowm/2;
g2D.setStroke(new BasicStroke(1.0f));
if(olds_x>0)
{
g.setColor(getBackground());
g.drawLine(x0,y0,olds_x,h-olds_y);
}
else
{
old_m = mm;
old_h = hh;
}
//绘制秒针
x=(int) (r*0.9*Math.cos(RAD*ss))+x0;//长度
y=(int)(r*0.9*Math.sin(RAD*ss))+y0;
g.setColor(Color.black);//指针颜色
g.drawLine(x0,y0,x,h-y);//轨迹 olds_x=x;
olds_y=y;
g2D.setStroke(new BasicStroke(2.2f));//分针粗细 //擦除分针
if(old_m!=mm)
{
g.setColor(getBackground());
g.drawLine(x0,y0,oldm_x,h-oldm_y);
} //绘制分针
x=(int)(r*0.7*Math.cos(RAD*mm))+x0;//长度
y=(int)(r*0.7*Math.sin(RAD*mm))+y0;
g.setColor(Color.red);//颜色
g.drawLine(x0,y0,x,h-y);
oldm_x=x;
oldm_y=y;
old_m=mm;
g2D.setStroke(new BasicStroke(3.4f));//时针粗细 //擦除时针
if(old_h!=hh)
{
g.setColor(getBackground());
g.drawLine(x0,y0,oldh_x,h-oldh_y);
} //绘制时针
x=(int)(r*0.5*Math.cos(RAD*hh))+x0;//长度
y=(int)(r*0.5*Math.sin(RAD*hh))+y0;
g.setColor(Color.red);//颜色
g.drawLine(x0,y0,x,h-y);
oldh_x=x;
oldh_y=y;
old_h=hh;
}
public void repaintp(Calendar cal)
{
this.pcal=cal;
this.repaint();
}
}
java时钟_表盘数字
需积分: 16 22 浏览量
2012-03-15
14:48:45
上传
评论
收藏 12KB RAR 举报
DeepLin_wei
- 粉丝: 0
- 资源: 17