Results 1 to 2 of 2
Thread: Build Clock Challenge Help
- 02-03-2009, 08:49 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Build Clock Challenge Help
I have a challenge my teacher gave to me to create a working clock using java. Here's the class we have to fill in.
Here's the CustomClock class mentioned in the constructor.Java Code:/** * Write a description of class MyClock here. * * @author (your name) * @version (a version number or a date) */ public class MyClock { private CustomClock clockBody; public MyClock() { clockBody = new CustomClock(); //fill in here } public void turnOn() { //Fill in here } public void turnOff() { //Fill in here } }
And finally here's an example of one of the other classes to build the clock parts.Java Code:import java.util.ArrayList; import java.util.Calendar; import javax.swing.Timer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * A clock body whose components (frame, hands, numbers) may be customized. * * @author Nadeem Abdul Hamid * @version 1.0 (18 January 2009) */ public class CustomClock { private static Timer timer; private ClockFrame innerFrame; private ClockFrame outerFrame; private ClockHand hourHand; private ClockHand minuteHand; private ClockHand secondHand; private ClockNumbers numbers; private boolean isVisible; private int xPosition; private int yPosition; /** * Create a new, empty clock body (has no frame, numbers, nor hands). */ public CustomClock() { } /** * Add a hand to the clock. A clock may have at most one of * each type of hand ("hour", "minute", and "second"). */ public void addHand(ClockHand hand) { erase(); if (hand != null) hand.setCenter(xPosition, yPosition); if (hand.getType().equals("hour")) hourHand = hand; else if (hand.getType().equals("minute")) minuteHand = hand; else if (hand.getType().equals("second")) secondHand = hand; draw(); } /** * Add numbers and hour/minute marks to the clock. */ public void addNumbers(ClockNumbers newNumbers) { erase(); if (newNumbers != null) newNumbers.setCenter(xPosition, yPosition); numbers = newNumbers; draw(); } /** * Add a frame around the clock. The isInner parameter specifies whether * it is an inner frame or not. The inner frame is drawn on top of the outer * frame, before all the other components are placed on the clock. */ public void addFrame(ClockFrame newFrame, boolean isInner) { erase(); if (newFrame != null) newFrame.setCenter(xPosition, yPosition); if (isInner) innerFrame = newFrame; else outerFrame = newFrame; draw(); } /** * Sets the coordinates of the center of the clock. */ public void setCenter(int xCenter, int yCenter) { erase(); xPosition = xCenter; yPosition = yCenter; if (hourHand != null) hourHand.setCenter(xPosition, yPosition); if (minuteHand != null) minuteHand.setCenter(xPosition, yPosition); if (secondHand != null) secondHand.setCenter(xPosition, yPosition); if (numbers != null) numbers.setCenter(xPosition, yPosition); if (innerFrame != null) innerFrame.setCenter(xPosition, yPosition); if (outerFrame != null) outerFrame.setCenter(xPosition, yPosition); draw(); } /** * Make the clock visible. If it was already visible, do nothing. */ public void makeVisible() { isVisible = true; draw(); } /** * Display the clock and turn it on. */ public void turnOn() { if (timer != null && timer.isRunning()) return; if (hourHand == null && minuteHand == null && secondHand == null) return; //if (frame == null) // return; setToCurrentTime(); makeVisible(); if (timer == null) timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent ev) { setToCurrentTime(); draw(); } }); timer.start(); } public void turnOff() { if (timer != null) timer.stop(); } /* * Erase the clock on screen. */ private void erase() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); if (numbers != null) numbers.makeInvisible(); if (hourHand != null) hourHand.makeInvisible(); if (minuteHand != null) minuteHand.makeInvisible(); if (secondHand != null) secondHand.makeInvisible(); if (innerFrame != null) innerFrame.makeInvisible(); if (outerFrame != null) outerFrame.makeInvisible(); } } /* * Draw the clock with current components on screen. */ private void draw() { if (isVisible) { if (outerFrame != null) outerFrame.makeVisible(); if (innerFrame != null) innerFrame.makeVisible(); if (numbers != null) numbers.makeVisible(); if (hourHand != null) hourHand.makeVisible(); if (minuteHand != null) minuteHand.makeVisible(); if (secondHand != null) secondHand.makeVisible(); } } /* * Sets the hands of the clock according to the current time. */ public void setToCurrentTime() { Calendar c = Calendar.getInstance(); int h = c.get(Calendar.HOUR_OF_DAY) % 12; if (h == 0) h = 12; int m = c.get(Calendar.MINUTE); int s = c.get(Calendar.SECOND); //System.out.printf("%d:%d:%d\n", h, m, s); if (hourHand != null) hourHand.setValue(h, m); if (minuteHand != null) minuteHand.setValue(m, s); if (secondHand != null) secondHand.setValue(s, 0); } }
I know I have to somehow call the methods of the CustomClock class in MyClock to add the parts to the clock, but I have no clue how to do that. Any help that might get me started on the right path will be extremely helpful.Java Code:import java.awt.BasicStroke; import java.awt.Rectangle; import java.awt.geom.Ellipse2D; /** * A clock frame with various attributes that draws itself around the * body of a clock. * * @author Nadeem Abdul Hamid * @version 1.0 (18 January 2009) */ public class ClockFrame { private int size; private String color; private int xPosition; // center of the clock private int yPosition; private String style; // should be "square" or "circle" private boolean isVisible; /** * Create a new clock frame with default attributes. */ public ClockFrame() { size = 100; xPosition = 100; yPosition = 100; color = "black"; style = "circle"; } /** * Change the type of clock frame. Valid types are "circle" and "square". */ public void setStyle(String newStyle) { erase(); style = newStyle; draw(); } /** * Change the color. Valid colors are "red", "yellow", "blue", "green", * "magenta" and "black". */ public void setColor(String newColor) { color = newColor; draw(); } /** * Make this clock frame visible. If it was already visible, do nothing. */ public void makeVisible() { isVisible = true; draw(); } /** * Make this clock frame invisible. If it was already invisible, do nothing. */ public void makeInvisible() { erase(); isVisible = false; } /** * Sets the coordinates of the center of the clock body around which this * frame is drawn. */ public void setCenter(int xCenter, int yCenter) { erase(); xPosition = xCenter; yPosition = yCenter; draw(); } /** * Change the size of the frame (in pixels). * Size must be > 0. */ public void setSize(int newSize) { erase(); size = newSize; draw(); } /* * Erase the clock frame on screen. */ private void erase() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.erase(this); } } /* * Draw the clock frame with current specifications on screen. */ private void draw() { if (isVisible) { Canvas canvas = Canvas.getCanvas(); int xleft = xPosition - size; int ytop = yPosition - size; if (style.equals("square")) canvas.draw(this, color, new Rectangle(xleft, ytop, size*2, size*2)); else canvas.draw(this, color, new Ellipse2D.Double(xleft, ytop, size*2, size*2)); } } }
-
Similar Threads
-
challenge for swing programmers
By makpandian in forum AWT / SwingReplies: 2Last Post: 12-24-2008, 07:21 PM -
Ultimate challenge in JComboBox
By vaidhu in forum AWT / SwingReplies: 3Last Post: 08-21-2008, 06:56 PM -
Help please in digital clock
By jaidod in forum Java AppletsReplies: 1Last Post: 04-17-2008, 04:05 PM -
java programming challenge Number 1
By tacosalad in forum New To JavaReplies: 5Last Post: 01-16-2008, 10:25 PM -
The Million Musician Challenge 0.821
By levent in forum Java SoftwareReplies: 0Last Post: 05-25-2007, 07:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks