Solved

Applet implementing an external Mouse class

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 42
simple java question 3 55
java.io.BufferedReader   readLine()  //returns the entire file ? 4 73
mysql jsp example issue 32 32
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

776 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