Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Assignments

SampleClock


MyClocks.java

//-----------------------------------------------------------
// MyClocks.java
// Written by Chuck Cusack
// October, 2001
//
// Displays and updates various clocks.
// Uses instances of DigitalClock and AnalogClock
//
//-----------------------------------------------------------
import java.awt.event.*;
import aLibrary.*;
//-----------------------------------------------------------
public class MyClocks extends AWindow {
    private AnalogClock myClock;    // The Analog Clock
    private DigitalClock myClock2;  // The Digital Clock
    private int placedClock;        // 1==Analog, 2==Digital
    private AButton hourButton;     // Increment Hour Button
    private AButton minButton;     // Increment Minute Button
    private AButton secButton;     // Increment Second Button
    private AButton togButton;     // Switch Clocks Button
    private int hour;              // Current Hour
    private int minute;            // Current Minute
    private int second;            // Current Second
//---------------------------------------------------------------
// The contructor
// Postcondition: A 450x500 window is created with the 4 buttons
//    along the bottom, and the digital clock in place.
//    The buttons inrement minutes, seconds, and hours.
//    The last button switches between digital and analog clock
    public MyClocks() {
        super(50,50,450,500);              // Create the AWindow

     // Set the clock to this arbitrary time
        hour=0;        
        minute=15;
        second=30;

     // Create the two clocks
        myClock=new AnalogClock(hour,minute,second);
        myClock2=new DigitalClock(hour,minute,second);
        myClock.place(this);
        myClock.setVisible(false);
        myClock2.place(this);
        placedClock=2;

     // Set up the increment hour button
        hourButton=new AButton(10,460,100,30);
        hourButton.setText("Hour");
        hourButton.addActionListener(this);
        hourButton.place(this);

     // Set up the increment minute button
        minButton=new AButton(120,460,100,30);
        minButton.setText("Minute");
        minButton.addActionListener(this);
        minButton.place(this);

     // Set up the increment second button
        secButton=new AButton(230,460,100,30);
        secButton.setText("Second");
        secButton.addActionListener(this);
        secButton.place(this);

     // Set up the toggle button
        togButton=new AButton(340,460,100,30);
        togButton.setText("Toggle");
        togButton.addActionListener(this);
        togButton.place(this);

        this.repaint();
    }
//-------------------------------------------------------------------------
// Increment the hour on all clocks
// Postconditon: The hours of the clocks are all incremented by 1
//
    public void incrementHours() {
        myClock.incrementHours();
        myClock2.incrementHours();
    }
//-------------------------------------------------------------------------
// Increment the minute on all clocks
// Postconditon: The minutes of the clocks are all incremented by 1
//
    public void incrementMinutes() {
        myClock.incrementMinutes();
        myClock2.incrementMinutes();
    }
//-------------------------------------------------------------------------
// Increment the second on all clocks
// Postconditon: The seocnds of the clocks are all incremented by 1
//
    public void incrementSeconds() {
        myClock.incrementSeconds();
        myClock2.incrementSeconds();
    }
//-------------------------------------------------------------------------
// The action handler for the 4 buttons
//
    public void actionPerformed(ActionEvent e) {
     // If the hour button is pushed, perform incrementHours
        if(e.getSource()==hourButton) {
 
            incrementHours();
        }
     // If the minute button is pushed, perform incrementMinutes
        else if(e.getSource()==minButton) {
            incrementMinutes();
        }
     // If the seconds button is pushed, perform incrementSeconds
        else if(e.getSource()==secButton) {
            incrementSeconds();
        }
     // If the toggle button is pushed, switch clock views
        else if(e.getSource()==togButton) {
            if(placedClock==1) {
               //myClock.remove();
               myClock.setVisible(false);
               myClock2.refresh();
               myClock2.setVisible(true);
               this.repaint();
               placedClock=2;
            }
            else if(placedClock==2) {
              myClock2.setVisible(false);
              myClock.refresh();
              myClock.setVisible(true);
              this.repaint();
              placedClock=1;
            }
        }
    }
//-------------------------------------------------------------------------

}