Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem with addMouseListener  (Read 933 times)
0 Members and 1 Guest are viewing this topic.
Offline Kurten

Junior Member


Medals: 3
Exp: 3 years


-w-


« Posted 2011-04-01 13:01:48 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-04-01 13:51:49 »

Replace:
1  
2  
3  
4  
    public void mouseInit(){
        addMouseListener(this); //Don't know what to do here! ;____;
       addMouseMotionListener(this); //Don't know what to do here! ;____;
   }


With:
1  
2  
3  
4  
    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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Kurten

Junior Member


Medals: 3
Exp: 3 years


-w-


« Reply #2 - Posted 2011-04-01 14:38:53 »

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 ^_^
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kurten

Junior Member


Medals: 3
Exp: 3 years


-w-


« Reply #3 - Posted 2011-04-03 11:22:15 »

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.
Offline zoto

Senior Member


Medals: 4



« Reply #4 - Posted 2011-04-03 11:47:08 »

From your applets init function you would add something like.
1  
2  
MouseClass mc = new MouseClass();
mc.mouseInit(this);


The keyword 'this' means the current instance of the class it is in.
Offline Kurten

Junior Member


Medals: 3
Exp: 3 years


-w-


« Reply #5 - Posted 2011-04-04 13:16:16 »

Thanks Zoto, it fixed the problem with mousetracking and everything works now ^_^ Thanks a lot guys, you guys own ^_^
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (40 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!