We help IT Professionals succeed at work.

Alarm Clock

Mike Eghtebas
Mike Eghtebas asked
on
To build an alarm clock, the attached code shows current hour/minute/second. Need help to:

1. Set alarm h/m/s and store it in an array and close set alarm screen.
    (display current time was on the alarm clock)

2. If alarm check box is true, then beep 10 times.  

Thank you.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;

public class AlarmClock extends JApplet implements ActionListener{

    private JCheckBox chkAlarm = new JCheckBox("Alarm");
    private JButton btnSetAlarm = new JButton("Set Alarm");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JLabel lblHourDisplay = new JLabel("9");
    private JLabel lblMinuteDisplay = new JLabel("24");
    private JLabel lblSecondDisplay = new JLabel("49");
    private AudioClip audioClip;
    Font f1 = new Font("Arial", 1, 36);
    Font f2 = new Font("Arial", 1, 12);
    private Timer timer = new Timer(0, new TimerListener());
    Border border = BorderFactory.createLineBorder(Color.GRAY);
    
    public void init() {

        setCurrentTime();
        chkAlarm.addActionListener(this);
        btnSetAlarm.addActionListener(this);

        JPanel pane1 = new JPanel(new GridLayout(0, 3, 0, 0));
        lblHour.setFont(f2); lblMinute.setFont(f2); lblSecond.setFont(f2);
        lblHour.setHorizontalAlignment(0); lblMinute.setHorizontalAlignment(0); lblSecond.setHorizontalAlignment(0);
        pane1.add(lblHour);
        pane1.add(lblMinute);
        pane1.add(lblSecond);
        add(pane1, BorderLayout.NORTH);
        
        JPanel pane2 = new JPanel(new GridLayout(0, 3, 5, 0));
        lblHourDisplay.setFont(f1); lblMinuteDisplay.setFont(f1); lblSecondDisplay.setFont(f1);
        lblHourDisplay.setHorizontalAlignment(0); lblMinuteDisplay.setHorizontalAlignment(0); lblSecondDisplay.setHorizontalAlignment(0);
        lblHourDisplay.setBackground(Color.white); lblMinuteDisplay.setBackground(Color.white); lblSecondDisplay.setBackground(Color.white);
        //lblHourDisplay.setBorder(border); lblMinuteDisplay.setBorder(border); lblSecondDisplay.setBorder(border);
        lblHourDisplay.setOpaque(true); lblMinuteDisplay.setOpaque(true); lblSecondDisplay.setOpaque(true);
        
        pane2.add(lblHourDisplay); pane2.add(lblMinuteDisplay); pane2.add(lblSecondDisplay);
        add(pane2, BorderLayout.CENTER);
        
        JPanel pane3 = new JPanel();
        pane3.add(chkAlarm);
        pane3.add(btnSetAlarm);
        add(pane3, BorderLayout.SOUTH);
        timer.start();
    }

    public void setCurrentTime() {
        
          Calendar calendar = new GregorianCalendar();
          String am_pm;
          int hour = calendar.get(Calendar.HOUR);
          int minute = calendar.get(Calendar.MINUTE);
          int second = calendar.get(Calendar.SECOND);
          if(calendar.get(Calendar.AM_PM) != 0)
              hour +=12;
          
          lblHourDisplay.setText("" + hour);
          lblMinuteDisplay.setText("" + minute);
          lblSecondDisplay.setText("" + second);
    }
    
    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
//          System.out.print("llkk");
          setCurrentTime();
//          repaint();
        }
    } 
    public void actionPerformed(ActionEvent e) 
    {
        Object source = e.getSource();
        
        if (source == btnSetAlarm) {
            System.out.println("test ok btnSetAlarm");
            AlarmClockReset restAlarm = new AlarmClockReset();
            restAlarm.setTitle("Set Alarm");
            restAlarm.setSize(200, 160);
            restAlarm.setLocationRelativeTo(null);
//            restAlarm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            restAlarm.setVisible(true);
        
        }else if (source == chkAlarm) {
            System.out.println("test ok chkAlarm");
        }
    }
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame();
        frame.setTitle("Exercise 18.22: Alarm Clock");
        frame.setSize(300, 160);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	AlarmClock tp = new AlarmClock();
	tp.init();
	frame.getContentPane().add(tp);
        frame.setVisible(true);
        
       
    }
}

