There isn't such a listener. But you could extend BufferedImage
and add the APIs you need.
Changelistener for BufferedImage
-
03-04-2022 - |
Domanda
Is there a way to track changes in the pixel data of a BufferdImage? I was thinking about something like that:
PixelChangeListener listener = new PixelChangeListener() {
public void pixelchange(Event e) {
// Get the coordinates in the Image that have been changed:
int x=e.getX();
int y=e.getY();
}
};
buffimage.addPixelChangeListener(listener);
If i now use g.drawLine
or buffimage.setRGB(x,y,rgb);
pixelchange(e)
should fire.
Is there a way to do that? :)
Soluzione
Altri suggerimenti
This is the solution from the OP (which was originally posted right in the question).
Not the BufferedImage itself but the WritableRaster behind it had to be modified. You can use
sun.awt.image.SunWritableRaster
for that, just use the notifychanged()
method to track pixelchanges.
To get a proper BufferedImage with the Raster I used this code:
ColorModel colormodel=ColorModel.getRGBdefault();
WritableRaster temp=colormodel.createCompatibleWritableRaster(400,400);
SunWritableRaster raster=new SunWritableRaster(temp.getSampleModel(),temp.getDataBuffer(),new Point(0,0));
BufferedImage img=new BufferedImage(colormodel,raster,true,null);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow