Solved

Applet implementing an external Mouse class

Posted on 2004-09-26
6
242 Views
Last Modified: 2013-11-23
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
Comment
Question by:djspinnet
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 40 total points
ID: 12153961
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
 
LVL 1

Author Comment

by:djspinnet
ID: 12154874
what do you mean by inner class? how do i do it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12156337
public class Outer {
    class Inner {}
}
0
 
LVL 92

Assisted Solution

by:objects
objects earned 40 total points
ID: 12156793
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now