具有不同时区的模拟时钟

时间:2013-05-11 19:21:04

标签: java user-interface clock

我正在尝试构建具有不同时区的模拟时钟,但我遇到了错误。 我想知道我是否可以像现在这样使用我的iListener。我希望通过更改当前时间的添加或减少来获得不同的时区。可能吗? 这是我的代码:

import javax.swing.*;
import java.awt.*;

import java.util.Date;

public class SimpleAnalogClock extends JPanel
{

int centerPointX;
int centerPointY;

int xs;
    int ys;

Font quartzFont;

Date currentDate;
private JCheckBox Turkey, England;

public SimpleAnalogClock()
{
    setBackground(new Color(0,0,0));

    JFrame frame=new JFrame("Simple Analog Clock");
    Turkey = new JCheckBox("Turkey");
    England = new JCheckBox ("England");
    add(Turkey);
    add(England);
    iListener listener = new iListener();
    Turkey.addItemListener(listener);
    England.addI

    frame.add(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(450,450);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);


    //LOOP THAT WILL UPDATE CLOCK 
    while(true)
    {
        repaint();
        try
        {
            Thread.sleep(900);
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
    }
}

public iListener implements ItemListener
{
    public void itemStateChanged (ItemEvent e)
    {
        if(Turkey.isSelected())
        {
                    /**Information that i mean here is
             *coordinate for second pointer's corner.
             *You can try change value 5 in xsm,ysm,xsa and ysa.
             *After that, try compile and execute this program.
             **/
            int s=currentDate.getSeconds();

            int s_minus_five=s-5;
            int s_add_five=s+5;

            xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 170 + centerPointX);
            ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 170 + centerPointY);

            int xsm=(int) (Math.cos(s_minus_five * Math.PI / 30 - Math.PI / 2) * 5 + centerPointX);
            int ysm=(int) (Math.sin(s_minus_five * Math.PI / 30 - Math.PI / 2) * 5 + centerPointY);

            int xsa=(int) (Math.cos(s_add_five * Math.PI / 30 - Math.PI / 2) * 5 + centerPointX);
            int ysa=(int) (Math.sin(s_add_five * Math.PI / 30 - Math.PI / 2) * 5 + centerPointY);
            //************GET CURRENT SECOND AND ALL INFORMATION TO DRAW SECOND POINTER**************

            //************GET CURRENT MINUTE AND ALL INFORMATION TO DRAW MINUTE POINTER**************
            /**Information that i mean here is
             *coordinate for minute pointer's corner.
             *You can try change value 10 in xmm,ymm,xma and yma.
             *After that, try compile and execute this program.
             **/
            int m=currentDate.getMinutes();

            int m_minus_ten=m-10;
            int m_add_ten=m+10;

            int xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 170 + centerPointX);
            int ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 170 + centerPointY);

            int xmm=(int) (Math.cos(m_minus_ten * Math.PI / 30 - Math.PI / 2) * 10 + centerPointX);
            int ymm=(int) (Math.sin(m_minus_ten * Math.PI / 30 - Math.PI / 2) * 10 + centerPointY);

            int xma=(int) (Math.cos(m_add_ten * Math.PI / 30 - Math.PI / 2) * 10 + centerPointX);
            int yma=(int) (Math.sin(m_add_ten * Math.PI / 30 - Math.PI / 2) * 10 + centerPointY);
            //************GET CURRENT MINUTE AND ALL INFORMATION TO DRAW MINUTE POINTER**************

            //************GET CURRENT HOUR AND ALL INFORMATION TO DRAW HOUR POINTER**************
            /**Information that i mean here is
             *coordinate for hour pointer's corner.
             *You can try change value 10 in xhm,yhm,xha and yha.
             *After that, try compile and execute this program.
             **/
            int h=currentDate.getHours();

            int h_minus_ten=h-10;
            int h_add_ten=h+10;

            int xh = (int) (Math.cos(h * Math.PI / 6 - Math.PI / 2) * 100 + centerPointX);
            int yh = (int) (Math.sin(h * Math.PI / 6 - Math.PI / 2) * 100 + centerPointY);

            int xhm=(int) (Math.cos(h_minus_ten * Math.PI / 6 - Math.PI / 2) * 10 + centerPointX);
            int yhm=(int) (Math.sin(h_minus_ten * Math.PI / 6 - Math.PI / 2) * 10 + centerPointY);

