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)
      {
      }
   }

Recommended Answers

All 19 Replies

You need to add the red text below:

public class Walking extends Applet implements ActionListener

Thanks!! :icon_cheesygrin:

I need some help with this because I am not too sure about how to do this with 3 applets . The buttons are on the first applet. How do I go about writing the code to make the button bring up just one walker for button one, two walkers on button two and 3 walkers on button 3?

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


    public class Walking extends Applet implements ActionListener    		   
   {
      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);
         oneWalkerButton.addActionListener(this);
         
         twoWalkersButton.addActionListener(this);
         threeWalkersButton.addActionListener(this);
      
      
      } 
        
       public void actionPerformed(ActionEvent e)
      {
           
   }

I need some help with this because I am not too sure about how to do this with 3 applets . The buttons are on the first applet. How do I go about writing the code to make the button bring up just one walker for button one, two walkers on button two and 3 walkers on button 3?

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


    public class Walking extends Applet implements ActionListener    		   
   {
      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);
         oneWalkerButton.addActionListener(this);
         
         twoWalkersButton.addActionListener(this);
         threeWalkersButton.addActionListener(this);
      
      
      } 
        
       public void actionPerformed(ActionEvent e)
      {
           
   }

You are missing an ending bracket on your actionPerformed function. Are you sure you want three separate Applets or do you want three walkers on one applet? The latter seems easier. If the latter, I'd have a variable called numWalkers which belongs to the Walking class and that will vary from one to three. When a button is clicked, in actionPerformed , numWalkers ' value is changed to reflect the button that was clicked. paint checks the value of numWalkers and draws that many walkers.

Well....lol
THe instructions are to create 3 walkers. The first called walking and then walker2 and walker3.
They all walk at varying speeds across the page.

I have everything working beautifully but, this silly book by Joyce Farrell doesn't explain applets very well. I have to add the buttons to bring up 1, 2 or 3 walkers. I am confused on the button thing as well. I know I had to add the action listener which I did, now I am stuck!

Well....lol
THe instructions are to create 3 walkers. The first called walking and then walker2 and walker3.
They all walk at varying speeds across the page.

I have everything working beautifully but, this silly book by Joyce Farrell doesn't explain applets very well. I have to add the buttons to bring up 1, 2 or 3 walkers. I am confused on the button thing as well. I know I had to add the action listener which I did, now I am stuck!

Well, go with one applet. I would imagine that is what they want. In actionPerformed, you need to figure out WHICH button was pressed. You can do that with the getSource () function. Once you figure out what button has been pressed, execute some code:

public void actionPerformed(ActionEvent e)
{
    if (e.getSource () == oneWalkerButton)
    {
        // code
    }
    else if (e.getSource () == twoWalkersButton)
    {
        // code
    }
    else if (e.getSource () == threeWalkersButton)
    {
        // code
    }
}

For the code part, you'll want to adjust that numWalkers variable, like I mentioned in the last post, then repaint. paint needs to check that variable and act accordingly. You may want to add a Walker class which keeps track of the coordinates of each walker and figures out what to draw, though that's not 100% necessary. You can do it in your Walking class.

The 3 walkers walk across the screen one behind the other at different speeds and then repeat. I will give your suggestion a whirl!

Thank you!

The 3 walkers walk across the screen one behind the other at different speeds and then repeat. I will give your suggestion a whirl!

Thank you!

You're welcome. Feel free to ask more questions if you are stuck/confused. There is more than one way to implement this.

Ok they want 3 applets after I reread it so how would I implement the getsource statements?

I put all my buttons on this applet. The other 2 applets are identical except for the names and the speed of the walkers.

Ok they want 3 applets after I reread it so how would I implement the getsource statements?

I put all my buttons on this applet. The other 2 applets are identical except for the names and the speed of the walkers.

This may be above my pay grade. I'm kind of new with applets. To control one applet from another applet would mean that the applets have to know about each other and communicate with each other. I don't know if that's possible or not. I've certainly never done it before. How are you creating the three applets? Do you have three applet tags hard coded into the HTML file? I know how to do this with three panels inside of one applet, but not with three applets because I've never tried to have an applet communicate with another applet, so I can't help. Hopefully someone else will come along who knows. I'll be interested in knowing myself. Good luck!

Yeah i have them as different classes in the html and it runs seamlessly. I just have to get these buttons to work with all 3.

A friend of mine said this :

create a constructor for the other two walkers in your primary walker class so that you will be able to call them inside your if/else if statement.

Trouble is I am not sure how to do that to get the other two applets to run when those buttons are pushed. The other applets are Walker2 and Walker3. The main applet is Walking.

Does anybody have any hints, suggestions on how to do this? I have looked and looked online and haven't found anything and the book is totally useless.

create a constructor for the other two walkers in your primary walker class so that you will be able to call them inside your if/else if statement.

He means references to them.

I undertand that but I am having trouble writing the code to call those walkers.

I have this but I am not sure what needs to be put in the code part to call walker2 and walker 3. The code for them is basically the same as I have posted.

public void actionPerformed(ActionEvent e)
      {
         if (e.getSource () ==oneWalkerButton);  
         {
            // code
    }
    else if (e.getSource () == twoWalkersButton)
    {
        // code
    }
    else if (e.getSource () == threeWalkersButton)
    {
        // code
            repaint();
         }
      }

Applets aren't my strong suit. My understanding is that an applet "owns" its own space and doesn't know about and can't control anything out of its little bubble. If zwench has three separate applets on three different parts of the web/html page, how can they know about each other and can they create each other? If I am an applet and I'm given a 300 x 300 section of a web page by the html tags, I cannot place any content outside of that 300 x 300 section, can I? Hence, since the other two applets will be presumably outside of that 300 x 300 section, they may as well be on Mars. I have no contact with them, right?

It sounds like zwench's friend is suggesting to have one applet be the main applet and create the other two applets. Those applets aren't created by the html code?

Again, full disclaimer, my understanding of applets in this regard may be completely incorrect and all of this may be perfectly possible.

On a different note, zwench, there's no law that says you have to compelte every exercise ina book, unless it's part of an assignment or something. If this is just a self teaching exercise guided by a book, you can always modify the assignment in any way you desire. If it was me, I'd modify it so you have one applet and you have three panels in the applet, rather than three applets. None of the issues I raised will be a factor.

This is an assignment. If the person clicks button one there should be one walker. Button two would bring up two walkers and button 3 would bring up all of them.

I didn't want to have to modify anything because I have it working perfectly with the 3 walkers and I am not that skilled to modify what I have to create one applet .

I spent over a month getting all 3 of them to work properly. The code for all 3 applets is the same except for the timing on the walkers and their names.

The main applet is walking, then there is walker2 and walker3 all seperate applets.
How if I chose the route you suggest with the one applet can I combine these 3?

Well i don't have to worry about this because my instructor said there were typos in the textbook and to just send what I have without the buttons.

Thanks for all the help. I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.