Button Listener to stop and reverse an object (GUI)


I have created two classes called ControlPanel and Bicycle Panel. They both run using a Japplet(GUI). The Bicycle Class creates a Bike one blue and one red. The control panel sets the display and creates the buttons. The buttons are there for the user to tell the bike to move, reverse, and stop. I display works correctly but I have trouble with the moving,stopping and reversing. When the program is first executed the bike starts moving automatically. However, my buttons dont work when I tell it to stop or reverse. I need some help on what I can change on my code to make it work or what am I missing. There is also a slider to change speed of bikes.

ControlPanel -

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

 public class ControlPanel extends JPanel
//1, blueBikeDelay;

//The constructor creates 6 buttons, 2 sliders, and 2 bicycle panels
//and organize them using layouts.
public ControlPanel(int width, int height)
    this.width = width;
    this.height = height;

    JLabel = new JLabel("Red Delay");
    JSlider = new JSlider(JSlider.VERTICAL);

    setPreferredSize(new Dimension(width,height));

private class ButtonListener implements ActionListener
    public void actionPerformed(ActionEvent event)
} //end of ButtonListener

private class SliderListener implements ChangeListener

The main problem (with your buttons) is the fact they have been declared twice. Once as instance variables (which you are using in the actionPerformed method) and again within the constructor.

These means that the buttons you are trying to reference in the actionPerformed method are not the same as those on the screen (and in fact are likely to be null)

Remove the second declaration in the constructor.

The (seemingly) second problem, of the bikes moving when you begin (not sure if this is a problem or not), is because they have a non-zero delta (or step). If you don't want them to start moving when the program starts, you could simply set the step to 0 to start with