Solved

Applet implementing an external Mouse class

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

685 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