• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help getting java clock applet to show random time when clicked on...  RSS feed

 
B Crews
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


import
java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;

import
javax.swing.*;

@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;

private boolean hourHandVisible = true;
private boolean minuteHandVisible = true;


/** Construct a default clock with the current time*/
public StillClock() {
setCurrentTime();

}


/** Construct a clock with specified hour, minute, and second */
public StillClock(int hour, int minute){
this.hour = hour;
this.minute = minute;

}


/** Return hour */
public int getHour() {
return hour;
}


/** Set a new hour */
public void setHour(int hour) {
this.hour = hour;
repaint();

}

/** Return minute */
public int getMinute() {
return minute;
}


/** Set a new minute */
public void setMinute(int minute) {
this.minute = minute;
repaint();

}

/** Draw the clock */
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Initialize clock parameters
int clockRadius =
(
int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;

// Draw circle
g.setColor(Color.
black);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius,

2 * clockRadius, 2 * clockRadius);

// Draw minute hand
if (minuteHandVisible) {
int mLength = (int)(clockRadius * 0.65);
int xMinute = (int)(xCenter + mLength *
Math.sin(
minute * (2 * Math.PI / 60)));
int yMinute = (int)(yCenter - mLength *
Math.cos(
minute * (2 * Math.PI / 60)));
g.setColor(Color.
blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);

}

// Draw hour hand
if (hourHandVisible) {
int hLength = (int)(clockRadius * 0.5);
int xHour = (int)(xCenter + hLength *
Math.sin((
hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int)(yCenter - hLength *
Math.cos((
hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.
green);
g.drawLine(xCenter, yCenter, xHour, yHour);

}

// }// 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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!