This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
Thanks.
Paras Ahuja
Ranch Hand
Joined: May 22, 2012
Posts: 62
posted
0
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.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
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)
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.