class AlarmClockReset extends JFrame implements ActionListener{
   
    private JButton btnOK;
    private JButton btnCancel;
    private JLabel lblInstruction;
    private JLabel lblHour;
    private JLabel lblMinute;
    private JLabel lblSecond;
    private JTextField txtHour;
    private JTextField txtMinute;
    private JTextField txtSecond;

    public AlarmClockReset() {
        
        btnOK = new JButton("OK");
        btnCancel = new JButton("Cancel");
        lblInstruction = new JLabel("Enter Hour, Minute, and Second");
        lblHour = new JLabel("Hour");
        lblMinute = new JLabel("Minute");
        lblSecond = new JLabel("Second");
        txtHour = new JTextField(2);
        txtMinute = new JTextField(2);
        txtSecond = new JTextField(2);

        btnOK.addActionListener(this);
        btnCancel.addActionListener(this);

        JPanel p1 = new JPanel(new BorderLayout());

        p1.add(lblInstruction, BorderLayout.NORTH);
        JPanel p2 = new JPanel(new GridLayout(4, 0));
        p2.add(lblHour); p2.add(txtHour);
        p2.add(lblMinute); p2.add(txtMinute);
        p2.add(lblSecond); p2.add(txtSecond);
        p2.add(btnOK); p2.add(btnCancel);

        Container c1 =  this.getContentPane();
        c1.setLayout(new BorderLayout());
        c1.add(p1, BorderLayout.NORTH);
        c1.add(p2, BorderLayout.CENTER);
    }    
    
    public void actionPerformed(ActionEvent e) 
    {
        Object source = e.getSource();
        if (source.equals(btnOK)) {
                System.out.println("test ok btnOK");
        }else if (source == btnCancel) {
            System.out.println("test ok btnCancel");
        }
    }
}

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:

this wil set time in the array as you requested, though
I would rathe have stored it in a datae object:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;

public class AlarmClock extends JApplet implements ActionListener{

    private JCheckBox chkAlarm = new JCheckBox("Alarm");
    private JButton btnSetAlarm = new JButton("Set Alarm");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JLabel lblHourDisplay = new JLabel("9");
    private JLabel lblMinuteDisplay = new JLabel("24");
    private JLabel lblSecondDisplay = new JLabel("49");
    private AudioClip audioClip;
    Font f1 = new Font("Arial", 1, 36);
    Font f2 = new Font("Arial", 1, 12);
    private Timer timer = new Timer(0, new TimerListener());
    Border border = BorderFactory.createLineBorder(Color.GRAY);
    int [] alarmTime;

    public void init() {

        setCurrentTime();
        chkAlarm.addActionListener(this);
        btnSetAlarm.addActionListener(this);

        JPanel pane1 = new JPanel(new GridLayout(0, 3, 0, 0));
        lblHour.setFont(f2); lblMinute.setFont(f2); lblSecond.setFont(f2);
        lblHour.setHorizontalAlignment(0); lblMinute.setHorizontalAlignment(0); lblSecond.setHorizontalAlignment(0);
        pane1.add(lblHour);
        pane1.add(lblMinute);
        pane1.add(lblSecond);
        add(pane1, BorderLayout.NORTH);

        JPanel pane2 = new JPanel(new GridLayout(0, 3, 5, 0));
        lblHourDisplay.setFont(f1); lblMinuteDisplay.setFont(f1); lblSecondDisplay.setFont(f1);
        lblHourDisplay.setHorizontalAlignment(0); lblMinuteDisplay.setHorizontalAlignment(0); lblSecondDisplay.setHorizontalAlignment(0);
        lblHourDisplay.setBackground(Color.white); lblMinuteDisplay.setBackground(Color.white); lblSecondDisplay.setBackground(Color.white);
        //lblHourDisplay.setBorder(border); lblMinuteDisplay.setBorder(border); lblSecondDisplay.setBorder(border);
        lblHourDisplay.setOpaque(true); lblMinuteDisplay.setOpaque(true); lblSecondDisplay.setOpaque(true);

        pane2.add(lblHourDisplay); pane2.add(lblMinuteDisplay); pane2.add(lblSecondDisplay);
        add(pane2, BorderLayout.CENTER);

        JPanel pane3 = new JPanel();
        pane3.add(chkAlarm);
        pane3.add(btnSetAlarm);
        add(pane3, BorderLayout.SOUTH);
        timer.start();
    }

