import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class ClockTester extends JPanel implements ActionListener { public static int WIDTH=800; public static int HEIGHT=600; private Font titleFont, regularFont, clockFont; private int x; private Timer motion, pulse; private Clock clock; public ClockTester() { //initialize variables here... titleFont = new Font("Roman", Font.BOLD, 18); clockFont = new Font("Roman", Font.BOLD, 72); regularFont = new Font("Helvetica", Font.PLAIN, 12); x=0; motion = new Timer(2, this); //1000=1 seconds pulse = new Timer(1000, this); //1000=1 seconds clock =new Clock(11, 59, 50); motion.start(); pulse.start(); } public static void main(String[] args) { ClockTester app= new ClockTester(); JFrame window = new JFrame("Clock Tester"); window.setSize(WIDTH, HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().add(app); window.setVisible(true); } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(),getHeight()); g.setColor(Color.BLUE); g.setFont(titleFont); g.drawString("Clock Tester", 20, 20); g.setColor(Color.BLACK); g.setFont(regularFont); //g.drawString("pulse = "+x, 20, 40); g.fillOval(x, 200, 10, 10); g.setColor(Color.RED); g.setFont(clockFont); g.drawString(clock.toString(), getWidth()/3, getHeight()/2); } public void actionPerformed(ActionEvent e) { if (e.getSource()==motion){ x=(x+1)%WIDTH; } else { // call the tick method clock.tick(); } repaint(); } }