alarm clock

simple java code

Page 1 of 1

7 Replies - 14412 Views - Last Post: 02 March 2010 - 05:55 PM Rate Topic: -----

#1 danie  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 18-November 07

alarm clock

Posted 02 March 2010 - 03:36 PM

/**I tried getting this to work, but its too complicated. I need something much simpler*/

package clock;

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

/** container class for the alarm clock 
 * @param <AlarmDialog>
 * @param <ClockInterface>
 * @param <TimeDisplay>
 * @param <TimeDialog>*/ 
public class AlarmClock<TimeDialog, AlarmDialog, ClockInterface, TimeDisplay> implements ActionListener
{
    /** main frame */ 
    private JFrame frame; 
    
    /** dialog for time set */ 
    private TimeDialog timeDialog; 

    /** dialog for alarm time set */ 
    private AlarmDialog alarmDialog; 

    /** the clock */ 
    private ClockInterface clock; 

    /** display for the current time */ 
    private Component timeDisplay; 

    /** display for alarm time */ 
    private TimeDisplay alarmDisplay; 

    /** thread object for the clock thread */ 
    private Thread clockThreadObject; 

    /** thread object for the alarm thread */ 
    private Thread alarmThreadObject; 

    /** menu item for exit */ 
    private JMenuItem exitMenuItem; 

    /** menu item for set time */ 
    private JMenuItem timeMenuItem; 

    /** menu item for set alarm time */ 
    private JMenuItem alarmTimeMenuItem; 

    /** responds to menu item activations */ 
    public void actionPerformed(ActionEvent e) 
    {
        if (e.getSource() == exitMenuItem)
        {
            System.exit(0); 
        }
        else if (e.getSource() == timeMenuItem)
        {
            timeDialog.showDialog(); 
        }
        else if (e.getSource() == alarmTimeMenuItem)
        {
            alarmDialog.showDialog(); 
        }
    }

    /** constructor which requires a clock, a clock thread, an alarm thread, 
        a display for the current time, and a display for the alarm time */ 
    public AlarmClock(
        ClockInterface clock, Runnable clockThread, Runnable alarmThread, 
        Component timeDisplay, TimeDisplay alarmDisplay)
    {
        this.clock = clock;

        // create threads 
        clockThreadObject = new Thread(clockThread); 
        alarmThreadObject = new Thread(alarmThread); 

        this.timeDisplay = timeDisplay; 
        this.alarmDisplay = alarmDisplay; 

        // create main frame
        frame = new JFrame(); 
        frame.setTitle("Alarm Clock"); 
        frame.getContentPane().setLayout(new BorderLayout(3,3)); 
        frame.getContentPane().add(timeDisplay, BorderLayout.CENTER); 
        frame.getContentPane().add(alarmDisplay, BorderLayout.SOUTH); 

        // create dialogs 
        timeDialog = new TimeDialog(frame, "Set time", clock); 
        alarmDialog = new AlarmDialog(frame, "Set alarm time", clock, alarmDisplay); 

        // create menu bar
        JMenuBar menuBar = new JMenuBar(); 
        frame.setJMenuBar(menuBar); 

        // menu creation 

        JMenu menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(menu);

        exitMenuItem = new JMenuItem("Exit",KeyEvent.VK_X);
        exitMenuItem.addActionListener(this); 
        menu.add(exitMenuItem); 

        menu = new JMenu("Set");
        menu.setMnemonic(KeyEvent.VK_E);
        menuBar.add(menu);

        timeMenuItem = new JMenuItem("Time",KeyEvent.VK_T);
        timeMenuItem.addActionListener(this); 

        menu.add(timeMenuItem); 

        alarmTimeMenuItem = new JMenuItem("Alarm time",KeyEvent.VK_A);
        alarmTimeMenuItem.addActionListener(this); 

        menu.add(alarmTimeMenuItem); 

        // show main frame 
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(screenSize.width/4, screenSize.height/4);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack(); 
        frame.setVisible(true); 

        // start the threads 
        clockThreadObject.start(); 
        alarmThreadObject.start(); 
    }
}

This post has been edited by JackOfAllTrades: 02 March 2010 - 05:45 PM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE!!!...[/code]


Is This A Good Question/Topic? -1
  • +

Replies To: alarm clock

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: alarm clock

Posted 02 March 2010 - 03:41 PM

Alright, danie, could you do something for us? Put your code between the code tags. :code:. Then tell us what you want. Right now, it's all blending together and not many people can see it well enough to help.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 danie  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 18-November 07

Re: alarm clock

Posted 02 March 2010 - 03:49 PM

/** This code makes the timer work, but its the alarm clock I need help with. I can tidy this up later. The timer shows the current times and it has a counter, which I dont need. I need help to set the alarm clock and print a message when it goes off. A snooze button is optional. If I can just get the alarm part to work!*/ 
package ex1;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