    public void setCurrentTime() {

          Calendar calendar = new GregorianCalendar();
          String am_pm;
          int hour = calendar.get(Calendar.HOUR);
          int minute = calendar.get(Calendar.MINUTE);
          int second = calendar.get(Calendar.SECOND);
          if(calendar.get(Calendar.AM_PM) != 0)
              hour +=12;

          lblHourDisplay.setText("" + hour);
          lblMinuteDisplay.setText("" + minute);
          lblSecondDisplay.setText("" + second);
    }

    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
//          System.out.print("llkk");
          setCurrentTime();
//          repaint();
        }
    }
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();

        if (source == btnSetAlarm) {
            System.out.println("test ok btnSetAlarm");
            AlarmClockReset restAlarm = new AlarmClockReset(this);
            restAlarm.setTitle("Set Alarm");
            restAlarm.setSize(200, 160);
            restAlarm.setLocationRelativeTo(null);
//            restAlarm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            restAlarm.setVisible(true);

        }else if (source == chkAlarm) {
            System.out.println("test ok chkAlarm");
        }
    }

    public void setAlarmTime(int [] ar) {
        this.alarmTime = ar;

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setTitle("Exercise 18.22: Alarm Clock");
        frame.setSize(300, 160);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	AlarmClock tp = new AlarmClock();
	tp.init();
	frame.getContentPane().add(tp);
        frame.setVisible(true);


    }
}

class AlarmClockReset extends JFrame implements ActionListener{

    private JButton btnOK;
    private JButton btnCancel;
    private JLabel lblInstruction;
    private JLabel lblHour;
    private JLabel lblMinute;
    private JLabel lblSecond;
    private JTextField txtHour;
    private JTextField txtMinute;
    private JTextField txtSecond;

    int timeArray[];
    AlarmClock ap;

    public AlarmClockReset(AlarmClock ap) {
        this.ap = ap;

        timeArray = new int[3];
        btnOK = new JButton("OK");
        btnCancel = new JButton("Cancel");
        lblInstruction = new JLabel("Enter Hour, Minute, and Second");
        lblHour = new JLabel("Hour");
        lblMinute = new JLabel("Minute");
        lblSecond = new JLabel("Second");
        txtHour = new JTextField(2);
        txtMinute = new JTextField(2);
        txtSecond = new JTextField(2);

        btnOK.addActionListener(this);
        btnCancel.addActionListener(this);

        JPanel p1 = new JPanel(new BorderLayout());

        p1.add(lblInstruction, BorderLayout.NORTH);
        JPanel p2 = new JPanel(new GridLayout(4, 0));
        p2.add(lblHour); p2.add(txtHour);
        p2.add(lblMinute); p2.add(txtMinute);
        p2.add(lblSecond); p2.add(txtSecond);
        p2.add(btnOK); p2.add(btnCancel);

        Container c1 =  this.getContentPane();
        c1.setLayout(new BorderLayout());
        c1.add(p1, BorderLayout.NORTH);
        c1.add(p2, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source.equals(btnOK)) {
                System.out.println("test ok btnOK");
             timeArray[0] = (new Integer(txtHour.getText()).intValue());
              timeArray[1] = (new Integer(txtMinute.getText()).intValue()); 
              timeArray[2] = (new Integer(txtSecond.getText()).intValue());
            ap.setAlarmTime(timeArray);
            this.dispose();

        }else if (source == btnCancel) {
            System.out.println("test ok btnCancel");
              this.dispose();
        }
    }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:
But you were studying this beeping stuff extensively - so add what you found best in a sense of making sounds
Awarded 2011
Awarded 2011
Commented:
This one is even playing - can't stand it 10 times - but at least 3 times and based on hour - minute
it is using my SimpleAudioPlayer class - you should have it already

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;

public class AlarmClock extends JApplet implements ActionListener{

    private JCheckBox chkAlarm = new JCheckBox("Alarm");
    private JButton btnSetAlarm = new JButton("Set Alarm");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JLabel lblHourDisplay = new JLabel("9");
    private JLabel lblMinuteDisplay = new JLabel("24");
    private JLabel lblSecondDisplay = new JLabel("49");
    private AudioClip audioClip;
    Font f1 = new Font("Arial", 1, 36);
    Font f2 = new Font("Arial", 1, 12);
    private Timer timer = new Timer(0, new TimerListener());
    Border border = BorderFactory.createLineBorder(Color.GRAY);
    int [] alarmTime;

    public void init() {
          alarmTime = new int[3];
        setCurrentTime();
        chkAlarm.addActionListener(this);
        btnSetAlarm.addActionListener(this);



        JPanel pane1 = new JPanel(new GridLayout(0, 3, 0, 0));
        lblHour.setFont(f2); lblMinute.setFont(f2); lblSecond.setFont(f2);
        lblHour.setHorizontalAlignment(0); lblMinute.setHorizontalAlignment(0); lblSecond.setHorizontalAlignment(0);
        pane1.add(lblHour);
        pane1.add(lblMinute);
        pane1.add(lblSecond);
        add(pane1, BorderLayout.NORTH);

        JPanel pane2 = new JPanel(new GridLayout(0, 3, 5, 0));
        lblHourDisplay.setFont(f1); lblMinuteDisplay.setFont(f1); lblSecondDisplay.setFont(f1);
        lblHourDisplay.setHorizontalAlignment(0); lblMinuteDisplay.setHorizontalAlignment(0); lblSecondDisplay.setHorizontalAlignment(0);
        lblHourDisplay.setBackground(Color.white); lblMinuteDisplay.setBackground(Color.white); lblSecondDisplay.setBackground(Color.white);
        //lblHourDisplay.setBorder(border); lblMinuteDisplay.setBorder(border); lblSecondDisplay.setBorder(border);
        lblHourDisplay.setOpaque(true); lblMinuteDisplay.setOpaque(true); lblSecondDisplay.setOpaque(true);

        pane2.add(lblHourDisplay); pane2.add(lblMinuteDisplay); pane2.add(lblSecondDisplay);
        add(pane2, BorderLayout.CENTER);

        JPanel pane3 = new JPanel();
        pane3.add(chkAlarm);
        pane3.add(btnSetAlarm);
        add(pane3, BorderLayout.SOUTH);
        timer.start();
    }

    public void setCurrentTime() {

          Calendar calendar = new GregorianCalendar();
          String am_pm;
          int hour = calendar.get(Calendar.HOUR);
          int minute = calendar.get(Calendar.MINUTE);
          int second = calendar.get(Calendar.SECOND);
          if(calendar.get(Calendar.AM_PM) != 0)
              hour +=12;

          lblHourDisplay.setText("" + hour);
          lblMinuteDisplay.setText("" + minute);
          lblSecondDisplay.setText("" + second);

                   if(alarmTime[0] == hour  && alarmTime[1] == minute & chkAlarm.isSelected()) {
                   for(int j=0; j<3; j++)
                 SimpleAudioPlayer.play("tada.wav");   }
        

    }

    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
//          System.out.print("llkk");
          setCurrentTime();
//          repaint();
        }
    }
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();

        if (source == btnSetAlarm) {
            System.out.println("test ok btnSetAlarm");
            AlarmClockReset restAlarm = new AlarmClockReset(this);
            restAlarm.setTitle("Set Alarm");
            restAlarm.setSize(200, 160);
            restAlarm.setLocationRelativeTo(null);
//            restAlarm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            restAlarm.setVisible(true);

        }else if (source == chkAlarm) {
            System.out.println("test ok chkAlarm");
        }
    }

    public void setAlarmTime(int [] ar) {
        this.alarmTime = ar;

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setTitle("Exercise 18.22: Alarm Clock");
        frame.setSize(300, 160);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	AlarmClock tp = new AlarmClock();
	tp.init();
	frame.getContentPane().add(tp);
        frame.setVisible(true);


    }
}

