views:

73

answers:

2

Hi

Currently working in Java, i'd like to be able to select part of an image using the mouse pointer co ordinates. The area selected then needs to be cut from the existing image and used to create a new separate image.

Just like a few pointers on how to go about it. Thanks.

+2  A: 

If you just want a usable tool or to see how it's done in Java, try ImageJ. If you want to write your own tool, have a look at Working with Images and How to Write a Mouse Listener. Once you have a BufferedImage and the desired coordinates, drawImage() will do most of what you want.

trashgod
thanks for a positive answer :-)
Uncle
+3  A: 

If you want the user to be able to "click-and-drag" to select a rectangle you need to implement a MouseMotionListener. Have a look at the mouseDragged method:

void mouseDragged(MouseEvent e)
          Invoked when a mouse button is pressed on a component and then dragged.

When you need to get hold of the sub-image, you simply use

public BufferedImage getSubimage(int x, int y, int w, int h)
          Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.

If you want to save the resulting image to disk, I suggest you have a look at Saving a Generated Graphic to a PNG or JPEG File.

aioobe
+1 for `getSubimage()`.
trashgod
also really helpful. getSubImage is particularly useful. thanks.
Uncle