Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Applet implementing an external Mouse class

I am doing an applet game, a Bug Swatting game. I implemented the MouseListener and MouseMotionListener in my main

public class BugSwat extends Applet implements MouseListener, MouseMotionListener, Runnable

and in my public void init() i call the addMouseListener(this); and my applet game works fine..
and since i have start() and stop() to pause the game,  my

public void mouseEntered(MouseEvent e){start();} calls the start function and
public void mouseExited(MouseEvent e){stop();} calls the stop/pause function

and the method that i call when a bug is swatted, to kill it, is in a separate class called class Bugs.

first i declare Bugs[] aBug = new Bugs[10];

in public void mouseClicked(MouseEvent e)
    {
          aBug[0].killBug();
    } // for now, to ease explanation, i'm killing just one bug.

my program works beautifully at this point.

Now, my problem is... my lecturer wants me to move all my MouseMotionListener and MouseListener methods in a new class, so i created a

public class Mouse extends MouseAdapter()
// and implemented the necessary methods in it, they do nothing as of now.

and in my init() now, i call
addMouseListener(new Mouse());

my problem now, is... how do i call the
1) mouseExited(MouseEvent e) in public class Mouse to call the stop(); method in my main BugSwat class to pause the game
2) mouseClicked(MouseEvent e) to call the aBug[i].killBug(); from the Bugs class.
3) how do i implement MouseMotionListener to move my cursor in the main game

i need help.. i really don't know why it doesn't work when i put it in a separate class
0
djspinnet
Asked:
djspinnet
  • 2
2 Solutions
 
CEHJCommented:
It will be easier if you make the class Mouse an inner class

1. & 2. (let the Mouse class be accessed via the variable 'mouse'):

You can just call

mouse.mouseExited(null);

and

mouse.mouseClicked(null);

(assuming you don't need the event)

3. There is no difference here to how you already implement your MouseListener methods
0
 
djspinnetAuthor Commented:
what do you mean by inner class? how do i do it?
0
 
CEHJCommented:
public class Outer {
    class Inner {}
}
0
 
objectsCommented:
Pass the required objects to your classes ctor which stores them for later use:

public class Mouse extends MouseAdapter
   implements MouseMotionListener
{
   private Bug[] bugs;

   Mouse(Bug[] bugs)
   {
      this.bugs = bugs;
   }

...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now