            int xha=(int) (Math.cos(h_add_ten * Math.PI / 6 - Math.PI / 2) * 10 + centerPointX);
            int yha=(int) (Math.sin(h_add_ten * Math.PI / 6 - Math.PI / 2) * 10 + centerPointY);    
        }
            if(England.isSelected())
        {
                    /**Information that i mean here is
             *coordinate for second pointer's corner.
             *You can try change value 5 in xsm,ysm,xsa and ysa.
             *After that, try compile and execute this program.
             **/
            int s=currentDate.getSeconds();

            int s_minus_five=s-5;
            int s_add_five=s+5;

            xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 170 + centerPointX);
            ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 170 + centerPointY);

            int xsm=(int) (Math.cos(s_minus_five * Math.PI / 30 - Math.PI / 2) * 5 + centerPointX);
            int ysm=(int) (Math.sin(s_minus_five * Math.PI / 30 - Math.PI / 2) * 5 + centerPointY);

            int xsa=(int) (Math.cos(s_add_five * Math.PI / 30 - Math.PI / 2) * 5 + centerPointX);
            int ysa=(int) (Math.sin(s_add_five * Math.PI / 30 - Math.PI / 2) * 5 + centerPointY);
            //************GET CURRENT SECOND AND ALL INFORMATION TO DRAW SECOND POINTER**************

            //************GET CURRENT MINUTE AND ALL INFORMATION TO DRAW MINUTE POINTER**************
            /**Information that i mean here is
             *coordinate for minute pointer's corner.
             *You can try change value 10 in xmm,ymm,xma and yma.
             *After that, try compile and execute this program.
             **/
            int m=currentDate.getMinutes();

            int m_minus_ten=m-10;
            int m_add_ten=m+10;

            int xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 170 + centerPointX);
            int ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 170 + centerPointY);

            int xmm=(int) (Math.cos(m_minus_ten * Math.PI / 30 - Math.PI / 2) * 10 + centerPointX);
            int ymm=(int) (Math.sin(m_minus_ten * Math.PI / 30 - Math.PI / 2) * 10 + centerPointY);

            int xma=(int) (Math.cos(m_add_ten * Math.PI / 30 - Math.PI / 2) * 10 + centerPointX);
            int yma=(int) (Math.sin(m_add_ten * Math.PI / 30 - Math.PI / 2) * 10 + centerPointY);
            //************GET CURRENT MINUTE AND ALL INFORMATION TO DRAW MINUTE POINTER**************

            //************GET CURRENT HOUR AND ALL INFORMATION TO DRAW HOUR POINTER**************
            /**Information that i mean here is
             *coordinate for hour pointer's corner.
             *You can try change value 10 in xhm,yhm,xha and yha.
             *After that, try compile and execute this program.
             **/
            int h=currentDate.getHours() - 2;

            int h_minus_ten=h-10;
            int h_add_ten=h+10;

            int xh = (int) (Math.cos(h * Math.PI / 6 - Math.PI / 2) * 100 + centerPointX);
            int yh = (int) (Math.sin(h * Math.PI / 6 - Math.PI / 2) * 100 + centerPointY);

            int xhm=(int) (Math.cos(h_minus_ten * Math.PI / 6 - Math.PI / 2) * 10 + centerPointX);
            int yhm=(int) (Math.sin(h_minus_ten * Math.PI / 6 - Math.PI / 2) * 10 + centerPointY);

            int xha=(int) (Math.cos(h_add_ten * Math.PI / 6 - Math.PI / 2) * 10 + centerPointX);
            int yha=(int) (Math.sin(h_add_ten * Math.PI / 6 - Math.PI / 2) * 10 + centerPointY);    
        }
    }
}

