|
-
February 20th, 2012, 10:00 PM
#5
Re: Buttons on Clock
I am having a similar problem with having the seconds increasing the speed of the clock by 2. What can I do without having to do if else statements within run()? Also I've tried to do something sort of like the Military button.
The part I am talking about is at the bottom of the program. Thanks.
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 Button b2xSpeed;
private boolean buttonMiltClicked = false;
private boolean b2xSpeedClicked = false;
String regTimeFormat = "hh:mm:ss a";
String milTimeFormat = "HH:mm:ss a";
SimpleDateFormat formatter = null;
Calendar cal = Calendar.getInstance();
public void init()
{
buttonMilt = new Button("Military Time");
add(buttonMilt);
buttonMilt.addActionListener(this);
b2xSpeed = new Button("2x Speed");
add(b2xSpeed);
b2xSpeed.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
formatter = new SimpleDateFormat(regTimeFormat);
timeString = formatter.format(cal.getTime());
this.setSize(400, 200);
}
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
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);
b2xSpeed.setLocation(200,50);
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) {
String formatStr = null;
Calendar cal = Calendar.getInstance();
if (e.getSource() == buttonMilt) {
buttonMiltClicked = !buttonMiltClicked;
if(buttonMiltClicked)
{
formatStr = milTimeFormat;
}
else
{
formatStr = regTimeFormat;
}
formatter = new SimpleDateFormat(formatStr);
timeString = formatter.format(cal.getTime());
repaint();
}
if (e.getSource()==b2xSpeed){
b2xSpeedClicked = !b2xSpeedClicked;
if (b2xSpeedClicked)
{
secHand = 2*cal.get(Calendar.SECOND); //*get seconds
}
else
{
secHand = cal.get(Calendar.SECOND); //*get seconds
}
repaint();
}
}
}
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
|