import javax.swing.*; import javax.swing.plaf.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; /** * Project/Class Description * This class implement a new JComponent named MyClock * It does nothing but display a clock. * * @author Stuart Hansen * @version September 3, 2003 */ public class MyClock extends JPanel implements Serializable { private double theta = 0; // the amount the pendulum has swung private double deltaTheta = Math.PI/20; // the amount the pendulum should swing private javax.swing.Timer ticktock; // the timer that causes the swing private boolean running = true; // is the clock running /** The default constructor sets the interface and starts the timer */ public MyClock() { setUI (new MyClockUI()); ticktock = new javax.swing.Timer(500, new TimerHandler()); ticktock.start(); // Try to keep the clock one size setSize(151, 251); setPreferredSize(new Dimension(151, 251)); setMaximumSize(new Dimension(151, 251)); setMinimumSize(new Dimension(151,251)); } /** We have only one look and feel for this object */ class MyClockUI extends ComponentUI { public void paint (Graphics g, JComponent jc) { Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); setBackground(Color.white); Graphics2D g2d = (Graphics2D)g; g2d.setColor(Color.black); // draw the outside of the clock g2d.drawLine(50, 0, 100, 0); g2d.drawLine(100, 0, 150, 50); g2d.drawLine(150, 50, 150, 100); g2d.drawLine(150, 100, 120, 130); g2d.drawLine(120, 130, 120, 240); g2d.drawLine(120, 240, 110, 250); g2d.drawLine(110, 250, 40, 250); g2d.drawLine(40, 250, 30, 240); g2d.drawLine(30, 240, 30, 130); g2d.drawLine(30, 130, 0, 100); g2d.drawLine(0, 100, 0, 50); g2d.drawLine(0, 50, 50, 0); // draw the clock face frame g2d.drawOval(20, 20, 110, 110); g2d.drawOval(15, 15, 120, 120); // draw the numbers on the face g2d.drawString("12", 67, 35); g2d.drawString("1", 95, 40); g2d.drawString("2", 112, 55); g2d.drawString("3", 120, 80); g2d.drawString("4", 112, 105); g2d.drawString("5", 95, 122); g2d.drawString("6", 71, 128); g2d.drawString("7", 47, 122); g2d.drawString("8", 30, 105); g2d.drawString("9", 23, 80); g2d.drawString("10", 28, 55); g2d.drawString("11", 45, 40); // draw the big hand double minuteTheta = Math.PI*(minute/30.0 + second/1800.0); g2d.rotate(minuteTheta, 75, 75); g2d.drawLine(75, 75, 72, 72); g2d.drawLine(75, 75, 78, 72); g2d.drawLine(72, 72, 75, 25); g2d.drawLine(78, 72, 75, 25); g2d.rotate(-minuteTheta, 75, 75); // draw the little hand double hourTheta = Math.PI*hour/6.0+minuteTheta/12.0; g2d.rotate(hourTheta, 75, 75); g2d.drawLine(75, 75, 72, 72); g2d.drawLine(75, 75, 78, 72); g2d.drawLine(72, 72, 75, 45); g2d.drawLine(78, 72, 75, 45); g2d.rotate(-hourTheta, 75, 75); // draw the regulator box g2d.drawRect(42, 142, 66, 96); g2d.drawRect(50, 150, 50, 80); // draw the regulator g2d.rotate(theta, 75, 150); g2d.drawOval(63, 198, 24, 24); g2d.drawLine(72, 151, 72, 198); g2d.drawLine(78, 151, 78, 198); } } /** This method starts and stops the clock */ public void setTicking(boolean running) { this.running = running; if (running) { ticktock.restart(); } else { ticktock.stop(); theta =0.0; repaint(); } } /** TickTockHandler takes care of the clock ticks. */ class TimerHandler implements ActionListener { // rotate the pendulum and redraw the clock public void actionPerformed (ActionEvent e) { if (theta > Math.PI/40 || theta < -Math.PI/40) deltaTheta *= -1; theta += deltaTheta; repaint(); } } }