CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    6

    Need help with digital clock.

    Im trying to learn Java and have a problem with my digital clock. The Clock works fine but i want to use a JButton that sets an alarm at an given time, and then changes the backgroundcolor of the contentpane. I've tried alot of stuff and this is the best i can come up to. Can anyone help make it better?

    Logic Class


    Code:
    import java.awt.Color;
    import java.util.Calendar;
    import java.util.List;
    
    import javax.swing.SwingWorker;
    
    
    public class Clock extends SwingWorker<Void, Void>{
    	private int alarm_hour;
    	private int alarm_minute;
    	public int alarm_second;
    	private int time_hour, time_minute, time_second;
    	
    	ClockUI myGUI;
    	public Clock(ClockUI gui){
    	myGUI = gui;
    		
    	}
    	
    	
    	@Override
    	protected Void doInBackground() throws Exception {
    		
    		
    		while(isCancelled() == false)
    		{
    		this.publish();
    		
    		// If alarmbutton is pressed alarm_second equals 30 and Thread sleeps for 30 seconds then activate alarm.
    		
            if(alarm_second == 30){
    		Thread.sleep(30000);
    		System.out.println(alarm_second);
    		System.out.println(time_second);
    		myGUI.invokeAlarm();
    		
    		
            }
    		}
    		
    		
    		  return null;
    }	
    	
    	
    	protected void process(List<Void> chunks) {
    		
    		// Get hour, minute, second 
    		
    		Calendar myTime = Calendar.getInstance();
    		time_hour = myTime.get(Calendar.HOUR_OF_DAY);
    		time_minute = myTime.get(Calendar.MINUTE);
    		time_second = myTime.get(Calendar.SECOND);
    		 
    		 myGUI.setTime(time_hour, time_minute, time_second);
    	}
    	
    	public void setAlarm(int second){
    
    		alarm_second = second;
    		System.out.println(alarm_second);
    	}
    			
    			}



    GUI Class
    Code:
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.util.Calendar;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Font;
    import javax.swing.SwingConstants;
    
    
    public class ClockUI extends JFrame {
    
    	private String time;
    	private JPanel contentPane;
    	private JLabel ClockLabel;
    	private Clock myClock;
    	String _hours;
    	String _minutes;
    	String _seconds;
    	int hour, minute, second;
    	public boolean alarmbutton;
    
    	
    	
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					ClockUI frame = new ClockUI();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public ClockUI() {
    		setTitle("Digital Clock ");
    		myClock = new Clock(ClockUI.this);
    		myClock.execute();
    
    		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 233, 170);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		ClockLabel = new JLabel();
    		ClockLabel.setHorizontalAlignment(SwingConstants.CENTER);
    		ClockLabel.setFont(new Font("Calibri", Font.PLAIN, 40));
    	    ClockLabel.setBounds(22, 11, 172, 74);
    		 contentPane.add(ClockLabel);
    		   
    		   JButton Alarm = new JButton("Alarm");
    		   Alarm.addActionListener(new ActionListener() {
    		   	public void actionPerformed(ActionEvent arg0) {
    		   		
    		   		myClock = new Clock(ClockUI.this);
                    myClock.execute();
                    myClock.setAlarm(30);   
    
    		   	
    		   	}});
    		   Alarm.setBounds(60, 96, 89, 23);
    		   contentPane.add(Alarm);
    		
    	}
    	public void setTime(int time_hour, int time_minute, int time_second){	
    		
    		
    		_hours=Integer.toString(time_hour);
    	     _minutes=Integer.toString(time_minute);
    		_seconds=Integer.toString(time_second);
    	      
    	       if(time_hour < 10){
    	    	    
    	    	   // Adds zero if time is under 10
    	    	   
    	    	   _hours = "0" + _hours;
    	}
    	       if(time_minute < 10)
    	       {
    	    	   _minutes = "0" + _minutes;
    	       }
    	       if(time_second < 10){
    	    	   _seconds = "0" + _seconds;
    	       }
    	       
    	       time = _hours + ":" +   _minutes + ":" + _seconds;
    	       
    		   ClockLabel.setText(time);
    	
    }
    	public void invokeAlarm(){
    		
    		
    		
    	Color d = new Color(3333);
    	contentPane.setBackground(d);
    
    		}
    			}

  2. #2
    Join Date
    Nov 2011
    Posts
    189

    Re: Need help with digital clock.

    What's the problem? I'll try to help but i don't have access to my pc so i cant run it

  3. #3
    Join Date
    Feb 2012
    Posts
    6

    Re: Need help with digital clock.

    Quote Originally Posted by cens View Post
    What's the problem? I'll try to help but i don't have access to my pc so i cant run it

    The alarm is not decided by the time i put in a variable but instead it only works by me setting Thread.sleep(insert time to alarm to go off)

    But i've fixed it now by myself
    I've also added textfield in this code to input an alarm.
    If anyone want to give some feedback on the code, please do so!

    Logic Class

    Code:
    import java.util.Calendar;
    import java.util.List;
    
    import javax.swing.SwingWorker;
    
    
    public class Clock extends SwingWorker<Void, Void>{
    	private int alarm_hour;
    	private int alarm_minute;
    	public int alarm_second;
    	private int time_hour, time_minute, time_second;
    	private boolean alarm;
    	ClockUI myGUI;
    	
    	
    	public Clock(ClockUI gui){
    	myGUI = gui;
    		
    	}
    	
    	
    	@Override
    	protected Void doInBackground() throws Exception {
    		
    		
    		while(isCancelled() == false)
    		{
    			
    if(time_hour == alarm_hour && time_minute == alarm_minute && time_second == alarm_second && alarm == true){
    				myGUI.invokeAlarm();
    }
    
    
    this.publish();
    Thread.sleep(1000);
    
    }
           
    return null;
    }	
    	
    	
    	protected void process(List<Void> chunks) {
    		
    		// Get hour, minute, second 
    		
    		Calendar myTime = Calendar.getInstance();
    		time_hour = myTime.get(Calendar.HOUR_OF_DAY);
    		time_minute = myTime.get(Calendar.MINUTE);
    		time_second = myTime.get(Calendar.SECOND);
    		 
    		 myGUI.setTime(time_hour, time_minute, time_second);
    	}
    	
    	public void setAlarm(int hour, int minute, int second){
    
    		alarm_hour = hour;
    		alarm_minute = minute;
    		alarm_second = second;
    	}
    		
    			public void setBoolean(boolean Alarm){
    				alarm = Alarm;
    			}
    	
    	
    			}
    GUI Class
    Code:
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.border.EmptyBorder;
    import java.awt.Toolkit;
    import javax.swing.JTextField;
    import javax.swing.border.EtchedBorder;
    import javax.swing.UIManager;
    import javax.swing.border.MatteBorder;
    
    
    public class ClockUI extends JFrame {
    
    	private String time;
    	private JPanel contentPane;
    	private JLabel ClockLabel;
    	private Clock myClock;
    	String _hours;
    	String _minutes;
    	String _seconds;
    	int hour, minute, second;
    	private String myHour, myMin, mySec;
    	int alarmHour, alarmMinute, alarmSecond;
    	private JTextField txtHour;
    	private JTextField txtMinute;
    	private JTextField txtSecond;
    
    	
    	
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					ClockUI frame = new ClockUI();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			} 
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public ClockUI() {
    		setIconImage(Toolkit.getDefaultToolkit().getImage(ClockUI.class.getResource("/pixel/clock_icon.png")));
    		setTitle(" Clock ");
    		myClock = new Clock(ClockUI.this);
    		myClock.execute();
    
    		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 218, 175);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		ClockLabel = new JLabel();
    		ClockLabel.setOpaque(true);
    		ClockLabel.setForeground(Color.BLACK);
    		ClockLabel.setBackground(Color.WHITE);
    		ClockLabel.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
    		ClockLabel.setHorizontalAlignment(SwingConstants.CENTER);
    		ClockLabel.setFont(new Font("Calibri", Font.PLAIN, 40));
    	    ClockLabel.setBounds(10, 11, 182, 74);
    		 contentPane.add(ClockLabel);
    		   
    		   JButton Alarm = new JButton("Alarm");
    		   Alarm.setBackground(Color.WHITE);
    		   Alarm.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
    		   Alarm.addActionListener(new ActionListener() {
    		   	public void actionPerformed(ActionEvent arg0) {
    		   		
    		   		
    		   		
    		   		myHour = new String (txtHour.getText());
    				myMin = new String (txtMinute.getText());
    				mySec = new String (txtSecond.getText());
    				
    				
    			    int _alarmHour = Integer.parseInt(myHour);
    			    int _alarmMinute = Integer.parseInt(myMin);
    			    int _alarmSecond = Integer.parseInt(mySec);
    		   		
    			    alarmHour = _alarmHour;
    			    alarmMinute = _alarmMinute;
    			    alarmSecond = _alarmSecond;
    
    			    myClock.setAlarm(alarmHour, alarmMinute, alarmSecond);
    			    
    			    txtHour.setText("");
    			    txtMinute.setText("");
    			    txtSecond.setText("");
    			    
    			    myClock.setBoolean(true);
    
    //              myClock.setAlarm(21,45,00);   
    
    		   	
    		   	}});
    		   Alarm.setBounds(124, 96, 68, 25);
    		   contentPane.add(Alarm);
    		   
    		   txtHour = new JTextField();
    		   txtHour.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
    		   txtHour.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		   txtHour.setBounds(10, 96, 36, 25);
    		   contentPane.add(txtHour);
    		   txtHour.setColumns(10);
    		   
    		   txtMinute = new JTextField();
    		   txtMinute.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
    		   txtMinute.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		   txtMinute.setColumns(10);
    		   txtMinute.setBounds(45, 96, 36, 25);
    		   contentPane.add(txtMinute);
    		   
    		   txtSecond = new JTextField();
    		   txtSecond.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
    		   txtSecond.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		   txtSecond.setColumns(10);
    		   txtSecond.setBounds(78, 96, 36, 25);
    		   contentPane.add(txtSecond);
    		
    	}
    	public void setTime(int time_hour, int time_minute, int time_second){	
    		
    		
    		_hours=Integer.toString(time_hour);
    	     _minutes=Integer.toString(time_minute);
    		_seconds=Integer.toString(time_second);
    	      
    	       if(time_hour < 10){
    	    	    
    	    	   // Adds zero if time is under 10
    	    	   
    	    	   _hours = "0" + _hours;
    	}
    	       if(time_minute < 10)
    	       {
    	    	   _minutes = "0" + _minutes;
    	       }
    	       if(time_second < 10){
    	    	   _seconds = "0" + _seconds;
    	       }
    	       
    	       time = _hours + ":" +   _minutes + ":" + _seconds;
    	       
    		   ClockLabel.setText(time);
    	
    }
    	public void invokeAlarm(){
    		
    	
    		 Color d = new Color((float) Math.random(), (float) Math.random(),(float) Math.random());
    		 contentPane.setBackground(d);
    
    		}
    			}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured