Handling Mouse Events for a generic class...
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   j_s
Posted On:   Tuesday, May 3, 2005 02:13 AM

Would anyone have help on mouse events for a simple generic class? The program I have is an applet which draws out a grid of squares - say 20 by 20 squares. The squares are a certain random color. Each of the squares is an instance of this "ColorSquare" class that I have defined in a separate .java file (not embedded/inline) and I am instantiating them in "main". So these squares are NOT currently Rectangles or whatever... should they be Shapes? Areas? *shudder* Custom-Components? etc. with x, y coords and sizes. These squares right now are just points and sizes. I'm guessing these ColorSquare objects need to be something more 'physical' for these ColorSquare objects to handle    More>>


Would anyone have help on mouse events for a simple generic class?



The program I have is an applet which draws out a grid of squares - say 20 by 20 squares. The squares are a certain random color. Each of the squares is an instance of this "ColorSquare" class that I have defined in a separate .java file (not embedded/inline) and I am instantiating them in "main". So these squares are NOT currently Rectangles or whatever... should they be
Shapes? Areas? *shudder* Custom-Components? etc. with x, y coords and sizes.



These squares right now are just points and sizes. I'm guessing these ColorSquare objects need to be something more 'physical' for these ColorSquare objects to handle mouse events.



So, the problem is that my ColorSquare class cannot handle mouse events that are unique to each square, or each instantiation. I want EACH ColorSquare object to have its own mouse event handler and change color upon the mouse "mousing over" each square. These are not components and I do not intend to add them to any panes or containers or other complications. Do I need to do this?



I simply want to have the capability to mouse over these squares which are instantiated in Main, handle mouse events in the ColorSquare class and to handle double-buffered drawing with paint() in Main as well.



I'm currently using...





public class ColorSquare extends Applet implements MouseListener

{

public ColorSquare(int xx, int yy, int ww, int hh, int id)

{

x = xx;

y = yy;

w = ww;

h = hh;

sqID = id;

this.addMouseListener(this); //doesn't work btw

}

.

.

.




//mouse handler methods...

public void mouseClicked (MouseEvent me)

{

System.out.println("click " +sqID);

}

public void mouseEntered (MouseEvent me)

{

System.out.println("entered " +sqID);

}

public void mousePressed (MouseEvent me) {}

public void mouseReleased (MouseEvent me) {}

public void mouseExited (MouseEvent me) {}




NOTE:


I'm extending Applet here in an attempt to utilize the addMouseListener method.


----------------------------------------------------------





My Main looks like...



public class Main extends Applet

{


public void init()

{

//instantiate ColorSquare objects...

}



public void paint(Graphics g)

{


//draw offscreen image (which is ColorSquare objects)


}



public void update(Graphics g)

{

//paint(g);

}










Any suggestions???


Thanks a bunch!





JS.    <<Less
About | Sitemap | Contact