Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

beginner mousepressed problem  RSS feed

 
Alice Liu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone! I have a homework problem and I can't seem to get it to work.

We're supposed to make a snowman melt when you click on it with the mouse, but my snowman refuses to melt. I know it's a problem with the mousePressed method in the MeltingSnowman class because it even refuses to print out the line, so it's not a fault with the toMeltMove method.

Anyway, here it goes:








The files imported are from the wheels class (and subclasses), which you can find http://cs.stuy.edu/common/index.py?root=apcs&basename=code&mod=code&dir=src/ml1x/platek/wheelsDocs.

Thanks for any help!
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would sit better on the Swing forum, so I shall move it.

What are you adding the MouseListener to? Your mousePressed method will only work if it is in something which implements the MouseListener interface (or extends the MouseAdapter class, which implements the same interface).
And you have to "add" the MouseListener to something, with its addMouseListener method.
 
Alice Liu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't learned MouseListener yet, so I guess I'll try it now...

Should I implement it to the MeltingSnowman class? I'm a bit confused. :/
 
Alice Liu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to add a MouseListener, but I'm really kind of lost...



I have absolutely no idea what to do with the addMouseListener... It keeps giving me an error:

cannot find symbol
symbol : method addMouseListener(MeltingSnowman)
location: class MeltingSnowman

so, that means that the method does not exist...?

I'm sure I'm not even doing this correctly. Any help would be appreciated.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually recommend implementing Listeners in their own classes, rather than the display. MouseListener does have a close association with the display, however.

Try implementing the Listener in its own class and call is something like "MeltingMouseListener." Give it a MeltingSnowman field, and pass the snowman object to its constructor. Then you can add the MouseListener to whichever Component you are displaying the snowman on.

myPanel.addMouseListener(new MeltingSnowmanListener(frosty));

You would have to implement 5 methods, or use a MouseAdapter which is an abstract class with all 5 methods implemented "empty" and override the method you need. Remember the @Override annotation if you use the Adapter.You shouldn't need all those parameters for the melting method; they ought all to be fields of the Snowman object already.
There is more about MouseListeners here.
 
J. Noah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if this is all the code or not, but it's unclear where the actual drawing takes place. You do set parameters for all the body parts, but you don't actually draw them on the screen. IE you have a bunch of ellipses, but you haven't drawn them. That aside, the way listeners work is as follows. A JFrame for example, can "listen" to mouse events. Such behavior isn't exactly automatic though as the frame doesn't know what to do with any events it would receive. As such, you need to add a MouseListener to the Frame. MouseListener is an interface which means that you need to create your own class which implements this interface. To simplify coding, you could extend a MouseAdapter instead of implementing a MouseListener as MouseAdapter has null ops for ease of typing. In the mousePressed code, you would tell your snowman to melt based on the mouse's coordinates. You should be able to google "how to use MouseListeners" to get more details.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!