• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Writing Mouse Events

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Don't use "==".

Use the equals() method.
 
George Jacobs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rob is correct the equals method is better.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic