This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mikescher
Mikescher wrote ...

2011/3/19

Loading a jpeg image from the jar-Ressource

Mikescher Mikescher

2011/3/19

#
Hello Community, I want to load an image from the ressource with this code: ----------------------------------------------------------------------- private void loadPreview(String Path) { try { InputStream is = this.getClass().getResourceAsStream("/level/levelPrev1.jpg"); if (is == null) { throw new IOException(); } BufferedImage temp = ImageIO.read(is); world_image = new GreenfootImage(64,64); BufferedImage wi_awt = world_image.getAwtImage(); wi_awt = temp; myGame.setImage(world_image); // nur anzeigen } catch (IOException e) { myGame.showMessage("Error while parsing Map - Couldn't find image \""+Path+"\"", "Error while parsing"); } } ----------------------------------------------------------------------- but unfortunately the resulting GreenfootImage is just fully transparent Hoping someone can help me ... Mike PS: Sorry for my bad English - Currently i'm learning this language :-D
PiRocks PiRocks

2011/3/19

#
Your code is pretty close, but you made one error: BufferedImage wi_awt = world_image.getAwtImage(); wi_awt = temp; Right here you change the variable 'wi_awt' to 'temp' instead of changing 'world_image.getAwtImage()' to 'temp'. You can't actually change 'world_image.getAwtImage()', but you can copy your image into it with the following code: BufferedImage wi_awt = world_image.getAwtImage(); Graphics g = wi_awt.getGraphics();//import java.awt.Graphics g.drawImage(temp,0,0,null);//copy image g.dispose();//release memory Of course, it would be easier to put your level folder into the images folder and replace the method with: private void loadPreview(String Path) { GreenfootImage world_image=new GreenfootImage("level/levelPrev1.jpg"); myGame.setImage(world_image); }
Mikescher Mikescher

2011/3/19

#
Thank you PI ROCKS - now it works :-D
You need to login to post a reply.