I need some help understanding how I would need to structure my classes to obtain the following behavior.
I need to provide the ability for a user to select a portion of a JPanel such that the user should be able to see a box around the selecion area as he drags his mouse to select a portion of the JPanel. My final aim is to obtain the Rectangle coordinates for the selection, so that I can create an image out of the selected portion.
Now, from my little understanding, I think it would be necessary to subclass JPanel so that I can override paintComponent(Graphics) in order for the box to be displayed while dragging. Is this true? Ideally, wouldn't it be better if I could have a class A in which I create a JFrame, add a JPanel to it, and then do something like B b = new B(panel);
Now, the B class would really be the selection utility class, in which I do all the required initialization, look for mouse events and mouse motion events, and draw the box around the selected area of the panel.
Am I making sense? If you could please provide some basic code outline/structure on how to arrange my classes, that's be very helpful.
Joined: May 22, 2012
I have the same problem and i also need the solution of it desperately as i have to implement it in my java program.
I believe it must be possible as there are classes to do every piece of work in java. I just don't know how to do it.
Anyone out there please help.
Joined: Jun 09, 2003
mousePressed gives you your starting point
mouseDragged gives you the rectangle from the starting point to the drag point,
changing the rectangle co-ords as it goes, and calling panel.repaint() for the panel's
paintComponent() to draw the dashed horizontal/vertical lines (BasicStroke)