我正在尝试构建具有不同时区的模拟时钟,但出现错误。我想知道我是否可以像现在使用的那样使用我的 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();
}
}