Problem with addMouseListener

Hello guys

I am currently building a program for a school project. The program involves mouselistening that i recently handeled from the main class of the program. However, the mouse activity became too much work for the animation thread to handle along with the animation stuff it does. So i decided to move the mouse listening to a seperate class called MouseClass. I implemented the usual, mouselitener, mousemotionlistener and so on, but i can’t call the method addMouseListener and addMouseMotionListener. I looked around in the API docs for this method and i found that it belonged to the Object class. I extended, imported, hell even made an Object object, but i strill can’t add the mouse listeners. You guys are good programmers and im in need of assistance since i need to have a demo ready of this in 4 weeks. Could you guys help me out? =)

Here is the source for MouseClass, i appologize if it is a bit messy, it’s heavily under construction.

package yumeEngineVN;

import java.awt.event.*;
import java.awt.*;

/********************************************************************************************************************************************************************
 * MouseClass for YumeEngineVN. It's task is to recieve information from the mouse listeners in MainEngine, process that info and then modify values in GameLogic
 * @author Johan Uttermalm // Kurten
 * Last Edited 2011-04-01
 * Version 0.4.2
 ********************************************************************************************************************************************************************/

public class MouseClass implements Runnable, MouseListener, MouseMotionListener{
	
	public GameLogic gl;
	public MainEngine me;
	
	public MouseClass(GameLogic glog, MainEngine mainen){
		gl = glog;
		me = mainen;
	}

	
	public void run() {
		while(true){
			try{
				mouseInit();
				mouseMoved();
				Thread.sleep(20);
				mouseClick();
				Thread.sleep(20);
			}catch(InterruptedException e){}
		}
	}
	
	public void mouseInit(){
		addMouseListener(this); //Don't know what to do here! ;____;
		addMouseMotionListener(this); //Don't know what to do here! ;____;
	}
	
	/**************************************************
	 * Methods for performing mouse click logic checks
	 **************************************************/
	//Gets called when MainEngine detects mouse clicks
	public void mouseClick(){
		checkClickButtonf();
		checkClickButtonb();
	}
	
	public void checkClickButtonf(){
		//Checks for button click on buttonf
		if(gl.clickX < gl.buttonfX + 100 && gl.clickX > gl.buttonfX){
			if(gl.clickY < gl.buttonfY + 30 && gl.clickY > gl.buttonfY){
				gl.setSp1PosX(170);
				gl.setSp1PosY(79);
			}
		}
	}
	
	public void checkClickButtonb(){
		if(gl.clickX < gl.buttonbX + 100 && gl.clickX > gl.buttonbX){
			if(gl.clickY < gl.buttonbY + 30 && gl.clickY > gl.buttonbY){
				gl.setSp1PosX(170);
				gl.setSp1PosY(480);
			}
		}
	}
	
	//Gets called when MainEngine detects mouse movement
	public void mouseMoved(){
		checkButtonfHover();
		checkButtonbHover();
		checkButtonC1Hover();
		checkButtonC2Hover();
	}
	
	public void checkButtonfHover(){
		//checks for movement that hovers over buttonf
		if(gl.moveX < gl.buttonfX + 100 && gl.moveX > gl.buttonfX){
			if(gl.moveY < gl.buttonfY + 30 && gl.moveY > gl.buttonfY){
				if(gl.buttonfFrameCount < 5){
					try{
						while(gl.buttonfFrameCount < 5){
							gl.buttonfFrameCount++;
							Thread.sleep(5);
						}
					}catch(InterruptedException ex){}	
				}
			}else if(gl.buttonfFrameCount >=5){
				try{
					while(gl.buttonfFrameCount != 0){
						gl.buttonfFrameCount--;
						Thread.sleep(5);
					}
				}catch(InterruptedException e){}
			}
		}else if(gl.buttonfFrameCount >= 5){
			while(gl.buttonfFrameCount != 0){
				try{
					gl.buttonfFrameCount--;
					Thread.sleep(5);
				}catch(InterruptedException e){}
			}
		}
	}
	
	public void checkButtonbHover(){
		//Checks for movement that hovers over buttonb
		if(gl.moveX < gl.buttonbX + 100 && gl.moveX > gl.buttonbX){
			if(gl.moveY < gl.buttonbY + 30 && gl.moveY > gl.buttonbY){
				if(gl.buttonbFrameCount <= 5){
					try{
						while(gl.buttonbFrameCount < 5){
							gl.buttonbFrameCount++;
							Thread.sleep(5);
						}
					}catch(InterruptedException ex){}	
				}
			}else if(gl.buttonbFrameCount >=5){
				try{
					while(gl.buttonbFrameCount != 0){
						gl.buttonbFrameCount--;
						Thread.sleep(5);
					}
				}catch(InterruptedException e){}
			}
		}else if(gl.buttonbFrameCount >= 5){
			while(gl.buttonbFrameCount != 0){
				try{
					gl.buttonbFrameCount--;
					Thread.sleep(5);
				}catch(InterruptedException e){}
			}
		}
	}
	