public void paint(Graphics g)
{
    centerPointX=getSize().width/2;
    centerPointY=getSize().height/2;

    currentDate=new Date();

    //************GET CURRENT SECOND AND ALL INFORMATION TO DRAW SECOND POINTER**************

    //************GET CURRENT HOUR AND ALL INFORMATION TO DRAW HOUR POINTER**************

    //OVAL THAT MAKE CLOCK CIRCLE
    int ovalWidth=400;
    int ovalHeight=400;
    //OVAL THAT MAKE CLOCK CIRCLE

    Graphics2D g2d=(Graphics2D)g;

    super.paint(g2d);

    //PEN COLOR TO DRAW : White
    Color penColor=new Color(255,255,255);

    //PEN COLOR TO DRAW : Black
    Color penColor2=new Color(0,0,0);

    //Clock circle background color
    g2d.setColor(penColor);
    g2d.fillOval(((getSize().width)/2)-(ovalWidth/2),((getSize().height)/2)-(ovalHeight/2),ovalWidth,ovalHeight);

    //Draw "Quartz" text
    g2d.setColor(penColor2);
    quartzFont=new Font("Verdana",Font.BOLD,20);
    g2d.setFont(quartzFont);
    g2d.drawString("Quartz",centerPointX-30,centerPointY-100);

    //Draw number 12,3,6 and 9
    quartzFont=new Font("Verdana",Font.BOLD,50);
    g2d.setFont(quartzFont);
    g2d.drawString("12",centerPointX-30,centerPointY-120);
    g2d.drawString("6",centerPointX-18,centerPointY+170);
    g2d.drawString("3",centerPointX+140,centerPointY+13);
    g2d.drawString("9",centerPointX-170,centerPointY+13);

    //*************DRAW SECOND POINTER*********
    int[]coordinateXs={centerPointX,xsm,xs,xsa};
    int[]coordinateYs={centerPointY,ysm,ys,ysa};
    g2d.fillPolygon(coordinateXs,coordinateYs,4);
    //*************DRAW SECOND POINTER*********

    //*************DRAW MINUTE POINTER*********
    int[]coordinateXm={centerPointX,xmm,xm,xma};
    int[]coordinateYm={centerPointY,ymm,ym,yma};
    g2d.fillPolygon(coordinateXm,coordinateYm,4);
    //*************DRAW MINUTE POINTER*********

    //*************DRAW HOUR POINTER*********
    int[]coordinateXh={centerPointX,xhm,xh,xha};
    int[]coordinateYh={centerPointY,yhm,yh,yha};
    g2d.fillPolygon(coordinateXh,coordinateYh,4);
    //*************DRAW HOUR POINTER*********

    //*************DRAW LINE TICK*******************
    //Line tick that i mean here is, short line to indicate second.
    //I hope you understand what i mean.
    for(int i=1; i<=360; i++)
    {
        int tickX;
        int tickY;

        int tickXb;
        int tickYb;

        tickX=(int) (Math.cos(i * Math.PI / 30 - Math.PI / 2)* 180 + centerPointX);
        tickY=(int) (Math.sin(i * Math.PI / 30 - Math.PI / 2)* 180 + centerPointY);

        tickXb=(int) (Math.cos(i * Math.PI / 30 - Math.PI / 2) * 300 + centerPointX);
        tickYb=(int) (Math.sin(i * Math.PI / 30 - Math.PI / 2) * 300 + centerPointY);

        g2d.drawLine(tickXb,tickYb,tickX,tickY);
    }
    //*************DRAW LINE TICK*******************
}

public static void main(String[]args)
{
    SimpleAnalogClock sac=new SimpleAnalogClock();
}

}

2 个答案:

答案 0 :(得分:0)

http://itechnobuzzz.blogspot.in/2013/09/make-analog-clock-using-java.html 从上面链接你有java的模拟时钟的答案..

答案 1 :(得分:0)

HI这是我的模拟时钟代码,带有下拉菜单以选择国家/地区。

这是一个java代码。 如果在运行代码或编译代码时遇到任何问题,请与我联系。

package clock;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.awt.*;

public class Clock extends JPanel implements Runnable,ActionListener {
    public static JMenu jMenu1=new JMenu();
    public static JMenu jMenu2=new JMenu();
    public static JMenu jMenu3=new JMenu();
    public static JMenuBar jMenuBar1=new JMenuBar();
    public static JMenuItem jMenuItem1=new JMenuItem();
    public static JMenuItem jMenuItem2=new JMenuItem();
    public static JMenuItem jMenuItem3=new JMenuItem();
    public static JMenuItem jMenuItem4=new JMenuItem();
    public static JMenuItem jMenuItem5=new JMenuItem();
    public static JMenuItem jMenuItem6=new JMenuItem();
    public static JMenuItem jMenuItem7=new JMenuItem();
    public static JMenuItem jMenuItem8=new JMenuItem();
    public static JMenuItem jMenuItem9=new JMenuItem();
    public static String [] array = {"Europe/Copenhagen","US/Michigan","Asia/Istanbul","Iran","Asia/Singapore","Australia/LHI"};
    public static JComboBox dropdown = new JComboBox(array); 
    public static JLabel label = new JLabel("Label");   

