Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Applet implementing an external Mouse class

Posted on 2004-09-26
6
245 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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
couple of eclipse 5 45
Tomcat: Unable to run tomcat service. 2 23
Android development question 2 51
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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

790 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