class Timer2 extends Thread{
	private JLabel l;
	
	
	public Timer2(JLabel label){
		    this.l=label;}
		    
    public void run(){
    	   while(true){
    	      Date d=new Date();
    	      int h=d.getHours();
    	      String time="PM";
    	      if (h>23){h-=24; time="AM";}
    	      l.setText(""+h+":"+d.getMinutes()+":"+d.getSeconds()+"  "+time);
    	      try{Thread.sleep(1000);}
			  catch(Exception e){}
    	   }
    	      }

}




class Timer implements Runnable{
	private JLabel l;
	
	
	public Timer(JLabel label){
		    this.l=label;}
		    
    public void run(){
    	   while(true){
    	      Date d=new Date();
    	      l.setText(d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
    	      try{Thread.sleep(1000);}
			  catch(Exception e){}
    	   }
    	      }

}

class Counter2 implements ActionListener {
    private int counter;
    private JFrame f1=new JFrame();
    private  JLabel l1=new JLabel("              ");
    private  JLabel l2=new JLabel("              ");
    private  JLabel l3=new JLabel("              ");
    private JLabel l4=new JLabel("ALARM");
    private JTextField h=new JTextField(5);
    private JTextField m=new JTextField(5);
    private JTextField s=new JTextField(5);
    private Font fnt=new Font("TimesNewRoman",Font.BOLD,16);
    private JButton b1=new JButton("STOP");
    private JButton b2=new JButton("RESTART");
    static Thread t5;
    static Thread t6;
    Timer2 t1;

    Counter2(){	counter=0;
                Container c=f1.getContentPane();
	          	l1.setFont(fnt); l2.setFont(fnt);
	          	l3.setFont(fnt);
	          	l1.setText("Counter:  "+0);
	          	f1.setLayout(new FlowLayout());
				f1.setSize(160,180);
				c.add(l1);   c.add(l2);
				c.add(l3);
				c.add(b1);c.add(b2);
				c.add(l4);
				c.add(h);c.add(m);c.add(s);
				b1.addActionListener(this);b2.addActionListener(this);
				f1.setVisible(true); }
    
    public void actionPerformed(ActionEvent e){
  	  Object target=e.getSource();
  	  if(target==b1){t5.suspend();
  	                 t1.suspend();}
  	  if(target==b2){t5.resume();}
    }
				
     public JLabel readLabel2(){return l2;}
     public JLabel readLabel3(){return l3;}
     
      public void display(){
				while(counter <100)
			        { 	counter++;
				 		l1.setFont(fnt);
				 		l1.setText("Counter:  "+counter);
			          	try{Thread.sleep(1000);}
						catch(Exception e){}
					  }
         }
      
      public void start_thread(){
          Timer t=new Timer (this.readLabel2());
          t1=new Timer2 (this.readLabel3());
          t5=new Thread(t);
          t1.start();
          t5.start();
      }

public static void main(String[] args)
    {
                     Counter2 c=new Counter2();
                     c.start_thread();

	                 c.display();
      }
                           }


Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: alarm clock

Posted 02 March 2010 - 03:52 PM

What are the problem(s) you're having? We can't help you if we don't know what the difference is between what you want to happen, and what is actually happening.

You already posted a thread like this once and it was closed, since we will not simply do your work for you. I hope you did actually write that code.
Was This Post Helpful? 0
  • +
  • -

#5 danie  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 18-November 07

Re: alarm clock

Posted 02 March 2010 - 04:00 PM

I assure you I did write the code in class.
I will try to be more specific, but I'm a beginner and an older man with very little programming knowledge.
The timer works. Only one time is needed, with a function to turn on or off the alarm. The time should be am or pm.
Textfields must be used to enter the time the alarm must display a message like: Wake up!!
All I need help with is to understand how the alarm part works.

Thank you for being patient,
Danie
Was This Post Helpful? -2
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: alarm clock

Posted 02 March 2010 - 05:39 PM

Thread and Swing don't go alone well
For that type of thing you should use javax.swing.Timer not Thread
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

Re: alarm clock

Posted 02 March 2010 - 05:49 PM

View Postdanie, on 02 March 2010 - 05:00 PM, said:

I assure you I did write the code in class.


And I will assure everyone else you got it from here.

Code, lies, and Google. They also don't mix.
Was This Post Helpful? 4
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10366
  • View blog
  • Posts: 38,403
  • Joined: 27-December 08

Re: alarm clock

Posted 02 March 2010 - 05:55 PM

Since you obviously want to put in effort to learn, I'll point you to some tutorials you may find helpful.

DIC Java Tutorials Section
Swing Timer API
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1