Nevermind, I found the solution
Hi Guys,
I am an active member over at the slackware forums, but I am doing some Java homework and I have a quick question.
Also please note that I know the proper etiquette, however when it comes to a programming question I may not ask correctly, so If I don't please gently correct me lol.
But I have my homework program working, its pretty simple its a GUI clock that displays hours and minutes of 0,15,30, and 45. It is finished I just need to to basically change the time when I ever I click the anywhere in the program. so if it loaded as 7:30 when I clicked it would change to what ever random time I have programmed in my range.
Thank you for any help in advance. And like I said if I am asking wrong please correct me. Here is my code, I know some of the formatting is off, the professor said it was ok to copy a portion of the code from the ebook, I just haven't formatted it yet to make it look nice. As you can see toward the top is where my mouselistener is, I am assuming that is where I need to put code to redraw and re-randomize the time, I just cant figure out how to do this?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Matt
*/
public class DisplayClock extends JApplet
{
public DisplayClock()
{
// create an analog clock for the current time
StillClock clock = new StillClock();
// Display hour, minute, and second in the message panel
MessagePanel messagePanel = new MessagePanel(clock.getHour() + ":" + clock.getMinute());
messagePanel.setCentered(true);
messagePanel.setForeground(Color.blue);
messagePanel.setFont(new Font("Courier", Font.BOLD, 16));
// Add the clock and message panel to the frame
add(clock);
add(messagePanel, BorderLayout.SOUTH);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
This is where I need to have the clock redraw and have a new time?
}
});
}
public static void main(String[] args)
{
JFrame frame = new JFrame("");
DisplayClock applet = new DisplayClock();
frame.add(applet);
frame.setTitle("Display Clock");
frame.setSize(300,350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class StillClock extends JPanel
{
private int hour;
private int minute;
private int second;
public StillClock()
{
setCurrentTime();
}
public StillClock(int hour, int minute, int seconds)
{
this.hour = hour;
this.minute = minute;
this.second = second;
}
public int getHour()
{
return hour;
}
public void setHour(int hour)
{
this.hour = hour;
repaint();
}
public int getMinute()
{
return minute;
}
public void setMinute(int minute)
{
this.minute = minute;
repaint();
}
public int getSecond()
{
return second;
}
public void setSecond(int second)
{
this.second = second;
repaint();
}
//@Override
protected void paintComponent(Graphics g)
{
super.paintComponents(g);
int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
g.setColor(Color.BLACK);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius);
g.drawString("12", xCenter - 5, yCenter - clockRadius + 12);
g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);
//int sLength = (int)(clockRadius * 0.8);
//int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
//int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
//g.setColor(Color.red);
//g.drawLine(xCenter, yCenter, xSecond, ySecond);
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);
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.blue);
g.drawLine(xCenter, yCenter, xHour, yHour);
}
public void setCurrentTime()
{
//Calendar calendar = new GregorianCalendar();
//this.hour = calendar.get(Calendar.HOUR_OF_DAY);
//this.minute = calendar.get(Calendar.MINUTE);
//this.second = calendar.get(Calendar.SECOND);
int min = (int)(Math.random() * (4 - 1)) + 1;
this.hour = (int)(Math.random() * (12 - 1)) + 1;
if (min == 1)
{
this.minute = 0;
}
else if (min == 2)
{
this.minute = 15;
}
else if (min == 3)
{
this.minute = 30;
}
else if (min == 4)
{
this.minute = 45;
}
}
public Dimension getPreferredSize()
{
return new Dimension(200,200);
}
}
public class MessagePanel extends JPanel {
/** The message to be displayed */
private String message = "Welcome to Java";
/** The x coordinate where the message is displayed */
private int xCoordinate = 20;
/** The y coordinate where the message is displayed */
private int yCoordinate = 20;
/** Indicate whether the message is displayed in the center */
private boolean centered;
/** The interval for moving the message horizontally and vertically */
private int interval = 10;
/** Construct with default properties */
public MessagePanel() {
}
/** Construct a message panel with a specified message */
public MessagePanel(String message) {
this.message = message;
}
/** Return message */
public String getMessage() {
return message;
}
/** Set a new message */
public void setMessage(String message) {
this.message = message;
repaint();
}
/** Return xCoordinator */
public int getXCoordinate() {
return xCoordinate;
}
/** Set a new xCoordinator */
public void setXCoordinate(int x) {
this.xCoordinate = x;
repaint();
}
/** Return yCoordinator */
public int getYCoordinate() {
return yCoordinate;
}
/** Set a new yCoordinator */
public void setYCoordinate(int y) {
this.yCoordinate = y;
repaint();
}
/** Return centered */
public boolean isCentered() {
return centered;
}
/** Set a new centered */
public void setCentered(boolean centered) {
this.centered = centered;
repaint();
}
/** Return interval */
public int getInterval() {
return interval;
}
/** Set a new interval */
public void setInterval(int interval) {
this.interval = interval;
repaint();
}
/** Paint the message */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (centered) {
// Get font metrics for the current font
FontMetrics fm = g.getFontMetrics();
// Find the center location to display
int stringWidth = fm.stringWidth(message);
int stringAscent = fm.getAscent();
// Get the position of the leftmost character in the baseline
xCoordinate = getWidth() / 2 - stringWidth / 2;
yCoordinate = getHeight() / 2 + stringAscent / 2;
}
g.drawString(message, xCoordinate, yCoordinate);
}
/** Move the message left */
public void moveLeft() {
xCoordinate -= interval;
repaint();
}
/** Move the message right */
public void moveRight() {
xCoordinate += interval;
repaint();
}
/** Move the message up */
public void moveUp() {
yCoordinate -= interval;
repaint();
}
/** Move the message down */
public void moveDown() {
yCoordinate += interval;
repaint();
}
/** Override get method for preferredSize */
public Dimension getPreferredSize() {
return new Dimension(200, 30);
}
}
}