	public void checkButtonC1Hover(){
		//Checks for movement that hovers over buttonC1
		if(gl.moveX < gl.buttonC1X + 200 && gl.moveX > gl.buttonC1X){
			if(gl.moveY < gl.buttonC1Y + 30 && gl.moveY > gl.buttonC1Y){
				try{
					while(gl.buttonC1FrameCount < 5){
						gl.buttonC1FrameCount++;
						Thread.sleep(5);
					}
				}catch(InterruptedException e){}
			}else if(gl.buttonC1FrameCount >= 5){
				try{
					while(gl.buttonC1FrameCount != 0){
						gl.buttonC1FrameCount--;
						Thread.sleep(5);
					}
				}catch(InterruptedException e){}
			}
		}else if(gl.buttonC1FrameCount >= 5){
			try{
				while(gl.buttonC1FrameCount != 0){
					gl.buttonC1FrameCount--;
					Thread.sleep(5);
				}
			}catch(InterruptedException e){}
		}
	}
	
	public void checkButtonC2Hover(){
		//checks for mouse hovering over buttonC2
		if(gl.moveX < gl.buttonC2X + 200 && gl.moveX > gl.buttonC2X){
			if(gl.moveY < gl.buttonC2Y + 30 && gl.moveY > gl.buttonC2Y){
				try{
					while(gl.buttonC2FrameCount < 5){
						gl.buttonC2FrameCount++;
						Thread.sleep(5);
					}
				}catch(InterruptedException e){}
			}else if(gl.buttonC2FrameCount >= 5){
				try{
					while(gl.buttonC2FrameCount != 0){
						gl.buttonC2FrameCount--;
						Thread.sleep(5);
					}
				}catch(InterruptedException e){}
			}
		}else if(gl.buttonC2FrameCount >= 5){
			try{
				while(gl.buttonC2FrameCount != 0){
					gl.buttonC2FrameCount--;
					Thread.sleep(5);
				}
			}catch(InterruptedException e){}
		}
	}
	
	//Mouse event variable mutators
	public void setClX(int cl){ gl.clickX = cl; }
	public void setClY(int cl){ gl.clickY = cl; }
	public void setMvX(int mv){ gl.moveX = mv; }
	public void setMvY(int mv){ gl.moveY = mv; }
	public void setEnX(int en){ gl.enterX = en; }
	public void setEnY(int en){ gl.enterY = en; }
	public void setExX(int ex){ gl.exitX = ex; }
	public void setExY(int ex){ gl.exitY = ex; }


	
	public void mouseDragged(MouseEvent arg0) {}

	public void mouseMoved(MouseEvent e){}

	public void mouseClicked(MouseEvent e) {}

	public void mouseEntered(MouseEvent arg0) {}

	public void mouseExited(MouseEvent arg0) {}

	public void mousePressed(MouseEvent arg0) {}

	public void mouseReleased(MouseEvent arg0{}
}

//Kurten

Replace:


    public void mouseInit(){
        addMouseListener(this); //Don't know what to do here! ;____;
        addMouseMotionListener(this); //Don't know what to do here! ;____;
    }

With:


    public void mouseInit(Component target){
        target.addMouseListener(this);
        target.addMouseMotionListener(this); 
    }

I think you have to read up on how interfaces actually work. addMouseListener() is not something magic in which you always pass in this and which is always invoked from the this reference. The method addMouseListener() is defined in Component, so you need a reference to a Component and call addMouseListener() with an instance that has a class that implements the MouseListener interface.

Thank you Riven, i will try this =)

Also, i admit, i am very new to the whole mouse listening concept and i have not used interfaces to a great extent. So i’ll take your advice and study them some more :slight_smile:

Im sorry, but I still can’t seem to grasp how I am supposed to use this. Im building the program in applet form so when i place addMouseListener(this) in the class that serves as the main class it works, but not when i place those methods in a class that doesn’t extend Applet. I managed to make it compile now though, but it doesn’t registers any mousemotion or mouseclicks.

From your applets init function you would add something like.


MouseClass mc = new MouseClass();
mc.mouseInit(this);

The keyword ‘this’ means the current instance of the class it is in.

Thanks Zoto, it fixed the problem with mousetracking and everything works now ^^ Thanks a lot guys, you guys own ^^