- #1
stripes
- 266
- 0
Homework Statement
Application draws a clock. I need it to start at 12:00 and then go to 12:01, then 12:02, and so on, every minute. Then after 59 minutes, bring back to :00 and move the hour up one, etc. Very simple.
Code:
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
/**
* Clock class.
*
* <P>Clock class is responsible for drawing the clock and its hands based on information
*input by the user.
*The information is made public, and then is passed from ClockTest class to this class.
*The ClockTest class creates the clock object
*
*Information is passed from ClockTest class to Clock class (to the constructor), and the
*constructor allows this information to be used by the paint method, which draws the clock.
*
*
* paint method does not take the information directly. It is directly taken by the consctructor.
*
* @author Robert Haycock
* @version 1.5
*/
public class Clock extends JApplet
{
/**two variables used to draw the clock*/
public int minute;
public int hour;
/**Constructor here for clock object. Input is read from the constructor, and then the paint
method uses the public variables that are assigned with the constructor.*/
public Clock(int min, int hr)
{
/**Information is passed from ClockTest class.*/
minute = min;
hour = hr;
}
/**paint method to draw the clock itself*/
public void paint(Graphics g)
{
/**Specifies the dimensions of the circle around the clock, helps determine where
numbers are placed, and centers the clock.*/
final int CIRCLE_START_WIDTH = 50;
final int CIRCLE_START_HEIGHT = 50;
final int CIRCLE_END_WIDTH = 300;
final int CIRCLE_END_HEIGHT = 300;
/**Constants for the minute intervals are listed*/
final int FIVE_MINUTES = 5;
final int TEN_MINUTES = 10;
final int FIFTEEN_MINUTES = 15;
final int TWENTY_MINUTES = 20;
final int TWENTY_FIVE_MINUTES = 25;
final int THIRTY_MINUTES = 30;
final int THIRTY_FIVE_MINUTES = 35;
final int FOURTY_MINUTES = 40;
final int FOURTY_FIVE_MINUTES = 45;
final int FIFTY_MINUTES = 50;
final int FIFTY_FIVE_MINUTES = 55;
final int SIXTY_MINUTES = 60;
final int CIRCLE_CENTER_X = CIRCLE_END_WIDTH - (2 * CIRCLE_START_WIDTH);
final int CIRCLE_CENTER_Y = CIRCLE_END_HEIGHT - (2 * CIRCLE_START_HEIGHT);
/**Variables will be used to determine the coordinates of the endpoints of the minute and
hour hands. Start points will be the origin, determined above.*/
int xHourHand, yHourHand, xMinuteHand, yMinuteHand;
/**Set position of the ends of the hands, based on trigonometric mathematical formula*/
xMinuteHand = (int)(Math.cos(minute * Math.PI / 30 - Math.PI / 2)*160
+ CIRCLE_CENTER_X);
yMinuteHand = (int)(Math.sin(minute * Math.PI / 30 - Math.PI / 2)*160
+ CIRCLE_CENTER_Y);
xHourHand = (int)(Math.cos((hour * 30 + minute / 2) * Math.PI / 180 - Math.PI / 2)
* 110 + CIRCLE_CENTER_X);
yHourHand = (int)(Math.sin((hour * 30 + minute / 2) * Math.PI / 180 - Math.PI / 2)
* 110 + CIRCLE_CENTER_Y);
/**Draws the circle. Coordinates were increased slightly to center the circle, due to the
nature of drawing the numbers around the clock. These are not magic numbers.*/
g.drawOval(CIRCLE_START_WIDTH + 4, CIRCLE_START_HEIGHT - 4,
CIRCLE_END_WIDTH, CIRCLE_END_HEIGHT);
/**Draws numbers around the clock*/
g.drawString("1", (int)(Math.cos(FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("2", (int)(Math.cos(TEN_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(TEN_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
//y-coordinate increased slightly to center the number 3
g.drawString("3", (int)(Math.cos(FIFTEEN_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(FIFTEEN_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) + 4);
g.drawString("4", (int)(Math.cos(TWENTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(TWENTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("5", (int)(Math.cos(TWENTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(TWENTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
//x-coordinate decreased slightly to center the number 6
g.drawString("6", (int)(Math.cos(THIRTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) - 2,
(int)(Math.sin(THIRTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("7", (int)(Math.cos(THIRTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(THIRTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("8", (int)(Math.cos(FOURTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(FOURTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
//y-coordinate increased slightly to center the number 9
g.drawString("9", (int)(Math.cos(FOURTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(FOURTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) + 4);
g.drawString("10", (int)(Math.cos(FIFTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(FIFTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("11", (int)(Math.cos(FIFTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(FIFTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
//x-coordinate decreased slightly to center the number 12
g.drawString("12", (int)(Math.cos(SIXTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) - 4,
(int)(Math.sin(SIXTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
/**Lines are below. Note that extra numbers were added to the coordinates again to center the lines,
due to the nature of the 3, 6, 9, 12 taking up space*/
/**Line for 03:00*/
g.drawLine((int)(Math.cos(FIFTEEN_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) - 35,
(int)(Math.sin(FIFTEEN_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y),
(int)(Math.cos(FIFTEEN_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(FIFTEEN_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
/**Line for 06:00*/
g.drawLine((int)(Math.cos(THIRTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) ,
(int)(Math.sin(THIRTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) -45,
(int)(Math.cos(THIRTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(THIRTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) - 10);
/**Line for 09:00*/
g.drawLine((int)(Math.cos(FOURTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) + 8,
(int)(Math.sin(FOURTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y),
(int)(Math.cos(FOURTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) + 42,
(int)(Math.sin(FOURTY_FIVE_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
/**Line for 12:00*/
g.drawLine((int)(Math.cos(SIXTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(SIXTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) + 35,
(int)(Math.cos(SIXTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(SIXTY_MINUTES * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
/**Hour and minute hands are now drawn*/
g.drawLine(CIRCLE_CENTER_X, CIRCLE_CENTER_Y, xMinuteHand, yMinuteHand);
g.drawLine(CIRCLE_CENTER_X, CIRCLE_CENTER_Y, xHourHand, yHourHand);
}
}
and
Code:
import java.util.*;
import java.util.Timer;
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.TimerTask;
/**
* ClockTest class.
*
* <P>ClockTest class is responsible for gathering the time from the user, and then creating the
*clock object. All of this information is passed to the Clock class, which is responsible for drawing
*the clock itself. This class also creates the frame in which the clock will be displayed.
*
*
* Recall that the paint method does not take the information directly. It is directly taken by the
*constructor.
*
* @author Robert Haycock
* @version 1.5
*/
public class ClockTest extends JApplet
{
public static void main(String[] args)
{
/**Creates frame object; will display clock object. Made public so other class methods
may access this frame.*/
JFrame frame = new JFrame();
/**Specifies the dimensions of the frame object.*/
final int FRAME_WIDTH = 450;
final int FRAME_HEIGHT = 450;
/**Specifies the highest number the hour and minute numbers (from user input) can be.*/
final int MAX_HOURS = 24;
final int MAX_MINUTES = 59;
/**Specifies the halfway point of the day. Used to convert 24 hour time to 12 hour time.*/
final int MID_DAY = 12;
/**Specifies the length that the inputted information must be.*/
final int INPUT_LENGTH = 5;
/**Specifies the index after the hour number appears in the input string.*/
final int INPUT_HOUR_INDEX = 2;
/**Specifies the index after the colon symbol appears in the input string.*/
final int INPUT_COLON_INDEX = 3;
/**Specifies the start hour.*/
final int startHour = 12;
/**Specifies the start minute.*/
final int startMinute = 0;
/**Specifies the interval by which the clock hands change.*/
final int minuteIncrease = 1;
/**specifies the number of minutes in one hour.*/
final int minuteLength = 60;
/**specifies the number of hours in one day.*/
final int hourLength = 24;
/**specifies the number of miliseconds in one second*/
final int miliSeconds = 1000;
/**Sets the frame size, title, and initiates the close operation*/
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Clock Applet");
//exit on close?
/**Information window is displayed here, telling the user how the program works.*/
JOptionPane.showMessageDialog(frame, "This program will display a "
+ "clock starting at 12:00. It will \nadvance every " + minuteIncrease + "minute(s) indefinitely. "
+ "The user of this program need not \ninput any information.");
Timer timer = new Timer();
timer.schedule(new Clock(startHour, startMinute), 60000);
}
}
Homework Equations
none
The Attempt at a Solution
I keep getting the error:
cannot find symbol
symbol : method schedule(Clock,int)
location: class java.util.Timer
Every 60,000 miliseconds, a new Clock object should be created, why is it not compiling? I've read hundreds of tutorials about actionlistener and event timer stuff and I don't want to do that. I just want two things: increase the minute by one every single minute, and create a new clock object every minute. No actionlistener or weird stuff. Where is the "schedule" method, as many tutorials use it but I cannot compile them because schedule cannot be found.