我正在做一个项目,在这个项目中,我必须使用java GUI创建一组6个时钟,显示不同地方的不同时间。我已经成功地创建了使用线程显示一个时钟的代码。但是如何创建6个不同的线程来处理6个时钟呢?这是创建单个时钟的以下类:
public class analogClockGUI extends JPanel implements Runnable{
int secx,secy,lsecx,lsecy;
int minx,miny,lminx,lminy;
int hrx,hry,lhrx,lhry;
int secRadius,minRadius,hrRadius,center;
int pointx1,pointy1,pointx2,pointy2;
double dhs,dhm,dhh;
Random rndm;
Thread t;
Font clockFont;
Date incre;
int s,h,m;
boolean off=false,drawFirst=true;
Dimension dimension;
Color wallColor,backColor=Color.black;
int previousMin,previousSec;
int change;
public analogClockGUI()
{
addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent me)
{
dimension=getSize();
if(change!=dimension.height)
{
center=dimension.height/2;
setSize(new Dimension(dimension.height,dimension.height));
}
/*else
{
center=dimension.width/2;
setSize(new Dimension(dimension.width,dimension.width));
}*/
drawFirst=true;
change=dimension.height;
secRadius=(int)(0.7*center);
minRadius=(int)(0.75*center);
hrRadius=(int)(0.5*center);
}
});
init();
}
public void init()
{
rndm=new Random();
t=new Thread(this);
dimension=getSize();
center=dimension.height/2;
secRadius=(int)(0.7*center);
minRadius=(int)(0.75*center);
hrRadius=(int)(0.5*center);
wallColor=new Color(rndm.nextInt(255),rndm.nextInt(255),rndm.nextInt(255));
t.start();
}
public void run() throws NullPointerException
{
while(true)
{
s=new Date().getSeconds();//+incre.getSeconds();
if(s!=previousSec)
{
dhs=Math.toRadians(s*6-90);
secx=(int)(secRadius*Math.cos(dhs))+center;
secy=(int)(secRadius*Math.sin(dhs))+center;
pointx1=(int)((secRadius+center*0.15)*Math.cos(dhs))+center;
pointy1=(int)((secRadius+center*0.15)*Math.sin(dhs))+center;
m=new Date().getMinutes();
dhm=Math.toRadians(m*6-90);
minx=(int)(minRadius*Math.cos(dhm))+center;
miny=(int)(minRadius*Math.sin(dhm))+center;
pointx2=(int)((secRadius+center*0.15)*Math.cos(dhm))+center;
pointy2=(int)((secRadius+center*0.15)*Math.sin(dhm))+center;
h =new Date().getHours();
dhh=Math.toRadians(h*30-90+((m-1)/2));
hrx=(int)(hrRadius*Math.cos(dhh))+center;
hry=(int)(hrRadius*Math.sin(dhh))+center;
repaint();
previousSec=s;
}
try
{
Thread.sleep(100);
if(off)
break;
}catch(InterruptedException e){}
}
}
public void upgate(Graphics g) throws NullPointerException
{
if(drawFirst==true)
{
clockFont=new Font("Serif",Font.PLAIN,(int)(0.2*center));
drawFirst=false;
}
if(m!=previousMin){
if(s==0)
{
wallColor=new Color(rndm.nextInt(255),rndm.nextInt(255),rndm.nextInt(255));
g.setColor(wallColor);
g.fillOval(0,0,2*center,2*center);
g.setColor(backColor);
g.fillOval((int)(0.1*center),(int)(0.1*center),(int)(center*1.8),(int)(center*1.8));
clear(g);
previousMin=m;
}}
drawPoints(g);
g.setColor(Color.white);
g.setFont(clockFont);
g.drawString("12",(int)(0.9*center),(int)(0.25*center));
g.drawString("6",(int)(0.95*center),(int)(1.885*center));
g.drawString("9",(int)(0.11*center),(int)(1.06*center));
g.drawString("3",(int)(1.805*center),(int)(1.07*center));
g.drawString("1",(int)(1.369*center),(int)(0.36*center));
g.drawString("2",(int)(1.674*center),(int)(0.645*center));
g.drawString("4",(int)(1.665*center),(int)(1.485*center));
g.drawString("5",(int)(1.372*center),(int)(1.778*center));
g.drawString("7",(int)(0.545*center),(int)(1.777*center));
g.drawString("8",(int)(0.227*center),(int)(1.485*center));
g.drawString("10",(int)(0.212*center),(int)(0.675*center));
g.drawString("11",(int)(0.522*center),(int)(0.365*center));
if(lsecx!=0)
{
g.setColor(backColor);
g.drawLine(center,center,lsecx,lsecy);
}
g.setColor(Color.green);
g.drawLine(center,center,hrx,hry);
g.drawLine(center+1,center,hrx,hry);
g.drawLine(center-1,center,hrx,hry);
g.drawLine(center,center+1,hrx,hry);
g.drawLine(center,center-1,hrx,hry);
g.drawLine(center+2,center,hrx,hry);
g.drawLine(center-2,center,hrx,hry);
g.drawLine(center,center+2,hrx,hry);
g.drawLine(center,center-2,hrx,hry);
//show the minute hand with bigger size
g.setColor(Color.blue);
g.drawLine(center,center,minx,miny);
g.drawLine(center+1,center,minx,miny);
g.drawLine(center-1,center,minx,miny);
g.drawLine(center,center+1,minx,miny);
g.drawLine(center,center-1,minx,miny);
g.setColor(Color.red);
g.drawLine(center,center,secx,secy);
g.fillOval(center-3,center-3,6,6); //center point
if(m%5!=0)
{
g.setColor(Color.cyan);
g.fillOval(pointx2-2,pointy2-2,4,4);
}
if(s%5!=0)
{
g.setColor(Color.red);
g.fillOval(pointx1-2,pointy1-2,4,4);
}
lminx=minx;
lminy=miny;
lhrx=hrx;
lhry=hry;
lsecx=secx;
lsecy=secy;
}
public void paint(Graphics g) throws NullPointerException
{
g.setColor(wallColor);
g.fillOval(0,0,2*center,2*center);
g.setColor(backColor);
g.fillOval((int)(0.1*center),(int)(0.1*center),(int)(center*1.8),(int)(center*1.8));
upgate(g);
}
public void clear(Graphics g) throws NullPointerException
{
if(lsecx!=0)
{
g.setColor(backColor);
g.drawLine(center,center,lhrx,lhry);
g.drawLine(center+1,center,lhrx,lhry);
g.drawLine(center-1,center,lhrx,lhry);
g.drawLine(center,center+1,lhrx,lhry);
g.drawLine(center,center-1,lhrx,lhry);
g.drawLine(center+2,center,lhrx,lhry);
g.drawLine(center-2,center,lhrx,lhry);
g.drawLine(center,center+2,lhrx,lhry);
g.drawLine(center,center-2,lhrx,lhry);
g.drawLine(center,center,lminx,lminy);
g.drawLine(center+1,center,lminx,lminy);
g.drawLine(center-1,center,lminx,lminy);
g.drawLine(center,center+1,lminx,lminy);
g.drawLine(center,center-1,lminx,lminy);
}
}
public void drawPoints(Graphics g) throws NullPointerException
{
int x1,y1;
double p;
for(int i=1;i<=60;i++)
{
if(i%5!=0)
{
p=i*0.10472;
x1=(int)((secRadius+center*0.15)*Math.cos(p))+center;
y1=(int)((secRadius+center*0.15)*Math.sin(p))+center;
g.setColor(Color.gray);
g.fillOval(x1-2,y1-2,4,4);
}
}
}
public void stop() throws NullPointerException /*close the thread when applet stopped*/
{
off=true;
t=null;
}
}
您最大的问题是Thread#run
代码。。。时钟的每个实例都使用相同的Date
值。
public void run() throws NullPointerException {
while (true) {
// !! This is wrong !!
// Each clock should have its own concept of it's start time, then each "second" you
// should advance the time by a single second...
s = new Date().getSeconds();//+incre.getSeconds();
您遇到的另一个问题是,不应该在线程的内容中修改UI所依赖的值。。。
// This is a bad idea...
if (s != previousSec) {
dhs = Math.toRadians(s * 6 - 90);
secx = (int) (secRadius * Math.cos(dhs)) + center;
secy = (int) (secRadius * Math.sin(dhs)) + center;
pointx1 = (int) ((secRadius + center * 0.15) * Math.cos(dhs)) + center;
pointy1 = (int) ((secRadius + center * 0.15) * Math.sin(dhs)) + center;
m = new Date().getMinutes();
dhm = Math.toRadians(m * 6 - 90);
minx = (int) (minRadius * Math.cos(dhm)) + center;
miny = (int) (minRadius * Math.sin(dhm)) + center;
pointx2 = (int) ((secRadius + center * 0.15) * Math.cos(dhm)) + center;
pointy2 = (int) ((secRadius + center * 0.15) * Math.sin(dhm)) + center;
h = new Date().getHours();
dhh = Math.toRadians(h * 30 - 90 + ((m - 1) / 2));
hrx = (int) (hrRadius * Math.cos(dhh)) + center;
hry = (int) (hrRadius * Math.sin(dhh)) + center;
repaint();
previousSec = s;
}
问题是,在更新所有值之前,UI可能会被更新,从而导致奇怪且不可预测的绘制技巧。
你真的需要1亿秒的更新吗??我最好将睡眠时间延长到500毫秒,在第二次变化之后稍微提前或稍微晚一点。
你的画也吓坏了我。非常小心,JPanel
已经有一个从Container
继承的名为update(Graphics)
的方法,您可能会出现命名冲突,从而导致程序崩溃(因为update
被重新绘制引擎使用)
很少需要直接覆盖paint
,最好使用paintComponent
。这样可以确保在每个绘制循环中正确更新和准备零部件。作为过程的一部分,paintComponent
还将清除图形上下文,因此不需要clear
方法(只要您尊重绘制链并调用super.paintComponent
想法和解决方案
我要做的是建立一个"ticker"类,它的唯一责任是通知注册的类发生了"tick"。在这门课上,我会使用javax.swing.Timer
。这将强制从事件调度线程的上下文中调用"tick"事件,确保对绘制方法所依赖的变量的更改在同一线程的上下文内更新。我可能会考虑使用大约250毫秒的延迟,以便在通知周期中留出一些额外的时间。
无论是使用Thread
还是Timer
,在更新"ticks"时都会遇到瓶颈。
我也会考虑将时钟的表面绘制为BufferedImage
,因为除非组件的大小发生变化,否则它不太可能发生变化。这会使油漆更快。
由于这些时钟只在固定的时间偏移上有所不同,因此它们可能不需要单独的线程,甚至不需要单个的定时器任务。编写一个更大、更复杂的paintComponent
方法,绘制使用相同基准时间值的具有不同时间偏移的多个时钟。