What exactly does getSubimage() of BufferedImage do?

ぃ、小莉子 提交于 2019-12-24 04:26:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!