java gui changing picture causes heapspace error
Posted
by pie154
on Stack Overflow
See other posts from Stack Overflow
or by pie154
Published on 2009-08-04T14:53:23Z
Indexed on
2010/04/10
11:03 UTC
Read the original article
Hit count: 293
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);
}
© Stack Overflow or respective owner