views:

185

answers:

2

I have a java programme than when a button is clicked it updates the image on screen to the according image. this will work for the first 15 or so clicks then it causes a java heapspace error. I think it is because of the way I am updating the jpanel that contains the bufferedimage but not sure what the reason is. My code to get make the JPanel contain the new image is,

public class extraScreenPanel {

static JPanel screenPanel = new JPanel(new BorderLayout()); 

public static JPanel extraScreenPanel(int dispNum) 
{
    JLabel label = new JLabel("" + dispNum + "");
    label.setPreferredSize(new Dimension(800, 600));
    //label.setUI( new VerticalLabelUI(true) );
    label.setVerticalAlignment( SwingConstants.TOP );
    screenPanel = imgDisp(dispNum);
    label.setForeground(Color.white);
 label.setFont(new Font("Serif", Font.BOLD, 200));
    screenPanel.add(label, BorderLayout.PAGE_END );

    return screenPanel;
}



public static JPanel imgDisp(int picNum) {  
   /* String url[] = new String[5000];
    String part1;
    url[0] = "C:/PiPhotoPic/pic16.jpg";
    for(Integer i=1;i<5000;i++){
        if(i<10){part1 = "C:/temp/new0000000";}
     else if(i<100){part1 = "C:/temp/new000000";}
     else if(i<1000){part1 = "C:/temp/new00000";}
     else {part1 = "C:/temp/new00000";}
     String num = Integer.toString(i);
        url[i]= part1 + num + ".jpg";
    }
    if(picNum<0){picNum=0;}
    String ref = url[picNum];*/ //this code is just to get specific ref for image location
    BufferedImage loadImg = loadImage(ref);  
    JImagePanel panel = new JImagePanel(loadImg, 0, 0);  
    panel.setPreferredSize(new Dimension(800, 600));
    return panel;
}  


public static class JImagePanel extends JPanel{  
    private BufferedImage image;  
    int x, y;  
   public JImagePanel(BufferedImage image, int x, int y) {  
        super();  
        this.image = image;  
        this.x = x;  
        this.y = y;  
    }  
    @Override  
    protected void paintComponent(Graphics g) {  
       super.paintComponent(g);  
        g.drawImage(image, x, y, null);  
   }  
}  


public static BufferedImage loadImage(String ref) {  
        BufferedImage bimg = null;  
        try {  

          bimg = javax.imageio.ImageIO.read(new File(ref));  
     } catch (Exception e) {  
         e.printStackTrace();  
     } 
     BufferedImage bimg2 = resize(bimg,800,600);
     return bimg2;  
 }  


 public static BufferedImage resize(BufferedImage img, int newW, int newH) {  
    int w = img.getWidth();  
    int h = img.getHeight();  
    BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());  
    Graphics2D g = dimg.createGraphics();  
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
    g.dispose();  
    return dimg;  
}

}

And the code that updates my gui is, it works by removing the panel from its containg panel and then readding it to it.

    picPanel = imgDisp.imgDisp(num);
    repaintPicPanel();

public static void repaintPicPanel()
    {
     picPanel.removeAll();
     menuPanel.remove(picPanel);;
     menuPanel.add(picPanel, BorderLayout.LINE_START);
    }
A: 

Attach with jvisualvm (in the Java 6 JDK) and see where your memory goes.

Thorbjørn Ravn Andersen
A: 

It's almost impossible to read your code but I would wager that since the images are tied into the JPanels and you never properly dispose of the panels or images they're hanging around and that causes your error. I would also try to do everything inline so that instead of removing the whole panel and replacing it with a new one, you just delete the image from the panel and replace that with a new one.

Brother Logic
how would I delete an image from a panel?
All you should need to do is add a setImage() method to the panel. As you reset the image the old resources should be released.
camickr
sorry but I have no idea how I would go about doing a setImage method? Any help or links to something like it would be really helpful. Thanks in advance.