-1

I would like to write a program to take a displayed image, and have a transparent rectangle on top of the image at a fixed aspect ratio. I want to have this rectangle be able to be moved and resized to visually select a portion of the underlying image, and then when done, export the selected part of the image to a file.

How would I go about this?

1 Answers1

-1

Use an image cropping library.

Alternatively, if you're trying to do-it-yourself:

  • Create a java.awt.Canvas
  • Draw your image on the canvas
  • Create variables to track the location of the rectangle (x, y)
  • Draw a rectangle on the canvas where (x, y) say
  • Use Canvas.processMouseEvent() to track mouse clicks and mouse drags
  • Update the (x, y) variables when drag is detected
  • redraw the image and rectangle on the canvas when (x, y) change
  • compare (x, y) and the rectangle's dimensions to determine what part on the image is inside the rectange
  • use image library to crop image

Some Java image libraries:

Community
  • 1
  • 1
Jay
  • 9,314
  • 7
  • 33
  • 40