Forums Register Login

Multiple mouselisteners

+Pie Number of slices to send: Send
Hi revered wizards,

I am writing a style exercice in preparation of a complex program I wish to write in which I'll be dragging around all sorts of objects
that will react differently depending on the area's they are dragged over and dropped in.

So, first I'm coding a simple Hanoi style game in which the tiles are to be dragged onto the tower areas. The towers are to change color when the
tile is dragged over them, and the tile itself has to change color when it is clicked upon.

Now, I'm a beginner OO programmer. So I'm trying to make each object react to mouse events in its own class. I don't know if this is the
correct way or whether it is possible at all, but it's not working in my code. Could someone have a look at it?

Here is the main class:So basically I'm making a screen with three towers, with five tiles on the first one.

The screen gets drawn at startup and draws the towers. A first mouselistener is added, but as a test all it does is type a message to the console whenever the screen is clicked on. This seems to be working.

Next comes the Tower class:I stripped this class to the bare necessities. It has its own mouselistener, which is supposed to send a message to the console whenever the tower area is clicked upon, but it doesn't work. And I can't figure out why.

Same problem in the Tile class:Same here, the message never gets sent. The local mouselistener doesn't seem to respond.

Any ideas what I'm doing wrong? Thanks a lot in advance.
+Pie Number of slices to send: Send
1) Why use AWT as your GUI library and not the newer, more robust and more flexible Swing GUI library?
2) I would use one MouseListener here and add it to the object that you are clicking and dragging -- the Tile objects. Within this MouseListener, I'd have it check to see if a Tile is over a Tower when you drag the Tile, and then tell that Tower (via a public method) to highlight itself.

but again, I'd strongly recommend that you do this all in Swing and not AWT.

Best of luck!
+Pie Number of slices to send: Send
I would also ask that the mods move this thread over to the Swing/AWT forum for better topic/forum alignment. Thanks!
+Pie Number of slices to send: Send
Yup.
Moving.
+Pie Number of slices to send: Send
Thanks for looking into it.

pete stein wrote:1) Why use AWT as your GUI library and not the newer, more robust and more flexible Swing GUI library?


I read somewhere that Swing was written on top of AWT and that it lost the generic layout in the bargain. At the end of the day I don't really care, as long as I get it to work. I'll check into Swing if you think it would be better.

pete stein wrote:2) I would use one MouseListener here and add it to the object that you are clicking and dragging -- the Tile objects. Within this MouseListener, I'd have it check to see if a Tile is over a Tower when you drag the Tile, and then tell that Tower (via a public method) to highlight itself.


Ok, but what if I have different types of tiles? Remember this is a preparation for a complex program. I am testing basic functionalities. In my Hanoi code, only the mouselistener on the screen responds. The one on the Tile class does not and neither does the Tower's.

Is there a way to write multiple listeners? Or is it simple not within scope of Java?
+Pie Number of slices to send: Send
 

Olaf Enulfson wrote:I read somewhere that Swing was written on top of AWT and that it lost the generic layout in the bargain. At the end of the day I don't really care, as long as I get it to work. I'll check into Swing if you think it would be better.


I'm sorry, but that doesn't really make sense to me as I've never heard of the "generic layout". I'm no pro, but as a "dedicated" amateur I feel strongly that Swing would be far better for your project. If any of the pros here feel differently, I encourage them to please let you and me know.

Ok, but what if I have different types of tiles? Remember this is a preparation for a complex program. I am testing basic functionalities. In my Hanoi code, only the mouselistener on the screen responds. The one on the Tile class does not and neither does the Tower's.
Is there a way to write multiple listeners? Or is it simple not within scope of Java?


Again, you don't need or want multiple mouse listeners here. One will do and would be much easier to manage by the way. Another way to write this program, by the way, is to not make your Towers and Tiles out of components (or JComponents with Swing), but to use Graphics2D Shape-derived objects, to do all the painting in the main JPanel's paintComponent method, and thus have one MouseListener (and MouseMotionListener, of course) attached to this main JPanel. There are several ways to skin this cat.
+Pie Number of slices to send: Send
 

pete stein wrote:(...) I feel strongly that Swing would be far better for your project.


Ok, I will then.

pete stein wrote:Again, you don't need or want multiple mouse listeners here. One will do and would be much easier to manage by the way.


For this Hanoi thingy, no doubt. But I'm thinking in terms of the bigger program I intent to write next. This is an exercice.

I don't want to sound too stubborn. I appreciate your comments and I do consider them. But I just don't see this happening. In the other program clicking on and dragging certain objects will cause objects in their neighborhood to change, depending on their types. Some will not change, some will change differently. Clicking next to a certain object will create another object, depending of that neighbor. Dragging selected objects of different kinds could react differently depending on the objects where they will be dumped next to. And so on and so on...

