views:

170

answers:

5

Hi! I'm working for the first time with images in a JFrame, and I have some problems. I succeeded in putting an image on my JFrame, and now i want after 2 seconds to remove my image from the JFrame. But after 2 seconds, the image does not disappear, unless I resize the frame or i minimize and after that maximize the frame. Help me if you can. Thanks.

Here is the code:

        File f = new File("2.jpg");

System.out.println("Picture " + f.getAbsolutePath()); BufferedImage image = ImageIO.read(f); MyBufferedImage img = new MyBufferedImage(image); img.resize(400, 300); img.setSize(400, 300); img.setLocation(50, 50); getContentPane().add(img);

this.setSize(600, 400); this.setLocationRelativeTo(null); this.setVisible(true);

Thread.sleep(2000); System.out.println("2 seconds over");

getContentPane().remove(img);

Here is the MyBufferedImage class:

public class MyBufferedImage extends JComponent{
 private BufferedImage image;

private int nPaint;
private int avgTime;

private long previousSecondsTime;

public MyBufferedImage(BufferedImage b) {
    super();

    this.image = b;

    this.nPaint = 0;
    this.avgTime = 0;

    this.previousSecondsTime = System.currentTimeMillis();
}

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    g2D.setColor(Color.BLACK);
    g2D.fillRect(0, 0, this.getWidth(), this.getHeight());

    long currentTimeA = System.currentTimeMillis();


    //g2D.drawImage(this.image, 320, 0, 0, 240, 0, 0, 640, 480, null);
    g2D.drawImage(image, 0,0, null);
    long currentTimeB = System.currentTimeMillis();
    this.avgTime += currentTimeB - currentTimeA;
    this.nPaint++;

    if (currentTimeB - this.previousSecondsTime > 1000) {
        System.out.format("Drawn FPS: %d\n", nPaint++);
        System.out.format("Average time of drawings in the last sec.: %.1f ms\n", (double) this.avgTime / this.nPaint++);
        this.previousSecondsTime = currentTimeB;
        this.avgTime = 0;
        this.nPaint = 0;
    }
}

}

A: 

Have you tried calling

getContentPane().revalidate() ;

after the call to remove?

tim_yates
There is no revalidate method, try using getContentPane().validate();Or possibly (on the JFrame) do this.validateTree();
Dave G
Doh! Also, they want to watch out for adding and removing things off of the EDT
tim_yates
A: 

You probably need to invalidate the frame component forcing a redraw.

Probably your best bet is to look at the update/repaint methods.

Dave G
A: 

You should ensure that you are removing your image from your component in the Event Dispatch Thread. Give this a try:

 SwingUtilities.invokeLater(new Runnable() {
         public void run() {
              getContentPane().remove(img);
         }
 }

Your img will either need to be a global or declared final in local scope for this to work. Take a look at concepts on Swing Threads if you are not already familiar.

Note: The remove call on Container will call invalidate() if the content pane is considered valid.

akf
+1  A: 

just call this.repaint() after removing the image and all will be well ;)

smeg4brains
A: 

Give SwingUtilities.updateComponentTreeUI(this); a Shot

ring bearer
Thank you very much, now it works. It works with SwingUtilities.updateComponentTreeUI(this), this.repaint or this.update(getGraphics()). Have a nice day!