    public static int seconds,minutes,hours;
    public static String timezone =  "Europe/Copenhagen";
    Thread thread = null;
    SimpleDateFormat formatter = new SimpleDateFormat("s", Locale.getDefault());
    Date currentDate;
    int xcenter = 175, ycenter = 175, lastxs = 0, lastys = 0, lastxm = 0, lastym = 0, lastxh = 0,lastyh = 0;

    private void drawStructure(Graphics g)
    {
        Color customColor = new Color(240,240,240);
        g.setColor(customColor);
        g.drawRect(10, 0, 600, 600);
        g.fillRect(0, 0, 600, 600);
        g.setFont(new Font("TimesRoman", Font.BOLD, 20));
        g.setColor(Color.WHITE);
        g.fillOval(xcenter - 170, ycenter - 170, 300, 300);
        g.setColor(Color.BLACK);
        g.drawString("12", xcenter - 30, ycenter - 150);
        g.drawString(".",xcenter + 50,ycenter - 140);
        g.drawString(".",xcenter + 100, ycenter - 90);
        g.drawString("3", xcenter + 115, ycenter - 20);
        g.drawString(".",xcenter + 100, ycenter + 50);
        g.drawString(".", xcenter + 50, ycenter + 100);
        g.drawString("6", xcenter - 30, ycenter + 125);
        g.drawString(".",xcenter - 90, ycenter + 100);
        g.drawString(".",xcenter - 140,ycenter +50);
        g.drawString("9", xcenter - 165, ycenter -20);
        g.drawString(".", xcenter - 140, ycenter -90);
        g.drawString(".",xcenter - 90,ycenter -140);
    }

    public void paint(Graphics g)
    {
        int xhour, yhour, xminute, yminute, xsecond, ysecond, second, minute, hour;
        drawStructure(g);
        currentDate = offsetTimeZone(new Date(), "Asia/Kolkata", timezone);
        //System.out.println("date:"+currentDate);
        formatter.applyPattern("s");
        second = Integer.parseInt(formatter.format(currentDate));
        formatter.applyPattern("m");
        minute = Integer.parseInt(formatter.format(currentDate));
        formatter.applyPattern("h");
        hour = Integer.parseInt(formatter.format(currentDate));
        //System.out.println("second="+second+",minute="+minute+",hour="+hour);
        seconds = second;
        minutes = minute;
        hours = hour;
        this.label.setBackground(Color.red);
        label.setText(hours+" : "+minutes+" : "+seconds);
        this.label.setBackground(Color.red);
        xsecond = (int) (Math.cos(second * 3.14f / 30 - 3.14f / 2) * 120 + xcenter-20);
        ysecond = (int) (Math.sin(second * 3.14f / 30 - 3.14f / 2) * 120 + ycenter-20);
        xminute = (int) (Math.cos(minute * 3.14f / 30 - 3.14f / 2) * 100 + xcenter-20);
        yminute = (int) (Math.sin(minute * 3.14f / 30 - 3.14f / 2) * 100 + ycenter-20);
        xhour = (int) (Math.cos((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 80 + xcenter-20);
        yhour = (int) (Math.sin((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 80 + ycenter-20);
        // Erase if necessary, and redraw
        g.setColor(Color.magenta);
        if (xsecond != lastxs || ysecond != lastys)
            g.drawLine(xcenter-20, ycenter-20, lastxs, lastys);
        if (xminute != lastxm || yminute != lastym)
        {
            g.drawLine(xcenter-20, ycenter - 21, lastxm, lastym);
            g.drawLine(xcenter - 21, ycenter-20, lastxm, lastym);
        }
        if (xhour != lastxh || yhour != lastyh)
        {
            g.drawLine(xcenter-20, ycenter - 21, lastxh, lastyh);
            g.drawLine(xcenter - 21, ycenter-20, lastxh, lastyh);
        }
        g.setColor(Color.black);
        g.drawLine(xcenter-20, ycenter-20, xsecond, ysecond);
        g.setColor(Color.BLACK);
        g.drawLine(xcenter-20, ycenter - 21, xminute, yminute);
        g.drawLine(xcenter - 21, ycenter-20, xminute, yminute);
        g.setColor(Color.BLACK);
        g.drawLine(xcenter-20, ycenter - 21, xhour, yhour);
        g.drawLine(xcenter - 21, ycenter-20, xhour, yhour);
        lastxs = xsecond;
        lastys = ysecond;
        lastxm = xminute;
        lastym = yminute;
        lastxh = xhour;
        lastyh = yhour;
    }

    public void start()
    {
        if (thread == null)
        {
            thread = new Thread(this);
            thread.start();
        }
    }

    public void stop() 
    {
        thread = null;
    }

    public void run()
    {
        while (thread != null)
        {
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e){}
            repaint();
        }
        thread = null;
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    private static Date offsetTimeZone(Date date, String fromTZ, String toTZ)
    {
        // Construct FROM and TO TimeZone instances
        TimeZone fromTimeZone = TimeZone.getTimeZone(fromTZ);
        TimeZone toTimeZone = TimeZone.getTimeZone(toTZ);

        // Get a Calendar instance using the default time zone and locale.
        Calendar calendar = Calendar.getInstance();

        // Set the calendar's time with the given date
        calendar.setTimeZone(fromTimeZone);
        calendar.setTime(date);

        //System.out.println("Input: " + calendar.getTime() + " in " + fromTimeZone.getDisplayName());

        // FROM TimeZone to UTC
        calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);

        if (fromTimeZone.inDaylightTime(calendar.getTime()))
            calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);

        // UTC to TO TimeZone
        calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());

        if (toTimeZone.inDaylightTime(calendar.getTime()))
            calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());

        return calendar.getTime();
    }

