Your post seems a bit vague in it’s wording. This is a bit of code to sift through and I’m away from an IDE at the moment.
Could you point me towards where you’re actually trying to do pixel manipulation? Depending on what you’re looking to do your answer can vary.
If you’re looking to access the exact value of a pixel, getting the Raster object is very useful (WritableRaster if you’re looking to actually change their values). For BufferedImages, that’s really easy, just use it’s getRaster() method, and it’ll return it’s WritableRaster.
However, dealing with BufferStrategies is different. They purposefully hide their inner workings because they perform very complex operations managing VolatileImages, keeping track of each buffer, clearing unused images, and handling optimized page-flipping or blit-buffering algorithms. For those reasons, it tries to disallow you to edit values inside of it’s system. Because of this, there is really only a backdoor way of getting the BufferStrategy’s raster, and that’s by working through the Graphics2D class.
When performing any drawing operation, the Graphics2D class creates/gets a source and destination raster, and runs them through any of it’s Composite classes to blend them together via CompositeContext.compose(). If you create your own Composite and CompositeContext subclasses, you can effectively gain legitimate access to the current BufferStrategy Raster object being used. It’s hacky, but it works.
The structure I’m using below is based on the source for AlphaComposite.
Here’s an example you can use:
For the Composite subclass
import java.awt.Composite;
import java.awt.CompositeContext;
class RasterComposite extends Composite{
//static version kept to reduce needless instantiation
//Composite objects really just exist to store the blending rule and other things needed to blend
//otherwise, Composites should be immutable
public static final RasterComposite Normal = new RasterComposite();
//bit of a singleton pattern here, but can be extended for multiple blending rules
public static final RasterComposite getInstance(){
return Normal;
}
private RasterComposite(){}
//this is the inherited method from Composite, that links Composite and CompositeContext
//for our case it's not that important to consider color models or rendering hints
//but if youd like it's just a matter of passing that stuff via constructor
public CompositeContext createContext(ColorModel cm1, ColorModel cm2, RenderingHints hints){
return new CompositeContext();
}
}
For the CompositeContext subclass
import java.awt.CompositeContext;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
class RasterCompositeContext extends CompositeContext{
//this is where you should store the destination Raster
private WritableComposite theRaster;
//an inherited method which Graphics2D uses to blend colors
//i believe that by default Graphics2D uses AlphaComposite.SrcOver to render, so we can redirect all rendering to that
public void compose(Raster dstIn, Raster srcIn, WritableRaster dstOut){
theRaster = dstOut; //try this one, but if that doesn't work, try switching it for "theRaster = dstIn.createCompatibleWritableRaster();"
AlphaComposite.SrcOver.compose(dstIn,srcIn,dstOut); //this allows pixels to be rendered normally in the meantime.
}
//another inherited method. no objects to dispose of though
public void dispose(){}
//and here's how you get the raster from your new composite context
public final WritableRaster getRaster(){
return theRaster;
}
}
Viola, and that’s how you get the Raster object out of a BufferStrategy, in theory at least. But depending on what you’re trying to do, (like if you’re trying to implement additive blending while drawing) it may just be easier to create a Composite system that does everything as part of the drawing process.
Probably another, more inefficient way of getting the pixel values is to use the Robot class and get a screen capture of your screen. This is bad because if I recall this doesn’t work in fullscreen, requires access to the window’s position/size, needs additional scaling math if you’re scaling up (because scaling down causes loss of pixel info), and during the capture, your window must be undecorated.
Hopefully I’ve provided some sort of solution to your problem.