• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing Mouse Events

 
George Jacobs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since my previous post has gotten much action I figured I would ask a more broad question.

How do I create a GUI that can have 2 mouse events on different "panes" and keep track of these? Is this doable?

For example, I have a JFrame with a mouse event and a JPanel with mouse events. Do I need to make one a keyboard event and keep the other as a mouse event?

Thanks!
 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, that is not a problem just set them both to have the same mouseListener. then in your mouse handling code determine where the event came from. e.getsource(). or let the JFrame handle its and let the JPanel handle its. that might be the best way.
 
George Jacobs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:no, that is not a problem just set them both to have the same mouseListener. then in your mouse handling code determine where the event came from. e.getsource(). or let the JFrame handle its and let the JPanel handle its. that might be the best way.


Thanks Randall!

I have created a class to attempt to try this, but I am having trouble getting a box to draw.

Here is my code associated with what I have done.



I had to implement all abstract methods, but the goal is to draw a rectangle with the mouselistener in the jpanel. I open the program and I get nothing besides the "Draw a box by dragging the mouse" message. What am I doing wrong?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there's a bunch of things you have wrong, using old depecated methods, using paint() etc

here's a framework for you to play around with.

run it, click anywhere in the panel, drag the mouse sideways, then release it and check the console messages.

 
George Jacobs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:there's a bunch of things you have wrong, using old depecated methods, using paint() etc

here's a framework for you to play around with.

run it, click anywhere in the panel, drag the mouse sideways, then release it and check the console messages.



Michael, thank you so much for the help. I got the rectangle to draw, but I am not sure if it is the best way because of the repaint method. Are there any problems with repaint

 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
repaint is the way you do it. never call paintComponent() directly. here i have some code that will help you.

there are 3 other classes in this program but the code i posted should help.
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Don't use "==".

Use the equals() method.
 
George Jacobs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code. Haven't tried to implement yet, but will try soon.

My next question is how do I now calculate the coordinates of the rectangle after it is drawn. For example, I would like to know center x, y coordinates of rectangle, the four corner coordinates, and the width/height which could be derived by these listed.
 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rob is correct the equals method is better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic