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?
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.
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 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
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.