In a previous thread i had described a problem i had to transfer a frame or panel and all it's
components as an image into a buffered image. I thought i had found the solution but it seems i am doing something wrong. In line 162 i call the method Bimage(lines 87 - 92) to do this job.But what i get is just the background of the panel and not what is drawn on it.I used try { ImageIO.write(bimage, "jpg", new File("C:/bimagepaint.jpg")); } catch (IOException e){ } to find out what this method produces. Any suggestions?

package game;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class Newgame2 extends JFrame implements MouseListener,MouseMotionListener,MouseWheelListener , Runnable{

int mouseX=0, mouseY=0;
int prevX = 0, prevY = 0;
Graphics g = null ;
Graphics2D g2 = (Graphics2D)g;
int height ;
int width ;
ArrayList sugardrop;
Color sugar = new Color(255, 0 , 255);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
BufferedImage bimage;
JLabel label = new JLabel("");


public Newgame2() {
super("SUGAR GAME");
setSize(500 , 500);
height = getContentPane().getHeight();
width = getContentPane().getWidth();
//super.setLocation((int)((dim.getWidth()/2)-225),(int)((dim.getHeight()/2)-225));
setLocation(0,0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(Color.BLACK);
sugardrop = new ArrayList();
label.setFont(new Font("Courier New", Font.BOLD, 20));
label.setForeground(Color.ORANGE);
label.setText("Sugar , Drop");
getContentPane().add(label , BorderLayout.NORTH);
setVisible(true);
getContentPane(). setVisible(true);
}

final Runnable paintagain = new Runnable() {
        @Override
     public void run() {
         paint();

     }
 };
@Override
public void run()
{ 
try {
while(true){ 
                try {
                    SwingUtilities.invokeAndWait(paintagain);
                } catch (InvocationTargetException ex) {
                    Logger.getLogger(Newgame2.class.getName()).log(Level.SEVERE, null, ex);
                }
Thread.sleep(1000);}
} catch (InterruptedException ex) {
Logger.getLogger(Newgame.class.getName()).log(Level.SEVERE, null, ex); 
Logger.getLogger(Newgame.class.getName()).log(Level.SEVERE, null, ex);
}
}



public void init() throws AWTException
{
this.getContentPane().setBackground(Color.BLACK);

addMouseListener(this);
addMouseMotionListener(this);
addMouseWheelListener(this);
}


public BufferedImage Bimage(Component component) {
BufferedImage image = new BufferedImage(component.getSize().width , component.getSize().height , BufferedImage.TYPE_INT_RGB);
Graphics ig = image.getGraphics();
component.paint(ig);
return image;
}


@Override
public void mousePressed(MouseEvent me)
{

g2.setColor(Color.blue);
mouseX=me.getX();
mouseY=me.getY();
prevX = mouseX;
prevY = mouseY;
g2.drawLine(prevX, prevY, mouseX, mouseY); // Δημιουργώ την τελεία όταν πατήσει κάποιος το πλήκτρο
}

@Override
public void mouseClicked(MouseEvent me)
{

}

@Override
public void mouseDragged(MouseEvent me)
{

mouseX=me.getX();
mouseY=me.getY();
g2.setColor(Color.blue);
g2.drawLine(prevX, prevY, mouseX, mouseY);
prevX = mouseX; // Δημιουργώ την γραμμή όταν σύρω το ποντίκι
prevY = mouseY;
}

@Override
public void mouseReleased(MouseEvent me)
{

}

@Override
public void mouseWheelMoved(MouseWheelEvent mwe)
{

}
@Override
public void mouseEntered(MouseEvent me)
{

}

@Override
public void mouseExited(MouseEvent me)
{

}
@Override
public void mouseMoved(MouseEvent me)
{

}

public synchronized void paint() {
g2 = (Graphics2D)getContentPane().getGraphics();
try{
g2.setColor(Color.RED);
g2.drawLine(204,360,254,360);
g2.drawLine(204,300,204,360);
g2.drawLine(254,300,254,360);
int counter2 = 0;
Color color ;
bimage = Bimage(this.getContentPane());
sugardrop.add(new Generator(200,200, 5)); // Draw sugardrop
for (int i = 0; i < sugardrop.size(); i++) {Generator gen = (Generator)sugardrop.get(i);gen.update();}

for (int y = getContentPane().getSize().height - 1; y >= 0; y--) {
for (int x = getContentPane().getSize().width - 1; x >= 0; x--) {
int rgb = bimage.getRGB(x, y);
int red = (rgb & 0x00ff0000) >> 16;
int green = (rgb & 0x0000ff00) >> 8;
int blue = rgb & 0x000000ff;
// and the Java Color is ...
color = new Color(red,green,blue);
if (y < height -1 ) {
if (color == Color.WHITE) {

System.out.println(" color = white");
//-----------------------------------DECIDE HOW TO MOVE DOWN------------------------------
switch (downposition(x, y)) { 
case 0:// CANNOT MOVE DOWN MUST GO LEFT OR RIGHT
System.out.println(" No movement DOWN available, then try special cases sideways ");
if ( (x>203) && (x<255) && (y > 319) && (y < 381))
{counter2++; if (counter2 > 99){ setBackground(Color.BLACK); }} 
//-----------------------------MOVE RIGHT OR LEFT------------------------------------ 
switch (theseisplai(x, y)) { 

case 2:// METHOD theseisplai MEANS CAN MOVE RIGHT 
System.out.println(" movement right available ");
if ( (x>203) && (x<255) && (y > 319) && (y < 381)){counter2++; if (counter2 > 99){setBackground(Color.BLACK);}}
movepixel(x, y, x + 1, y);
break;

case 3:// METHOD theseisplai MEANS CAN MOVE LEFT
if ( (x>203) && (x<255) && (y > 319) && (y < 381)){counter2++; if (counter2 > 99){setBackground(Color.BLACK);}} 
movepixel(x, y, x - 1, y);
break;
default:
// No room for pixel to move in either direction
break;
} //END OF theseisplai(x, y)
break;

//-----------------------------MOVE DOWN----------------------------------------------- 
case 1://METHOD downposition  MEANS CAN MOVE DOWN 
movepixel(x, y, x, y + 1);
break;
case 2://METHOD downposition  MEANS CAN MOVE RIGHT

movepixel(x, y, x + 1, y + 1);
break;
case 3://METHOD downposition  MEANS CAN MOVE LEFT
// try {ImageIO.write(bimage, "jpg", new File("C:/bimagecase3.jpg"));}catch (IOException e){}
movepixel(x, y, x - 1, y + 1);
break;
}
}//

}
}
}

}
catch (AWTException ex) {
Logger.getLogger(Newgame.class.getName()).log(Level.SEVERE, null, ex);
}
}

int theseisplai(int x, int y) throws AWTException {
//System.out.println(" THESIPLAI");
//BufferedImage bimage = (BufferedImage)getContentPane().createImage(getContentPane().getSize().width, getContentPane().getSize().height);
if (x>= width-2 || x <= 2) {
// We can't perform this calculation, since the pixel is right on the edge of the screen.
return 0;
}
int rgb = bimage.getRGB(x+1, y);
int red = (rgb & 0x00ff0000) >> 16;
int green = (rgb & 0x0000ff00) >> 8;
int blue = rgb & 0x000000ff;
Color colorxkai1y = new Color(red,green,blue);

rgb = bimage.getRGB(x+1, y-1);
red = (rgb & 0x00ff0000) >> 16;
green = (rgb & 0x0000ff00) >> 8;
blue = rgb & 0x000000ff;
Color colorxkai1yplin1 = new Color(red,green,blue);

rgb = bimage.getRGB(x-1, y);
red = (rgb & 0x00ff0000) >> 16;
green = (rgb & 0x0000ff00) >> 8;
blue = rgb & 0x000000ff;
Color colorxplin1y = new Color(red,green,blue);

rgb = bimage.getRGB(x-1, y-1);
red = (rgb & 0x00ff0000) >> 16;
green = (rgb & 0x0000ff00) >> 8;
blue = rgb & 0x000000ff;
Color colorxplin1yplin1 = new Color(red,green,blue);

if ( 

colorxkai1y == Color.BLACK && // blank right 
colorxkai1yplin1 == Color.BLACK // blank top right

) {
// CAN MOVE RIGHT
return 2;
}

if (
colorxplin1y == Color.BLACK && // blank left
colorxplin1yplin1 == Color.BLACK // blank top left

) {
// CAN MOVE LEFT
return 3;
}

return 0;
} 
int downposition(int x, int y )throws AWTException {
// CHECKS IF A PARTICLE CAN DROP
boolean one = false;
boolean two = false;
boolean three = false;


int rgb = bimage.getRGB(x, y+1);//down
int red = (rgb & 0x00ff0000) >> 16;
int green = (rgb & 0x0000ff00) >> 8;
int blue = rgb & 0x000000ff;
Color colorxykai1 = new Color(red,green,blue);

rgb = bimage.getRGB(x+1, y+1);//right down
red = (rgb & 0x00ff0000) >> 16;
green = (rgb & 0x0000ff00) >> 8;
blue = rgb & 0x000000ff;
Color colorxkai1ykai1 = new Color(red,green,blue);

rgb = bimage.getRGB(x-1, y+1);//left down
red = (rgb & 0x00ff0000) >> 16;
green = (rgb & 0x0000ff00) >> 8;
blue = rgb & 0x000000ff;
Color colorxplin1ykai1 = new Color(red,green,blue);

if ( colorxykai1 == Color.BLACK) {
one = true;
}
if (x < (this.getContentPane().getSize().width - 1) && colorxkai1ykai1 == Color.BLACK) {
// The pixel down to the right is available
two = true;
}
if (x > 0 && colorxplin1ykai1 == Color.BLACK) {
// The pixel down to the left is available
three = true;
}

if (one && two && three) {
int r = (int) (1 + (Math.random() * (51 - 2)) );

if (r > 3) {
// go down
return 1;
}
else {
// go diagonal
return r;
}
}

if (!one && two && three) { // The immediate down pixel is NOT available
int r = (int) (2 + (Math.random() * (3 - 1)) );
return r;
}

if (one && !two && three) {//The pixel down to the right is NOT available
int r = (int) (1 + (Math.random() * (3 - 1)) );

if (r == 2) {
return 3;
}
else {
return 1;
}
}

if (one && two && !three) { // The pixel down to the left is NOT available
int r = (int) (1 + (Math.random() * (3 - 1)) );
if (r == 2) {
return 2;
}
else {
return 1;
}
}

if (one && !two && !three) {//The pixel down to the right is NOT available - The pixel down to the left is NOT available
return 1;
}

if (!one && two && !three) {// The immediate down pixel is NOT available - The pixel down to the left is NOT available
return 2;
}

if (!one && !two && three) {// The immediate down pixel is NOT available - The pixel down to the right is NOT available
return 3;
}

if (!one && !two && !three) {//No down pixel available
return 0;
}

return 0;
}

void movepixel(int sourcex, int sourcey, int targetx, int targety) {
SetPixel(sourcex, sourcey, new Color(255 , 255 , 255));//white
SetPixel(targetx, targety, new Color(0 , 0 , 0));}//black

void SetPixel(int x, int y, Color col) {
g2.setColor(col);
g2.drawLine(prevX, prevY, mouseX, mouseY); 
}

class Generator {
// A Generator generates sugar
int x, y, w;

Generator (int newX, int newY, int newW) {
x = newX;
y = newY;
w = newW;
}

public void update() {
Color col;
col = sugar; 
g2.setColor(col);
g2.setPaintMode();
for (int i = 1; i <= w; i++) {
int r = (int)(1 + (Math.random() * (4 - 1)) );//Min + (Math.random() * (Max - Min))
if (r == 1) {
g2.drawLine(x+i, y , x+i , y);
}

}
}
}
public static void main(String[] args) throws AWTException {
Newgame2 game = new Newgame2();
game.init();
game.run();
}
}

Please repost the code with proper formatting and indentation. Unformatted code is hard to read and understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.