Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Moving a component over an image  RSS feed

Bob Sale
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My application is an image editor. I display an image icon in a JLabel and would like to add a selection rectangle for cropping. I should be able to resize (by dragging and edge) and move the selection rect (by dragging in the interior area) with the mouse, and when I repaint, the perimeter of the prior selection box should be cleared to reveal the image that it was covering. Any hints on how to achieve this or examples that you can refer me to? Any help would be appreciated.
Rob Camick
Ranch Hand
Posts: 2752
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a JLabel with a LineBorder. You would then need to set the bounds of the label and add it to your image label.

Check out Resizing Components and Moving Windows to meet your other requirements.
Piet Souris
Posts: 1782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Bob,

just implemented a Photocropper couple of months ago myself. I thought it would be easy, but I found it a
very tough going. But then, I used a method that turned out to be quite complicated.

When I had finished and the program running, I thought of a much easier method. But I was fed up with this
program, so I did not try to implement it. But you could, so here goes:

First, I had a photo loaded in a BufferedImage, and I had a JPanel with just the size of this photo.
(Well, actually, I used a second BufferedImage, that contained a scaled down version of the photo, such that
it would just fit on my screen. Photo's nowadays tend to be rather big)

Now, having this JPanel, use a null LayoutManager., draw your JLabel at position 0.0 and add aMouseAdapter.
Then, when you detect dragging going on, use another JLabel, transparant but with a Border, with a size
that is indicated by the starting point of the muoise (i.e. when receiving a MousePressed event), and the current
drag position (from the MouseDragged event), and do a repaint of your panel (or maybe, just of this JLabel).

When you have this JLabel, and you get another MuosePressed event, check whether the position is in or
out of the second JLabel, to decide whether you must drag the complete JLabel, or the borders, or start a new Jlabel.

What I do not know is:

maybe you can let Java do the dragging of a Component, or its borders by setting some property. I would certainly
look at the Java API for this, since if such a thing exists, that would save a lot of frustrating coding!
But no doubt some Swing experts here around can tell you this straight away.


Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!