Re: java heap space on pictures
- From: Daniel <info at daik dot se>
- Date: Tue, 18 Apr 2006 09:18:16 +0200
As to why your images are not displaying properly. It seems you are
not actually loading them properly. Java loads Images asynchroniously,
this means that in your case you must wait for the images to load.
Lukily for you there is easy ways to do that. I show you with a simple
example.
MediaTracker mt= new MediaTracker(parentComponent);
if (zoom == 0){
wholeimage =
Toolkit.getDefaultToolkit().getImage("images\\map0.jpg");
mt.addImage(wholeimage,0);
try{
mt.waitForAll();
}catch(InterruptedException e){
// handle this exception
}
}
after this is executed and no exception took place, and only after
this, is the images ready to be displayed. I guess you shuold have a
look in the API to read a bit more about the MediaTracker.
regards
Daniel
ok im doing simple image showing, and the images are not more than 400
kb in size, and yes i have set my heapspace, -Xmx to 512M and still the
problem persists.
Here is my code.
if (zoom == 0){
wholeimage =
Toolkit.getDefaultToolkit().getImage("images\\map0.jpg");
}
else if (zoom == 1){
wholeimage =
Toolkit.getDefaultToolkit().getImage("images\\map1.jpg");
}
else if (zoom == 2){
wholeimage =
Toolkit.getDefaultToolkit().getImage("images\\map2.jpg");
}
else if (zoom == 3){
wholeimage =
Toolkit.getDefaultToolkit().getImage("images\\map3.jpg");
}
else if (zoom == 4){
wholeimage =
Toolkit.getDefaultToolkit().getImage("images\\map4.jpg");
}
thumbImage = new BufferedImage(4000, 4000,
BufferedImage.TYPE_INT_RGB);
g2d = thumbImage.createGraphics();
g2d.drawImage(wholeimage, 0, 0, 4000, 4000, map);
g2d.setColor(Color.GREEN);
thumbImage = thumbImage.getSubimage((int)(x -
((2000/(Math.pow(zoomfactor,
zoom)))/2)),(int)(y-((2000/(Math.pow(zoomfactor,
zoom)))/2)),(int)(2000/(Math.pow(zoomfactor,
zoom))),(int)(2000/(Math.pow(zoomfactor, zoom))));
imaa = new ImageIcon(thumbImage);
ima = imaa.getImage();
ima = ima.getScaledInstance(400, 400, 100);
imaa = new ImageIcon(ima);
imagemap = new JLabel(imaa);
imagemap.setBounds(10, 10, 400, 400);
map.add(imagemap);
map.repaint();
Now it has to be said that there are two more strange things. Firstly
sometimes the picture doesnt display and when i zoom in and out MAYBE
eventually instead of a black picture i get the image, secondly if i
load the images and display them and then instantly remove them again
they sometimes show too AND in order for my application to start
showing these pictures i have to press a button, now everytime i press
this button the same thing happens, BUT only if i press it three times
and let the application do the task a third time i can see the pictures
instead of a black picture... any ideas?
Oh the heapspace only happens when i zoom in and out, but im setting
the same object to be a differnet image so no new variables are
created...
So is there a more efficent way to do it?
Thanks
Hochi
.
- References:
- java heap space on pictures
- From: erik . hochweller
- java heap space on pictures
- Prev by Date: Re: JDialog and File Handling ! plz help
- Next by Date: Anyway to alter the default font size for ToolTips ?
- Previous by thread: Re: java heap space on pictures
- Next by thread: Re: java heap space on pictures
- Index(es):