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

2012/9/12

BufferedImage to GreenfootImage

tylers tylers

2012/9/12

#
is there a way to get a BufferedImage to a GreenfootImage. if so can I have a code example. thank you :D
danpost danpost

2012/9/12

#
The only way I know, is to save the BufferedImage to a file of 'jpeg' or 'png' format, then create the GreenfootImage using the filename. Unfortunately, that will not work on the site. If there is a way without saving to a file, I would like to know, also!
tylers tylers

2012/9/12

#
it would be good if you could save an image on userdata
danpost danpost

2012/9/12

#
That would require a lot more memory on the server than they are willing to give up. Easier would be to add a GreenfootImage constructor to accept a java.awt.image.BufferedImage object in the parameter.
mjrb4 mjrb4

2012/9/12

#
is there a way to get a BufferedImage to a GreenfootImage.
Of course.

            BufferedImage bufImage = //Whatever
            
            GreenfootImage gImage = new GreenfootImage(bufImage.getWidth(), bufImage.getHeight());
            BufferedImage gBufImg = gImage.getAwtImage();
            Graphics2D graphics = (Graphics2D)gBufImg.getGraphics();
            graphics.drawImage(bufImage, null, 0, 0);
            
            setImage(gImage);
Simply create your new GreenfootImage with the same width / height as the BufferedImage, grab the backing image of your GreenfootImage then use the graphics of that image to draw the original BufferedImage onto your GreenfootImage. No temporary file saving / reflection / other crazy dark magic needed. :-)
it would be good if you could save an image on userdata
Highly unlikely I'm afraid - as well as potentially taking up a lot of space, it creates a whole new world of issues both technically and practically that just aren't worth it!
You need to login to post a reply.