Question

I'm working on a 'drawing' application. The entire 'drawing' is constantly stored in a BufferedImage object (using a constant update loop). Let's call it drawingArea.

I'm implementing a selection tool, using the BufferedImage class' getSubimage() method. The user uses this tool to select an area of the drawing, and paste it anywhere on the drawing.

The user selects an area by dragging the mouse, and when he/she releases the mouse, the selected area is stored in a BufferedImage. This is done by calling drawingArea's getSubimage( /* properties of the selected rectangular area */ ) method, and putting the returned value inside that BufferedImage (let's call it selectedArea).

This operation is done only once (aka, once there's a value inside selectedArea, there isn't any code that assigns anything new to selectedArea).

However, I found out (and am certain of it), that the value stored in selectedArea does change - the image it stores gets updated!

Meaning: If the user selects an area (at this point an image which is a copy of this area is saved), then draws something inside that area, and then pastes the picture - the picture pasted would be the one after the drawing, and not before it.

How can this be? Does getSubimage() return a BufferedImage that gets updated when it's origin is updated? (aka the area that it was copied from)? This seems silly but at this point it seems to me like the only option.

Was it helpful?

Solution

When in doubt, check the documentation:

Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.

In other words, yes, modification in either the original or the sub image is seen in the other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top