Need help getting java clock applet to show random time when clicked on...
B Crews
Greenhorn
Joined: Jan 20, 2012
Posts: 1
posted
0
I am trying to get the clock applet to show random time that will change when mouse clicked. The hour should be between 0 and 11, and the minute is 0, 15, 30, or 45. This code is from my text book and I am supposed to modify it. I know that I need to put in a +mouseClicked MouseEvent but not sure where. Also, I think that I need to remove the showCurrent time code but not sure what to replace it with for the random time generation.
@SuppressWarnings("serial")
public class StillClock1 extends JApplet {
private StillClock clock = new StillClock();
// Create a timer with delay 1000 ms
Timer
timer = new Timer(1000, new TimerListener());
public StillClock1() {
add(
clock);
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
private class TimerListener implements ActionListener {
/** Handle the action event */
public void actionPerformed(ActionEvent e) {
// Set new time and repaint the clock to display current time
clock.setCurrentTime();
clock.repaint();
}
}
/** Main method */
public static void main(String[] args) {
JFrame frame =
new JFrame();
JApplet applet =
new StillClock1();
applet.init();
applet.start();
frame.add(applet);
frame.setSize(800, 800);
frame.setLocationRelativeTo(
null); // Center the frame
frame.setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.setVisible(
true);
}
class StillClock extends JPanel {
private int hour;
private int minute;
// }// Display hours on the clock
g.setColor(Color.
red);
for (int i = 0; i < 12; i++) {
int x = (int)(xCenter +
0.8 * clockRadius * Math.sin (i*(2 * Math.
PI / 12)));
int y = (int)(yCenter -
0.8 * clockRadius * Math.cos (i*(2 * Math.
PI / 12)));
g.drawString(
"" + ((i == 0) ? 12 : i), x, y);
}
}
public void setCurrentTime() {
// Construct a calendar for the current date and time
Calendar calendar =
new GregorianCalendar();
// Set current hour, minute and second
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
}
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
public boolean isHourHandVisible() {
return hourHandVisible;
}
public boolean isMinuteHandVisible() {
return hourHandVisible;
}
public void setHourHandVisible(boolean hourHandVisible) {
this.hourHandVisible = hourHandVisible;
repaint();
}
public void setMinuteHandVisible(boolean minuteHandVisible) {
this.minuteHandVisible = minuteHandVisible;
repaint();
}
}
}
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
> I know that I need to put in a +mouseClicked MouseEvent but not sure where.
never use mouseClicked() - doesn't always fire, use mousePressed() or mouseReleased().
you add the listener to the panel that displays the clock
> Also, I think that I need to remove the showCurrent time code
depends, is it supposed to open showing the current time, then show a random time when clicked?
or is this a total rewrite of the code?
> but not sure what to replace it with for the random time generation.
as you're only dealing with hours and minutes, calculate the total number of minutes in the clockface
generate a random number from that total
randNum/60 = hours
randNum % 60 = minutes
minutes/15*15 = 0,15,30 or 45 (which is rounding down to the quarter hour)
if you want rounding to nearest quarter hour
(minutes+7)/15*15 = 0,15,30,45 or 0 again (if it rounds up to the hour, you need to increase hours by 1)
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Need help getting java clock applet to show random time when clicked on...