-
February 4th, 2009, 10:58 PM
#1
Have Graphics2D, need BufferedImage to get pixel data.
I'm having some difficulty creating a Java applet with a BufferedImage. What I want is for randomly-located and colored polygons to be rendered on the screen, and then I'll be able to get pixel color data in an int array via conversion to a BufferedImage. I try to do so, but the int[] of pixel data from the BufferedImage is all the same number, -16777216. Any help is greatly appreciated, as I've been banging my head against the desk for days with this problem.
Here's the standard initialization; nothing exciting:
public void init() {
setBackground(Color.white);
setForeground(Color.black);
} //end public void init
Here's some of the paint method:
public void paint(Graphics g) {
g2 = (Graphics2D) g; //needs to be in a Graphics2D context
//create a BufferedImage that is compatible with being displayed on screen:
GraphicsConfiguration config = g2.getDeviceConfiguration();
int transparency = Transparency.OPAQUE;
screenShot = config.createCompatibleImage(500, 500, transparency);
//gc will now be used to write into the screenShot bufferedimage
gc = (Graphics2D) screenShot.getGraphics();
//drawing for loop to create NUM_POLYS number of polygons
for (int i = 0; i < NUM_POLYS; i++){
Poly myPoly = new Poly ();
// draw GeneralPath (polygon)
int x1Points[] = {myPoly.X1, myPoly.X2, myPoly.X3, myPoly.X4, myPoly.X5};
int y1Points[] = {myPoly.Y1, myPoly.Y2, myPoly.Y3, myPoly.Y4, myPoly.Y5};
GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, x1Points.length);
polygon.moveTo(x1Points[0], y1Points[0]);
for (int index = 1; index < x1Points.length; index++) {
polygon.lineTo(x1Points[index], y1Points[index]);
};
polygon.closePath();
//here's where everything's actually drawn into BufferedImage, I presumed:
gc.setPaint(myPoly.filler);
gc.fill(polygon);
//here's where things are drawn on screen:
g2.setPaint(myPoly.filler);
g2.fill(polygon);
} //end drawing for loop
gc.drawRenderedImage(screenShot, null);
gc.dispose();
/////////////////////////////////////////////////////////////
//turn polygons into bufferedImage for use by pixel grabber
//broken?
//BufferedImage screenShot = new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
int w = screenShot.getWidth(null);
int h = screenShot.getHeight(null);
int[] pixelArray_genA = new int[w*h]; //array to hold pixel color data.
screenShot.getRGB(0, 0, w, h, pixelArray_genA, 0, w);
// print pixel data to screen:
//WHY is it all the same???? Everything's equal to -16777216!
int r = 0;
for (int q=0; q<1000; q++){
if (pixelArray_genA[q] != -16777216){
g.drawString("pixel " + q + ": " + pixelArray_genA[q], 5, 25 + (25*r));
r++;
}
}
So I know I'm messing something up in getting the data into the BufferedImage, I just don't know what that is. Any help is greatly appreciated. I've been reading tutorials and digging through the API, but to no avail.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|