If I have to check all the conditions (which type of object am I, who am I next to, what states are they in, where am I being dumped next to) within the confines of a single mouselistener, I will go mad. I think it would be better to split up the checks within the different object types. For Hanoi it doesn't make sense. It's overly complex. But for my project I think it does. And I need to learn how to do it with an easier problem.

Now, if you tell me it can't be done, then fine. I'd have to cope with it. (I'm half mad already anyway. ;-)

pete stein wrote:Another way to write this program, by the way, is to not make your Towers and Tiles out of components (or JComponents with Swing), but to use Graphics2D Shape-derived objects, to do all the painting in the main JPanel's paintComponent method, and thus have one MouseListener (and MouseMotionListener, of course) attached to this main JPanel. There are several ways to skin this cat.


Yeah, but isn't that like saying Don't treat your objects like objects. It's all just one big canvas. ? I'm going for the Object Oriented approach here.

Maneesh Godbole wrote:Yup.
Moving.


Thanks for the new location. The view is much better here. ;-)
+Pie Number of slices to send: Send
Hey, I'm spending way too much time on this, LOL. Anyway, here's a demo of the concept I mentioned above. Note that this only shows one tile, does not contain code to place tiles on a tower, and contains no "logic" for the Towers of Hanoi puzzle, but nevertheless, I hope this helps you.

+Pie Number of slices to send: Send
 

If I have to check all the conditions (which type of object am I, who am I next to, what states are they in, where am I being dumped next to) within the confines of a single mouselistener



That is not the function of the MouseListener. You define and implement methods like "isDropAllowed(source, target)". Then this logic is implemented in the components not the MouseListener.

You should also probably be looking at Swing Drag and Drop which already supports this type of processing. Although I must admit I find it rather confusing and have never played with it. But you should at least read the section from the Swing tutorial on Drag and Drop which is designed for complex situations and then decide how you want to proceed.
+Pie Number of slices to send: Send
 

pete stein wrote:Hey, I'm spending way too much time on this, LOL.


Whoa, you wrote all that in less than an hour? It'll take me the better part of a week to analyse it.
Your program runs like a charm. And Swing seems to take care of the flickering I experienced with my code. Thank you.

pete stein wrote:I hope this helps you.


It's completely different from what I expected, but it'll defenitely get me on the way.
Just a small last question. To make a tower light up in another color by clicking it without dragging the tile on it, do I code that within mousePressed(mousevent e) ? I'm not asking for more code. I just need to understand the idea.

Rob Camick wrote:You should also probably be looking at Swing Drag and Drop which already supports this type of processing.


Thanks for the link. I'll check it out too.
+Pie Number of slices to send: Send
Please don't get angry, pete, but I tried anyway. I guess I am stubborn.
I added a mouseListener to the towers so that a clicked-on tower changes to blue, independently from the red tile's location.

Rob Camick wrote:You define and implement methods like "isDropAllowed(source, target)". Then this logic is implemented in the components not the MouseListener.


I intent to do that. I guess pete's setHighlight() and my setColor() kind of reflect that.
But the different kinds of mousePressed and mouseDragged are now defined for their own object.

Is this ok? Is there a logical problem with that? Is there something I'm not thinking of?
+Pie Number of slices to send: Send
 

Olaf Enulfson wrote:Please don't get angry, pete, but I tried anyway. I guess I am stubborn.
I added a mouseListener to the towers so that a clicked-on tower changes to blue, independently from the red tile's location.


LOL, I think I should only get mad if your code somehow blows up my computer. The fact is your experimenting here; you're playing with code to see what effect the changes will have, and I think this is wonderful. Your implementation of a second mouse listener appears sound to my non-professional eye.

Rob Camick wrote:You define and implement methods like "isDropAllowed(source, target)". Then this logic is implemented in the components not the MouseListener.


I intent to do that. I guess pete's setHighlight() and my setColor() kind of reflect that.
But the different kinds of mousePressed and mouseDragged are now defined for their own object.

Is this ok? Is there a logical problem with that? Is there something I'm not thinking of?


I don't see any problem with this. Drag & Drop is a whole different concept, one that I'm not very familiar with, but one that will likely work well for you (especially if Rob endorses it). There's a decent tutorial on this at the Sun Swing tutorials, and Rob has a link to this in his post.
+Pie Number of slices to send: Send
Well, thank you very much. I couldn't have done this without your help.
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3887 times.
Similar Threads
custom shaped gui components?
browsers won't let applets run
change color of an oval in a polygon
Multithreading: bouncing balls problem
Parallel dragging
Could anyone explain this code please?
problem with JDialog
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:41:36.