问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/22133993/what-exactly-does-getsubimage-of-bufferedimage-do