class AlarmClockReset extends JFrame implements ActionListener{

    private JButton btnOK;
    private JButton btnCancel;
    private JLabel lblInstruction;
    private JLabel lblHour;
    private JLabel lblMinute;
    private JLabel lblSecond;
    private JTextField txtHour;
    private JTextField txtMinute;
    private JTextField txtSecond;

    int timeArray[];
    AlarmClock ap;

    public AlarmClockReset(AlarmClock ap) {
        this.ap = ap;

        timeArray = new int[3];
        btnOK = new JButton("OK");
        btnCancel = new JButton("Cancel");
        lblInstruction = new JLabel("Enter Hour, Minute, and Second");
        lblHour = new JLabel("Hour");
        lblMinute = new JLabel("Minute");
        lblSecond = new JLabel("Second");
        txtHour = new JTextField(2);
        txtMinute = new JTextField(2);
        txtSecond = new JTextField(2);

        btnOK.addActionListener(this);
        btnCancel.addActionListener(this);

        JPanel p1 = new JPanel(new BorderLayout());

        p1.add(lblInstruction, BorderLayout.NORTH);
        JPanel p2 = new JPanel(new GridLayout(4, 0));
        p2.add(lblHour); p2.add(txtHour);
        p2.add(lblMinute); p2.add(txtMinute);
        p2.add(lblSecond); p2.add(txtSecond);
        p2.add(btnOK); p2.add(btnCancel);

        Container c1 =  this.getContentPane();
        c1.setLayout(new BorderLayout());
        c1.add(p1, BorderLayout.NORTH);
        c1.add(p2, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source.equals(btnOK)) {
                System.out.println("test ok btnOK");
             timeArray[0] = (new Integer(txtHour.getText()).intValue());
              timeArray[1] = (new Integer(txtMinute.getText()).intValue()); 
              timeArray[2] = (new Integer(txtSecond.getText()).intValue());
            ap.setAlarmTime(timeArray);
            this.dispose();

        }else if (source == btnCancel) {
            System.out.println("test ok btnCancel");
              this.dispose();
        }
    }
}

Open in new window

Mike EghtebasDatabase and Application Developer

Author

Commented:
re:> can't stand it 10 times - but at least 3 times a

You are right. I was thinking of having system Beep(). But an audio is of course better.

Now I am trying to make it to play only once.
Awarded 2011
Awarded 2011

Commented:
no problem- just change 3 in the loop index to 1
Mike EghtebasDatabase and Application Developer

Author

Commented:
for_yan,

You will see lots of changes in my coding sensibility. I know it has perhaps been difficult to look at some of my codes. But starting 2012, my submission will improve.

Thank you for your patience.

Mike
Awarded 2011
Awarded 2011

Commented:
wasted time darwing your cars - you deleted the question in the meantime

Anyway, i added the buttons, but was lazy doing these pictures
It was enough work adjustung these sizes with buttons.
You have applet with that disk and logo rotating - the same thing with these pictures
- just change your Car class and adddrwaing image - should be even simpler than draing the cars.




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

public class Exercise18_17 extends JApplet {

    public void init()
    {
        Exercise18_171 d= new Exercise18_171();
        add(d);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Exercise18_17 applet = new Exercise18_17();
        frame.setTitle("Exercise18_17: Car Race");
        frame.add(applet);
        applet.init();
        frame.setSize(780,260);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class Car extends JPanel {
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        int x[]={12,20,35,43};
        int y[]={11, 0, 0, 11};
        g.setColor(Color.BLACK);
        g.fillPolygon(x,y,x.length);
        g.setColor(Color.GRAY);
        g.fillRect(3,11,50,11);
        g.setColor(Color.BLACK);
        g.fillOval(6,20,10,10);
        g.fillOval(38,20,10,10);
    }
}
class Exercise18_171 extends JPanel {

