用java设计和编写一个指针式时钟程序,应用线程实现时钟的走动,有的发我邮箱,361390264@qq.com,谢啦!!
2个回答
展开全部
package 时钟;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JApplet;
/**
*
* @author Jiang
*/
public class Clock2 extends JApplet implements Runnable{
double PI=Math.PI;//圆周率
double hourlen=50;//时针长度
double minutelen=70;//分针长度
double secondlen=90;//秒针长度
int yuanxinx=100,yuanxiny=100;//圆心坐标
int banjin=100;//半径长度
int qix,qiy,zhongxh,zhongxm,zhongxs,zhongyh,zhongym,zhongys;//各指针的起点和终点坐标(起点一致)
Thread th;
BufferedImage buf;
int hour,minute,second;//记录当前时间
Calendar calendar;//用于获取当前时间
Graphics g1;//用于内存绘图
public void init(){
hour=minute=second=0;
buf=new BufferedImage(400,400,BufferedImage.TYPE_INT_ARGB);//控制内存绘图区域大小
g1=buf.getGraphics();//创建缓冲绘图区
this.setSize(400,400);
}
public void start(){
if(th==null)
th=new Thread(this);
th.start();//启动线程
}
public void stop(){
th=null;//终止线程
}
public void run(){//线程的覆盖方法
while(true){
try{
th.sleep(1000);
}catch(Exception e){}
//用背景色填充绘图区,擦除上次绘图
g1.setColor(this.getBackground());
//g1.fillRect(yuanxinx-banjin,yuanxiny-banjin,2*banjin,2*banjin);
g1.fillRect(0,0,400,400);
repaint();
}
}
public void paint(Graphics g){
//将图像先画到缓冲区
g1.setColor(this.getForeground());
//绘制时钟刻盘(刻度长度为8)
g1.drawOval(yuanxinx-banjin,yuanxiny-banjin,yuanxinx+banjin,yuanxiny+banjin);
//特殊位置简便画法
//g1.drawLine(yuanxinx-banjin,yuanxiny,yuanxinx-banjin+8,yuanxiny);//9
//g1.drawLine(yuanxinx,yuanxiny-banjin,yuanxinx,yuanxiny-banjin+8);//12
//g1.drawLine(yuanxinx+banjin,yuanxiny,yuanxinx+banjin-8,yuanxiny);//3
//g1.drawLine(yuanxinx,yuanxiny+banjin,yuanxinx,yuanxiny+banjin-8);//6
//1
int qixx=(int)(yuanxinx-banjin*Math.cos((0.5+1.0/6)*PI));
int qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+1.0/6)*PI));
int zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+1.0/6)*PI));
int zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+1.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//2
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+2.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+2.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+2.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+2.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//3
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+3.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+3.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+3.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+3.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//4
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+4.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+4.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+4.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+4.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//5
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+5.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+5.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+5.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+5.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//6
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+6.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+6.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+6.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+6.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//7
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+7.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+7.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+7.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+7.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//8
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+8.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+8.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+8.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+8.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//9
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+9.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+9.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+9.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+9.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//10
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+10.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+10.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+10.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+10.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//11
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+11.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+11.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+11.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+11.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//12
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+12.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+12.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+12.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+12.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//g1.drawLine(150,15,140,25);
//g1.drawString(""+qixx+" "+qiyy+" "+zhongxx+" "+zhongyy,200,300);
//获取当前时间
calendar=Calendar.getInstance();
hour=calendar.get(Calendar.HOUR);
minute=calendar.get(Calendar.MINUTE);
second=calendar.get(Calendar.SECOND);
//计算指针坐标
qix=qiy=yuanxinx;
zhongxh=(int)(yuanxinx-hourlen*Math.cos((0.5+2.0*hour/12)*Math.PI));
zhongyh=(int)(yuanxiny-hourlen*Math.sin((0.5+2.0*hour/12)*Math.PI));
zhongxm=(int)(yuanxinx-minutelen*Math.cos((0.5+2.0*minute/60)*Math.PI));
zhongym=(int)(yuanxiny-minutelen*Math.sin((0.5+2.0*minute/60)*Math.PI));
zhongxs=(int)(yuanxinx-secondlen*Math.cos((0.5+2.0*second/60)*Math.PI));
zhongys=(int)(yuanxiny-secondlen*Math.sin((0.5+2.0*second/60)*Math.PI));
//绘制指针
Font oldfont=this.getFont();
g1.setColor(Color.red);
g1.setFont(new Font(oldfont.getName(),Font.BOLD,oldfont.getStyle()+20));
g1.drawLine(qix,qiy,zhongxh,zhongyh);
g1.setColor(Color.green);
g1.setFont(new Font(oldfont.getName(),Font.BOLD,oldfont.getStyle()+15));
g1.drawLine(qix,qiy,zhongxm,zhongym);
g1.setColor(Color.blue);
g1.setFont(new Font(oldfont.getName(),Font.BOLD,oldfont.getStyle()+10));
g1.drawLine(qix,qiy,zhongxs,zhongys);
//输出时间、日期
g1.setFont(new Font("Time New Roman",Font.BOLD,25));
g1.setColor(Color.PINK);
g1.drawString(""+hour+":"+minute+":"+second,150,240);
g1.setFont(new Font("Time New Roman",Font.BOLD,18));
g1.setColor(Color.green);
g1.drawString("中国标准时间 Chinese Standard Time",25,280);
g1.setFont(new Font("Time New Roman",Font.BOLD,20));
g1.setColor(Color.blue);
g1.drawString(new Date().toString(), 25,320);
//g1.drawString(""+hour+" "+minute+" "+second, 200,200);
g.drawImage(buf, 0,0, this);//将缓冲区图像画到JApplet上
}
public void update(Graphics g){//覆盖方法
paint(g);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JApplet;
/**
*
* @author Jiang
*/
public class Clock2 extends JApplet implements Runnable{
double PI=Math.PI;//圆周率
double hourlen=50;//时针长度
double minutelen=70;//分针长度
double secondlen=90;//秒针长度
int yuanxinx=100,yuanxiny=100;//圆心坐标
int banjin=100;//半径长度
int qix,qiy,zhongxh,zhongxm,zhongxs,zhongyh,zhongym,zhongys;//各指针的起点和终点坐标(起点一致)
Thread th;
BufferedImage buf;
int hour,minute,second;//记录当前时间
Calendar calendar;//用于获取当前时间
Graphics g1;//用于内存绘图
public void init(){
hour=minute=second=0;
buf=new BufferedImage(400,400,BufferedImage.TYPE_INT_ARGB);//控制内存绘图区域大小
g1=buf.getGraphics();//创建缓冲绘图区
this.setSize(400,400);
}
public void start(){
if(th==null)
th=new Thread(this);
th.start();//启动线程
}
public void stop(){
th=null;//终止线程
}
public void run(){//线程的覆盖方法
while(true){
try{
th.sleep(1000);
}catch(Exception e){}
//用背景色填充绘图区,擦除上次绘图
g1.setColor(this.getBackground());
//g1.fillRect(yuanxinx-banjin,yuanxiny-banjin,2*banjin,2*banjin);
g1.fillRect(0,0,400,400);
repaint();
}
}
public void paint(Graphics g){
//将图像先画到缓冲区
g1.setColor(this.getForeground());
//绘制时钟刻盘(刻度长度为8)
g1.drawOval(yuanxinx-banjin,yuanxiny-banjin,yuanxinx+banjin,yuanxiny+banjin);
//特殊位置简便画法
//g1.drawLine(yuanxinx-banjin,yuanxiny,yuanxinx-banjin+8,yuanxiny);//9
//g1.drawLine(yuanxinx,yuanxiny-banjin,yuanxinx,yuanxiny-banjin+8);//12
//g1.drawLine(yuanxinx+banjin,yuanxiny,yuanxinx+banjin-8,yuanxiny);//3
//g1.drawLine(yuanxinx,yuanxiny+banjin,yuanxinx,yuanxiny+banjin-8);//6
//1
int qixx=(int)(yuanxinx-banjin*Math.cos((0.5+1.0/6)*PI));
int qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+1.0/6)*PI));
int zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+1.0/6)*PI));
int zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+1.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//2
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+2.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+2.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+2.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+2.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//3
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+3.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+3.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+3.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+3.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//4
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+4.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+4.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+4.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+4.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//5
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+5.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+5.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+5.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+5.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//6
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+6.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+6.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+6.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+6.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//7
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+7.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+7.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+7.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+7.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//8
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+8.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+8.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+8.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+8.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//9
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+9.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+9.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+9.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+9.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//10
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+10.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+10.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+10.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+10.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//11
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+11.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+11.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+11.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+11.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//12
qixx=(int)(yuanxinx-banjin*Math.cos((0.5+12.0/6)*PI));
qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+12.0/6)*PI));
zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+12.0/6)*PI));
zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+12.0/6)*PI));
g1.drawLine(qixx,qiyy,zhongxx,zhongyy);
//g1.drawLine(150,15,140,25);
//g1.drawString(""+qixx+" "+qiyy+" "+zhongxx+" "+zhongyy,200,300);
//获取当前时间
calendar=Calendar.getInstance();
hour=calendar.get(Calendar.HOUR);
minute=calendar.get(Calendar.MINUTE);
second=calendar.get(Calendar.SECOND);
//计算指针坐标
qix=qiy=yuanxinx;
zhongxh=(int)(yuanxinx-hourlen*Math.cos((0.5+2.0*hour/12)*Math.PI));
zhongyh=(int)(yuanxiny-hourlen*Math.sin((0.5+2.0*hour/12)*Math.PI));
zhongxm=(int)(yuanxinx-minutelen*Math.cos((0.5+2.0*minute/60)*Math.PI));
zhongym=(int)(yuanxiny-minutelen*Math.sin((0.5+2.0*minute/60)*Math.PI));
zhongxs=(int)(yuanxinx-secondlen*Math.cos((0.5+2.0*second/60)*Math.PI));
zhongys=(int)(yuanxiny-secondlen*Math.sin((0.5+2.0*second/60)*Math.PI));
//绘制指针
Font oldfont=this.getFont();
g1.setColor(Color.red);
g1.setFont(new Font(oldfont.getName(),Font.BOLD,oldfont.getStyle()+20));
g1.drawLine(qix,qiy,zhongxh,zhongyh);
g1.setColor(Color.green);
g1.setFont(new Font(oldfont.getName(),Font.BOLD,oldfont.getStyle()+15));
g1.drawLine(qix,qiy,zhongxm,zhongym);
g1.setColor(Color.blue);
g1.setFont(new Font(oldfont.getName(),Font.BOLD,oldfont.getStyle()+10));
g1.drawLine(qix,qiy,zhongxs,zhongys);
//输出时间、日期
g1.setFont(new Font("Time New Roman",Font.BOLD,25));
g1.setColor(Color.PINK);
g1.drawString(""+hour+":"+minute+":"+second,150,240);
g1.setFont(new Font("Time New Roman",Font.BOLD,18));
g1.setColor(Color.green);
g1.drawString("中国标准时间 Chinese Standard Time",25,280);
g1.setFont(new Font("Time New Roman",Font.BOLD,20));
g1.setColor(Color.blue);
g1.drawString(new Date().toString(), 25,320);
//g1.drawString(""+hour+" "+minute+" "+second, 200,200);
g.drawImage(buf, 0,0, this);//将缓冲区图像画到JApplet上
}
public void update(Graphics g){//覆盖方法
paint(g);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询