Hey, so I’m working on a program and for debugging purposes, I’m trying to get the program to take a screenshot of a part of the display. I want to have the display updated, but I can’t seem to get it to work. I’m sure it’s a simple issue, but my experience with Java Applets is very small.
Here’s the part that I’m having issues with:
...
Thread.sleep(5000);
try {gb = new GameBoard(frame.getBounds());}
catch(Exception e){System.out.println("Error.");} // Make "gameboard" Object
while (true)
{
Thread.sleep(1000);
gb.grabImage(); // use java.awt.Robot's createScreenCapture()
ImageIcon icon = new ImageIcon(gb.image()); // wrap the image
JLabel label = new JLabel(icon, JLabel.CENTER);
frame.getContentPane().add(label,BorderLayout.EAST); //display the image (works)
//JOptionPane.showMessageDialog(null, label, "icon", -1);
label.repaint(); //update the display??
frame.repaint();
frame.getContentPane().repaint();
}
As I said, the image appears, and will create new ones if I change the Applet size, but I need a constantly changing image.
Thanks in advance!
You are creating and adding a new
JLabeleach time through the loop. Because you are changing the structure of the component tree you’ll need to callrevalidateon the frame’s content pane.A better solution would be to just change the image on a single JLabel. Create one label, add it, then in your loop use
JLabel.setIconandrepaint.