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 85 浏览量
2012-03-15
14:48:45
上传
评论
收藏 12KB RAR 举报
DeepLin_wei
- 粉丝: 0
- 资源: 17
最新资源
- 877668389368492摩托车队经理.apk
- IDM下载器官网免安装包,注册CMD文件,突破30天的限制
- qud.win.1.1-installer-10053.9.zip
- adb-r-linux.zip adb-r-linux.zip
- 对光伏出力预测的优化设计,实现了部分功能(注释完全,可直接运行)(文档加Matlab源码)
- 70-android.rules
- 可用于光伏出力预测,引入气象影响因子,效果良好(注释完全,可直接运行)(文档加Matlab源码)
- 利用白噪声特性和ARMR模型,通过求解模型参数生成随机风速(注释完全,可直接运行)(文档加Matlab源码)
- 51-android.rules
- RecationTest.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