|
-
February 20th, 2012, 04:57 PM
#1
Buttons on Clock
Whenever the Military Time button is pushed the Military time is displayed, but only for one second. It then reverts back to standard 12 hour digital time. What is going on, and can anyone help me to fix this?
Code:
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.applet.*;
public class AnalogClock extends Applet implements ActionListener, Runnable{
private static final long serialVersionUID = 1787899701025425670L;
TextField text = new TextField(20);
Thread t = null;
boolean threadSuspended;
String timeString = "";
int width, height;
Calendar calendar = Calendar.getInstance();
int hrHand, mnHand, secHand, radius = 85, hrLength, mnLength,
secLength, cx, cy, x1, y1, x2, y2;
private Button buttonMilt;
private boolean buttonMiltClicked = false;
public void init()
{
buttonMilt = new Button("Military Time");
add(buttonMilt);
buttonMilt.addActionListener(this);
width = getSize().width; //get width of applet
height = getSize().height; //get height of applet
hrLength = 6*radius/10; //sets hours
mnLength = 8*radius/10; //sets minutes
secLength = 9*radius/10; //sets seconds
cx = width/2; //initialize position cx
cy = height/2; //initialize position cy
}
public void start() //initialize clock
{
if(t == null)
{
t = new Thread(this);
t.setPriority(Thread.MIN_PRIORITY);
threadSuspended = false;
t.start();
}
else
{
if (threadSuspended)
{
threadSuspended = false;
synchronized(this)
{
notify();
}
}
}
}
public void stop() //stop clock
{
threadSuspended = true;
}
public void run() //run clock
{
try {
while(true)
{
Calendar cal = Calendar.getInstance(); //*gets time from the system
hrHand = cal.get(Calendar.HOUR_OF_DAY); //*gets hour
if (hrHand > 12) hrHand -= 12; //*if hour exceeds 12 decrease
mnHand = cal.get(Calendar.MINUTE); //*get minutes
secHand = cal.get(Calendar.SECOND); //*get seconds
SimpleDateFormat formatter //********
= new SimpleDateFormat("hh:mm:ss a", Locale.getDefault()); //*Gets time and
Date date = cal.getTime(); //*puts it into a digital timer
timeString = formatter.format(date); //********
if (threadSuspended)
{
synchronized(this)
{
while (threadSuspended)
{
wait();
}
}
}
repaint();
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
}
}
public void paint(Graphics g) //paint clock
{
Graphics2D g2d = (Graphics2D) g; //graphics g2d
Graphics2D g2 = (Graphics2D) g; //graphics g2
buttonMilt.setLocation(200,25);
if(hrHand >= 12) //decrease if hour exceeds 12
hrHand -= 12;
for (int i = 0; i < 60; i++) //tickmarks for loop
{
g.setColor(Color.black); //color of tickmarks
x2 = (int)(cx + radius * Math.sin(i * 2 * Math.PI / 60)); //tickmarks endpoint1
y2 = (int)(cy - radius * Math.cos(i * 2 * Math.PI / 60)); //tickmarks endpoint2
if (i % 5 != 0)
{
x1 = (int)(cx + 0.9f * radius * Math.sin(i * 2 * Math.PI / 60)); //length bigticks endpoint1
y1 = (int)(cy - 0.9f * radius * Math.cos(i * 2 * Math.PI / 60)); //length bigticks endpoint2
}
else
{
x1 = (int)(cx + 0.8f * radius * Math.sin(i * 2 * Math.PI / 60)); //length littleticks endpoint1
y1 = (int)(cy - 0.8f * radius * Math.cos(i * 2 * Math.PI / 60)); //length littleticks endpoint2
}
g.drawLine(x1, y1, x2, y2); //tickmarks
}
g2d.setFont(getFont()); //sets font of numbers
for(int hour = 1; hour <= 12; hour++) //Analog numbers for loop
{
double angle = (hour-3)*2*Math.PI/12; //angle of numbers
int x = (int)(Math.cos(angle)*(width/3))+width/2-5; //endpoint1 of numbers
int y = (int)(Math.sin(angle)*(width/3))+width/2+5; //endpoint2 of numbers
g.setColor(Color.black); //color of analog numbers
g2d.drawString(""+hour, x, y); //Analog numbers
}
x2 = (int)(cx + hrLength * Math.sin((hrHand + mnHand / 60 + secHand / 3600)
* 2 * Math.PI / 12)); //endpoint1 of hour hand
y2 = (int)(cy - hrLength * Math.cos((hrHand + mnHand / 60 + secHand / 3600)
* 2 * Math.PI / 12)); //endpoint2 of hour hand
g.setColor(Color.red); //color of hour hand
g2.setStroke(new BasicStroke(2)); //width of hour hand
g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); //hour hand
g2.setStroke(new BasicStroke(0)); //reset width
x2 = (int)(cx + mnLength * Math.sin((mnHand + secHand / 60) * 2 * Math.PI / 60)); //endpoint1 of minute hand
y2 = (int)(cy - mnLength * Math.cos((mnHand + secHand / 60) * 2 * Math.PI / 60)); //endpoint2 of minute hand
g.setColor(Color.green); //color of minute hand
g2.setStroke(new BasicStroke(2)); //width of minute hand
g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); //minute hand
g2.setStroke(new BasicStroke(0)); // reset width of minute hand
x2 = (int)(cx + secLength * Math.sin(secHand * 2 * Math.PI / 60)); //endpoint1 of second hand
y2 = (int)(cy - secLength * Math.cos(secHand * 2 * Math.PI / 60)); //endpoint2 of second hand
g.setColor(Color.blue); //color of circle
g2.setStroke(new BasicStroke(0)); //width of circle
g.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); //second hand
g.setColor(Color.black); //color of circle
g2.setStroke(new BasicStroke(2)); //width of circle
g.drawOval((width - 2 * radius) / 2, (height - 2 * radius) / 2, 2 * radius,
2 * radius); //circle
g2.setStroke(new BasicStroke(0)); //reset width
g.setColor(Color.black); //color of timer
g.drawString(timeString, 75, height-105); //timer
}
public void actionPerformed(ActionEvent e)
{
Calendar cal = Calendar.getInstance(); //*gets time from the system
if(e.getSource()==buttonMilt){
if(buttonMiltClicked == true)
{
SimpleDateFormat formatter //********
= new SimpleDateFormat("HH:mm:ss a", Locale.getDefault()); //*Gets time and
Date date = cal.getTime(); //*puts it into a digital timer
timeString = formatter.format(date); //********
}
buttonMiltClicked = true;
repaint();
}
}
}
Program created in Eclipse.
The action performed is at the bottom of the code, and the time is in the run() not the init().
You will probably need to push the button a couple of time to actually see the digital time go into Military time.
Also the applet window will need to be extended to actually see the button.
Please excuse some of my code and the way it is formatted. Thank you for you time and help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|