    public static void main(String[] args) {
        // TODO code application logic here
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(350, 500);
        window.setLayout(new GridBagLayout());
        Clock clock = new Clock();
         GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 20;      //make this component tall
    //c.weightx = 0.0;
    //c.gridwidth = 100;
        c.ipadx=190;
    c.gridx = 0;
    c.gridy = 0;
    window.setJMenuBar(jMenuBar1);
        window.add(dropdown,c);
        //window.add(Constants.p1);
        //label.setSize(500, 100);
//        label.setBackground(Color.black);
//        Constants.p1.add(dropdown);
//        Constants.p1.add(label);
         c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 20;      //make this component tall
    //c.weightx = 0.0;
    //c.gridwidth = 100;
    c.gridx = 0;
    c.gridy = 1;
        window.add(label,c);
         c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 300;      //make this component tall
        c.ipadx=300;
    //c.weightx = 0.0;
    c.gridwidth = 1000;
    c.gridx = 0;
    c.gridy = 2;
        //window.add(dropdown,c);
        window.add(clock,c);
        //window.add(p2);
        window.setVisible(true);
        clock.start();
    }

   public Clock()
   {
       //Constants.p1.setSize(new Dimension(500, 500));
        jMenu1.setText("View");
        jMenu1.setToolTipText("");
        jMenu1.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_1, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText("Tax Caculator");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_2, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem2.setText("Length Calculator");
        jMenu1.add(jMenuItem2);
        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_3, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem3.setText("Currency Calculator");
        jMenu1.add(jMenuItem3);
        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem4.setText("Time Calculator");
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem5.setText("Cut");
        jMenu2.add(jMenuItem5);
        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setText("Copy");
        jMenu2.add(jMenuItem6);
        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem7.setText("Paste");
        jMenu2.add(jMenuItem7);
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Help");
        jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jMenuItem8.setText("Help");
        jMenu3.add(jMenuItem8);
        jMenuItem9.setText("About Calculator");
        jMenu3.add(jMenuItem9);
        jMenuBar1.add(jMenu3);

        dropdown.addActionListener(this);
   }

   @Override
   public void actionPerformed(ActionEvent e)
   {
       Object instance = dropdown.getSelectedItem();
       timezone = instance.toString();
       //System.out.println(timezone);
       repaint();
   }
}