• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Selecting portion of a JPanel

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
You get good luck from rubbing the belly of a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic