使用java GUI的多个时钟

时间:2013-02-03 07:26:14

标签: java multithreading swing user-interface

我正在做一个项目,我必须创建一组6个时钟,使用java GUI显示不同地方的不同时间。我已经成功创建了使用线程显示一个时钟的代码。但是如何创建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;
    }

}

2 个答案:

答案 0 :(得分:1)

您最大的问题在于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,从而导致奇怪且不可预测的油漆图案。

你真的需要一个100毫秒的更新?我最好将睡眠时间缩小到500毫秒,或者在第二次改变后稍微提前或稍晚。

你的绘画也吓坏了我。请非常小心,JPanel已经有一个名为update(Graphics)的方法继承自Container,您可能最终会遇到一个会导致程序崩溃的命名冲突(由update使用{重绘引擎`)

您很少需要直接覆盖paint,您最好使用paintComponent。这可确保在每个喷涂周期中正确更新和准备组件。作为该过程的一部分,paintComponent也将清除图形上下文,因此不需要clear方法(只要您尊重绘制链并调用super.paintComponent

想法和解决方案

我会做的是设立一个“自动收报机”课程,其唯一的责任是通知已注册的班级已发生“勾号”。在本课程中,我将使用javax.swing.Timer。这将强制从事件调度线程的上下文中调用“tick”事件,确保对paint方法所依赖的变量的更改在同一线程的上下文中更新。我可能会考虑使用250毫秒的延迟,在通知周期中允许一些额外的软糖。

无论是使用还是使用ThreadTimer,您都会在更新“滴答声”时最终得到一个瓶颈。

我还会考虑将时钟的面部绘制成BufferedImage,因为除非组件的大小发生变化,否则它不太可能改变。这将使涂料更快。

答案 1 :(得分:1)

由于这些时钟仅因固定时间偏移而不同,因此它们可能不需要单独的线程甚至单独的定时器任务。编写一个更大,更复杂的paintComponent方法来绘制具有不同时间偏移的多个时钟,使用相同的基准时间值。