I have 3 applets like this one that have walkers. What I need help with is adding an action listener to the buttons. I have tried several times and keep getting this error. I did have oneWalkerButton.addActionListener(this);
in java.awt.Button cannot be applied to walking
What is supposed to happen is the person should be able to pick how many walkers they want. 1,2 or 3.
Can someone please explain how to do this? I am new to java and I have been trying to figure this out for myself for a couple of weeks with no luck.
Thank you for any guidance!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Walking extends Applet
{
private int count = 0;
private int sleep = 100;
private int startx = 45,oldx = 42;
public void start()
{
count = 0;
startx = 45;
}
public void update(Graphics gr)
{
paint(gr);
}
public void paint(Graphics gr)
{
if((count%2)==0)
{
gr.setColor(getBackground());
gr.drawOval(oldx - 15,30,45,45);
gr.drawLine(oldx,75,oldx,160);
gr.drawLine(oldx,160,oldx+5,160);
gr.drawLine(oldx+5,110,oldx+10, 155);
gr.drawLine(oldx+10,155,oldx+20, 155);
gr.drawLine(oldx,110,oldx+20,140);
gr.drawLine(oldx+20,140,oldx+30,140);
gr.setColor(getForeground());
gr.drawOval(startx - 15,30,45,45);
gr.drawLine(startx,75,startx,110);
gr.drawLine(startx,110,startx,160);
gr.drawLine(startx,160,startx+5,160);
gr.drawLine(startx+5,110,startx+10,155);
gr.drawLine(startx+ 10,155,startx + 20,155);
gr.drawLine(0, 165,300,165);
}
else
{
gr.setColor(getForeground());
gr.drawLine(startx,110,startx+20,140);
gr.drawLine(startx+20,140,startx+30,140);
gr.setColor(getBackground());
gr.drawLine(startx+5,110,startx+10, 155);
gr.drawLine(startx+10,155,startx+20, 155);
oldx = startx;
startx +=3;
}
++count;
if(startx>180 )
startx = 45;
try
{
Thread.sleep(sleep);
}
catch(InterruptedException e)
{
showStatus(e.toString());
}
repaint();
}
Button oneWalkerButton;
Button twoWalkersButton;
Button threeWalkersButton;
public void init()
{
setLayout(null);
oneWalkerButton = new Button("One walker");
twoWalkersButton = new Button ("Two walkers");
threeWalkersButton = new Button("Three walkers");
oneWalkerButton.setBounds(0,180,75,20);
add(oneWalkerButton);
twoWalkersButton.setBounds(0,210,75,20);
add(twoWalkersButton);
threeWalkersButton.setBounds(0,240,85,20);
add(threeWalkersButton);
}
public void actionPerformed(ActionEvent e)
{
}
}