Alarm Clock using Java 2D

Hey Guys,
I have built an analog clock in java using swing and gregorian calender. Now my professor wants me to add an alarm functionality to it. Any ideas of how to implement this into the already existing code. The folllowing is the code i have written so far..everything works except i dont know how to implement the alarm functionality. Assistance needed ASAP, as its due by this wednesday!!
/** Analog Clock using Java Graphics 2D Component */
import java.awt.*;
import java.awt.Graphics2D;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
/** Built on JPanel..Used class Timer for repeatedly executing
the clocktimer thread/
public class AnalogClock extends JPanel
ImageIcon img;
private GregorianCalendar m_calendar;
private int[] x=new int[2];
private int[] y=new int[2];
private java.util.Timer clocktimer=new java.util.Timer();
/**You could set the TimeZone for the clock here. I used the Dfault time
zone from the user so that every time the program runs on different
computers the correct time is displayed*/
private TimeZone clockTimeZone=TimeZone.getDefault();
//Constructor
public AnalogClock()
this.setPreferredSize(new Dimension(210,210));
this.setMinimumSize(new Dimension(210,210));
//schedules the clocktimer task to scan for every 1000ms=1sec
clocktimer.schedule(new TickTimerTask(),0,1000);
//The Clock Face instance method
public void paint(Graphics g)
g.setColor(Color.orange);
g.fillRect(0,0,this.getWidth(),this.getHeight());
drawCardinals((Graphics2D)g);
drawHands((Graphics2D)g);
//Endpoints of the Clock Hand
void clockMinutes(int startRadius,int endRadius,double theta)
theta-=Math.PI/2;
x[0]=(int)(getWidth()/2+startRadius*Math.cos(theta));
y[0]=(int)(getHeight()/2+startRadius*Math.sin(theta));
x[1]=(int)(getWidth()/2+endRadius*Math.cos(theta));
y[1]=(int)(getHeight()/2+endRadius*Math.sin(theta));
//The Hours/Cardinals of the clock
/** Set Stroke sets the thickness of the cardinals and hands*/
void drawCardinals(Graphics2D g)
g.setStroke(new BasicStroke(9));
g.setColor(Color.black);
for(double theta=0;theta<Math.PI*2;theta+=Math.PI/6)
clockMinutes(100,100,theta);
/**Draws a sequence of connected lines defined by arrays of x and
*y coordinates. Each pair of (x, y) coordinates defines a point.
*The figure is not closed if the first point
differs from the last point./
g.drawPolyline(x,y,2);
//The Hand of the Clocks instance method
public void drawHands(Graphics2D g)
double h=2*Math.PI*(m_calendar.get(Calendar.HOUR));
double m=2*Math.PI*(m_calendar.get(Calendar.MINUTE));
double s=2*Math.PI*(m_calendar.get(Calendar.SECOND));
g.setStroke(new BasicStroke(9));
clockMinutes(0,55,h/12+m/(60*12));
g.setColor(Color.red);
g.drawPolyline(x,y,2);
clockMinutes(0,70,m/60+s/(60*60));
g.setColor(Color.blue);
g.drawPolyline(x,y,2);
clockMinutes(0,70,s/60);
g.setColor(Color.black);
g.drawPolyline(x,y,2);
g.fillOval(getWidth()/2-8,getHeight()/2-8,16,16);
//method to update/refresh the clock every second
class TickTimerTask extends TimerTask
public void run()
m_calendar=(GregorianCalendar)GregorianCalendar.getInstance(clockTimeZone);
repaint();
The Main File is below:
import javax.swing.JFrame;
import java.awt.Toolkit;
public class AnalogClockView extends JFrame
public static void main(String[] args)
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setTitle("My Java Project Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Clock.gif"));
AnalogClock m_AnalogClock=new AnalogClock();
frame.add(m_AnalogClock);
frame.setVisible(true);
Edited by: sIrIUs_bLAcK on Aug 31, 2008 11:17 PM

some items unrelated to your main question:
*) When drawing in JPanels and other JComponents, you should override paintComponent, not paint. Also, the first method in this override should be super.paintComponent(g). This will refresh your screen and will eliminate the need for your having to fill the rectangle with a color on each iteration of paintComponent. You'll have to call setBackground(Color c) on your drawing JPanel somewhere in the initialization code though.
*) If you're using a Timer with Swing apps, consider using a Swing Timer. It works well and has the added convenience of having all code in it's actionlistener called on the event dispatch thread, the main Swing thread.
*) Finally, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview tab to make sure that your code is formatted correctly. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:
&#91;code]
  // your code block goes here.
  // note the differences between the tag at the top vs the bottom.
&#91;/code]or
{&#99;ode}
  // your code block goes here.
  // note here that the tags are the same.
{&#99;ode}Edited by: Encephalopathic on Aug 31, 2008 5:29 PM

Similar Messages

  • Stoping System Clock using Java code.

    Is it possible to do so if yes then some code idea for
    Stoping System Clock using Java code.
    [email protected]

    The system clock is controlled from the BIOS, and as such, I don't think even windoze can stop it, though it can be continually reset. I don't believe that Java provides a sufficiently low level of control to set the system clock, although you could use a native method to accomplish the task.
    Why would you want to do such a thing?

  • Alarm Clock Problem

    My alarm clock used to work correctly, but within the last two weeks, the only way to get it to turn off is to snooze it, turn off the phone, turn the phone back on, and then turn off the alarm (in the edit options of the alarm settings). The slide to the right option (to turn off the alarm, not snooze it), does not work, it only slides the screen to the right and the alarm continues to go off.

    Hello, valmichele14.
    Try closing all open applications in multitasking and power cycling the device.  Test to see if the unable to dismiss behavior persists. 
    iOS: Force an app to close
    http://support.apple.com/kb/ht5137
    An update was recently released that may also help with this issue. 
    iOS 7.0.3
    http://support.apple.com/kb/DL1691
    Cheers,
    Jason H. 

  • Alarm Clock on Nano

    My alarm on the 3rd gen Nano works when it wants to. Sometimes it'll go off at the selected time and play music for hours (i have it set to play music for the alarm), sometimes it'll only play music for 1 minute or so, and sometimes the alarm won't even go off. I have it set to repeat every day and it might work once. I constantly have to reset the alarm. Any help or info will be appreciated and thanks in advance.

    I have the same problem with my son's new nano 3rd gen. We let it do the firmware upgrade to 1.0.3 when we first plugged it in. We are trying to use it as his alarm clock using powered speakers (the inMotion mini which is great btw). However it seems to be inconsistent as to whether it plays the selected alarm playlist through the speakers. So far, after sitting overnight this has failed three nights in a row. However when testing it during the day we can get it to work most of the time if we are VERY careful about following the same procedure in setting up the alarm. Will try to downgrade as suggested here and confirm this solution.
    Another complaint about the alarm function is that once the alarm goes off, pressing the center button with the "snooze" function selected, does NOT silence the playlist. You have to also pause the music in a separate action. My feeling is that you if snooze should always be selected by default, and pressing the center button or any location on the click wheel (pressing, not just touching) should snooze the alarm and the music at the same time.

  • How can I turn my Mac using iTunes into an alarm clock?

    Here's my question: How can I turn my Mac, using iTunes, into an alarm clock?
    I want to be able to put my Mac to sleep but have it set up so that when it wakes up at the predetermined time iTunes will open and start playing the music I've selected....
    A "musical alarm clock"...
    A friend suggested using the Energy Saver option in Sys. Preferences to wake the computer at the right time. He said that then I could use iCal to start iTunes. I would then have to use an Apple Script to get iTunes to play the song I want.
    But is there a SIMPLER way to do this? Are there any programs out there that do this already??
    Thanks for any help....

    I recently got a copy of AlarmClock Pro from MacWorld UK. It is shareware, I think it is (USD?)19. I haven't had a chance to try it out much, but it does look quite good. I have an older version. The latest version looks even better.
    I'm sure there's plenty of freeware apps out there. You might also want to try looking here for an applescript here Doug's AppleScripts for iTunes
    p53

  • Docking/Using as alarm clock

    Hi all,
    This is my first post here so I apologise if I seem a bit simple - I've only had my iPhone 3G 8 days.
    I'm looking for a number of accessories for my iPhone. I have the case and the car adaptor sorted but would like a dock.
    What I need in my dock -
    - Good looks
    - Good sound Quality
    - An alarm clock (hopefully which can use any of the tunes on my iPhone for the alarm)
    - Is compatible with the iPhone 3G (no airplane mode or incompatibilites leading to no charging)
    - Generally quite easy to use but not too simple either (much like the iPhone not a simple system but very easy to use)
    All your recommendations welcome,
    Thanks

    Posted below is the link to all the audio dock speaker sets that work with iPhone 3G
    http://store.apple.com/us/browse/home/shopiphone/iphoneaccessories/speakers?n=iphone2&mco=MTI5MjUxMA&s=topSellers
    As far for as which are easy to use and get the most done. Not anyone I can really recommend. It depends on the budget you have set. And what features you are looking for in the long run. Most if not all of them seem to charge the iPhone while it is docked.
    There are a couple Alarm clock based stations listed below that I found.
    http://store.apple.com/us/product/TR499VC/A?n=iphone2&fnode=MTY1NDA5Mg&mco=MjQzM zg2MA&s=topSellers
    http://store.apple.com/us/product/TT881LL/A?n=iphone2&fnode=MTY1NDA5Mg&mco=MjQzM zgzMA&s=topSellers
    http://store.apple.com/us/product/TU698LL/A?n=iphone2&fnode=MTY1NDA5Mg&mco=MjQzM zg2NA&s=topSellers

  • Alarm Clock giving weird notification when using song

    When using my alarm clock with the new song feature, I am getting a notification that says "Download over cellular? Additional fees may apply when downloading songs over cellular." Options are Cancel or Download. The songs I have chosen are already on my iPhone so there should be 0 reason to have to download anything. Please advise.
    Thank you,
    Andrew

    Goto Settings--> General--> Cellular--> Is your cellular Data on? I turned mine off and it fixed the problem. You may also want to check Settings--> iTunes&App stores--> Use cellular Data and set to off so you don't eat up data on wifi. Some people have had big charges when left on. 

  • Using my Mac as alarm clock

    Hi- I notice in the Sys Prefs under Energy saver you can get the Mac to Startup (or Shutdown) at specific times.
    The problem is you can only set Weekends, Weekdays, Every Day or ONE day in the week. I work Saturdays but not Mondays, so I need to set the Mac to wake at 6:00 on Tues-Sat inc, and not on Sun/Mon.
    Can the Mac do this on it's own (Automator?), or will I need a bit of shareware?
    Thanks muchly!

    There's nothing more reliable than a windup alarm clock that you set every night... What if there's a power outage?
    Here's another one that looks pretty good (never used it myself) and it's donationware.
    http://www.robbiehanson.com/alarmclock/index.html
    If you leave your Mac ON all the time (using only display sleep) like I do, you can create a repeating event in iCal that sounds an alarm. I think it can also run a script, which can be used to launch iTunes and play a specific playlist. But I don't think iCal will wake up your Mac if it is sleeping.

  • Using the alarm clock

    I'm having trouble figuring out how to use the Alarm Clock feature on my iPod. If I set the alarm for a time that's only a minute or two in the future, it seems to work just fine. However, if I set it for ten minutes (or more) in the future, then just set it down, the iPod times out and shuts off after a few minutes of non-use. Then, when the alarm time comes around, nothing happens.
    Is the alarm clock supposed to be able to "turn on" the iPod when it is off? If so, why wouldn't it be doing it? If not, how do I prevent my iPod from turning off, so that the alarm will work?
    Dell Inspiron 9400   Windows XP Pro  

    Go to Extras > Clock > Sleep timer.
    Try setting it to off.

  • Can I use my iphone with a Bluetooth speaker as an alarm clock?

    Good morning!
    I use my iPhones alarm functionality to get me up and out in the morning. I was wondering if I had a Bluetooth speaker would the alarm sound thru it?
    A thousand thank!!
    N

    bluetooth is likely closed when the iphone is suspended if not then you could use it as alarm clock but
    bluetooth use battery so if not connected to AC it would likely suck your iphone dry in the night

  • HT1222 So do you all use timer more than alarm clock for iPhone 5?  pane?

    I find myself using alarm clock all the time and not the timer under the bottom quick control pane. Why hasn't Apple changed that?

    Nightfuse wrote:
    I find myself using alarm clock all the time and not the timer under the bottom quick control pane. Why hasn't Apple changed that?
    Submit your feedback directly to Apple using the appropriate link on the Feedback page:
    http://www.apple.com/feedback
    I'm wih Allan on this. And I've got one-button access to the Alarm. I use Siri.

  • Would like to use an alarm clock.  I need the alarm clock to function from the sleep mode.  A review said that MAC products will not allow a program to wake up a Mac.  Is this correct ?  Are there any products in the Mac store what will function from the

    Would like to use an alarm clock.  I need the alarm clock to function from the sleep mode.  A review said that MAC products will not allow a program to wake up a Mac.  Is this correct ?  Are there any products in the Mac store what will function from the sleep mode ?  How do I check this out ?

    You can set times for your computer to turn on or wake from sleep in the Energy Saver system preference pane using the Schedule... button.

  • I cannot get my iphone 4 unlock its got a "iphone not activated contact your carrier if this problem continues to occur" cant even use phone for alarm clock.

    I cannot get my iphone 4 unlock its got a "iphone not activated contact your carrier if this problem continues to occur" cant even use phone for alarm clock.

    Did you already follow these steps?
    iPhone: Troubleshooting activation issues
    Symptoms
    Your iPhone may display one of the following messages when you attempt to activate it:
    "Your iPhone could not be activated because the activation server is temporarily unavailable."
    "The SIM card inserted in this iPhone does not appear to be supported"
    "The iPhone is not recognized and cannot be activated for service."
    "iTunes was unable to verify your device."
    Resolution
    Perform the following steps if you receive one of the messages above:
    Restart the iPhone.
    Try another means of reaching the activation server and attempt to activate.
    Try connecting to a known-good Wi-Fi network if you're unable to activate using a cellular data connection.
    Try connecting to iTunes if you're unable to activate using Wi-Fi.
    Restore the iPhone.
    If you receive an alert message when you attempt to activate your iPhone, try to place the iPhone in recovery mode and perform a restore. If you're still unable to complete the setup assistant due to an activation error, contact Apple for assistance.
    Additional Information
    If you are asked to enter a password to proceed with activating the iPhone, Activation Lock may be enabled as part of Find my iPhone.

  • Can i use my iphone as an alarm clock?

    i'm wondering if i can use my iphone as an alarm clock/clock radio without connecting it to one of the third-party docks?

    You can use the clock feature as an alarm clock I do, nothing  to stop you just plugging in with the supplied cable and the mains plug or an Apple dock to hold the iPhone upright to the mains plug

  • CAN I USE MY PC LIKE AN "ALARM CLOCK"????

    I have an HP Touch Smart running Windows 8 (which I don't think means too much to this question).  O.K. Here it is- I'd like to set up my PC to send me "Alerts" kind of like an alarm clock.  If I need to be remimded of an appointment or if I put something in the oven & I need to go check in 1/2 hr, or esp. I want to get in on an auction & it starts in an hr. or so or a later date @ a certain time.  I'd like to st it up on my PC to alert me by a bell or whistle or even better a voice alert saying "the bla bla bla is starting at..."  Can I do this?  Thanks, I'm not great w/computers & how to use them to really simplify my life, normally, it just takes up a lot of my time as I can't type fast, have not set up speaking naturally as I ususally have background noise & think it would require "quiet" & I am also not great w/ instructions.  So all of you wonderfully smart people who know what they are doing, please help me!  Thanks much!

    Hi Mets4Me,
    Windows 8 is actually quite relevant to your situation - it is Made for this stuff.  Smiling.
    I do not know if there is actually "the exact APP" you seek out there, but you can certainly start looking and have some fun doing it.
    On your Start Screen, anywhere on the screen, type "Store" > then Click on the "Store" icon to get into the Windows Store
    Next, same thing:  You can start typing along the top part of the plain, white portion of the screen any words you want:  "alerts", or maybe "to do lists", or perhaps "clock"
    The Apps that with context for the keyword(s) used will come up on the screen.  You can click on any of the selections to see more about each App.  Be aware that some of the Apps cost money -- not every App is free.
    Alternatively, on the Main Store page
    You can use your finger (for a touch screen) or your mouse and slide the bar on the bottom to the right to see the different categories.  Along about 3/4 of the way to the end of the way you will find "Productivity" (after Finance).  You can pick, for example, the "Top Free" Apps list.
    Will you find something you like?  I do not know, but you might do so and if not, you might find something that will work well enough for the time being.
    Good luck!
    Kind Regards,
    Dragon-Fur

Maybe you are looking for

  • How do i turn off automatic plugin check on startup... I have turned off in options/advanced, but still checks on startup

    on firefox startup I get two tabs... one for google (my home page) the other for plugin check. I go to tools/advanced and turn off startup check for plugins, the restart firefox... the plugin check tabe still appears. All of my plugins are up to date

  • Dual SSID's

    I am looking into purchasing 2 1131AG's. What I want to do is have 1 access point on each floor of my buidling, 2 floors total. Now I want to make sure that when an employee gets on with a wireless laptop, he can access the wireless network with full

  • " Title on the warning : warning, a problem occurred"

    I've just bought final cut studio 2. i've installed it and everything seems ok, BUT... when I switch on the mac, a window now appears, bearing the symbol of FCP and it says, more or less (I translate it from french) : " Title on the warning : warning

  • Install flash with a batch file

    good morning every one! i need some help.here's the hole thing.i made an unattended XP installation and i wtote this little batch file "start /wait flash9.exe /q" . i want to install the flash active-x after the finish of xp installation,when it star

  • How to install HP F4583 deskjet??

    Hi, pls help, I am an absolute beginner and would like to install an HP F4583 deskjet on OS x 10.8.2. 1.) HP says I need to click on software update to get the driver from the app store, but no updates are available for me. I think that's fine, maybe