    Car c1= new Car();
    Car c2= new Car();
    Car c3 = new Car();
    Car c4 = new Car();
    private int delay = 75;
    JTextField s1 = new JTextField();
    JTextField s2 = new JTextField();
    JTextField s3 = new JTextField();
    JTextField s4 = new JTextField();
    JLabel l1 = new JLabel("Car 1");
    JLabel l2 = new JLabel("Car 2");
    JLabel l3 = new JLabel("Car 3");
    JLabel l4 = new JLabel("Car 4");

    JButton b1 = new JButton("1");
     JButton b2 = new JButton("2");
     JButton b3 = new JButton("3");
     JButton b4 = new JButton("4");

    private Timer timer = new Timer(delay,new TimerListener());

        private int x1=650;
        private int x2=650;
        private int x3=650;
        private int x4=650;

        private int dx1=0;
        private int dx2=0;
        private int dx3=0;
        private int dx4=0;


    public Exercise18_171() {
        setLayout(null);
        l1.setBounds(0,0,65,30); add(l1);
        s1.setBounds(65,0,60,30);add(s1);
         b1.setBounds(125,0,60,30);add(b1);

        l2.setBounds(185,0,65,30);add(l2);
        s2.setBounds(250,0,60,30);add(s2);
         b2.setBounds(310,0,60,30);add(b2);

        l3.setBounds(370,0,65,30);add(l3);
        s3.setBounds(435,0,60,30);add(s3);
         b3.setBounds(495,0,60,30);add(b3);

        l4.setBounds(555,0,65,30);add(l4);
        s4.setBounds(620,0,60,30);add(s4);
         b4.setBounds(680,0,60,30);add(b4);

        c1.setBounds(700,65,50,30);add(c1);
        c2.setBounds(700,105,50,30);add(c2);
        c3.setBounds(700,145,50,30);add(c3);
        c4.setBounds(700,185,50,30);add(c4);

        b1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                if(s1.getText().equals("1")) {
                    setSpeed1(10);
                } else if(s1.getText().equals("2")) {
                    setSpeed1(5);
                } else if(s1.getText().equals("3")) {
                    setSpeed1(3);
                } else if(s1.getText().equals("4")){
                    setSpeed1(1);
                }
                revalidate();
                repaint();
            }
        });

        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                if(s2.getText().equals("1")){
                    setSpeed2(10);
                }else if(s2.getText().equals("2")){
                    setSpeed2(5);
                }else if(s2.getText().equals("3")){
                    setSpeed2(3);
                }else if(s2.getText().equals("4")){
                    setSpeed2(1);
                }
                revalidate();
                repaint();
            }
        });

        b3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                if(s3.getText().equals("1")){
                    setSpeed3(10);
                }else if(s3.getText().equals("2")) {
                    setSpeed3(5);
                } else if(s3.getText().equals("3")){
                    setSpeed3(3);
                }else if(s3.getText().equals("4")){
                setSpeed3(1);
                }
                revalidate();
                repaint();
            }
        });

    b4.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(s4.getText().equals("1")){
                setSpeed4(10);
            }else if(s4.getText().equals("2")){
                setSpeed4(5);
            }else if(s4.getText().equals("3")){
                setSpeed4(3);
            }else if(s4.getText().equals("4")){
                setSpeed4(1);
            }
            revalidate();
            repaint();
        }
    });

        timer.start();

    }

    public void setSpeed1(int a){ dx1=a;}
    public void setSpeed2(int a){ dx2=a;}
    public void setSpeed3(int a){ dx3=a;}
    public void setSpeed4(int a){ dx4=a;}

    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e){
            c1.setBounds(x1,75,50,30);
            c2.setBounds(x2,115,50,30);
            c3.setBounds(x3,155,50,30);
            c4.setBounds(x4,195,50,30);
            revalidate();
        }
    }

    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.drawLine(0,60,700,60);
        g.drawLine(0,105,700,105);
        g.drawLine(0,145,700,145);
        g.drawLine(0,185,700,185);

        x1=x1-dx1;
        x2=x2-dx2;
        x3=x3-dx3;
        x4=x4-dx4;
    }
}

Open in new window