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

2012/12/3

exporting the world as image`?

erdelf erdelf

2012/12/3

#
Is there a way to export the world with actors and everything as an image?
danpost danpost

2012/12/3

#
Being you have this classified as 'Other', I would guess you do not need to do this programatically. You can use a snipping tool to capture the image to save a copy of it in a new image file; or, you can create a public method in the world class so you can pause the scenario, right-click on the world and select the method. The method should create a copy of the background image and for each object in the world draw its image at that object's location, minus half the width and height of the object's image. Then the method should get the buffered image of that image so you can write it to a new file. The list of actors must be sorted in the same order as your 'setPaintOrder' statement sets them in, and you must draw them in the same order that the program does.
erdelf erdelf

2012/12/3

#
the world is too big for my screen and I dont have much time today to program this. Could you please make this method for me?
danpost danpost

2012/12/3

#
Couldn't you capture parts of the image in seperate files and combine them with a graphics editor?
erdelf erdelf

2012/12/3

#
sure, I am doing this right now, but this takes much time on a 1980*1080 screen and a 3000*3000 world.
erdelf erdelf

2012/12/3

#
could anyone program a method for me or give me an another way to do this?
danpost danpost

2012/12/3

#
Will it be neccessary to sort the objects and draw them in paint order? or, are there no intersecting objects? If there are control objects you do not want in the final saved image, you can manually remove them from the world. If no intersecting objects, then the main idea in creating the GreenfootImage is:
1
2
3
4
5
6
GreenfootImage main = new GreenfootImage(getBackground());
for (Object obj : getObjects(null))
{
    Actor actor = (Actor) obj;
    main.drawImage(actor.getImage(), actor.getX()-actor.getImage().getWidth()/2, actor.getY()-actor.getImage().getHeight()/2);
}
To output the image, use the following (supplying 'pathname'):
1
2
3
4
5
6
7
8
9
10
try
{
    BufferedImage bi = main.getAwtImage();
    File outputfile = new File(pathname); // supply String pathname/filename (no suffix)
    ImageIO.write(bi, "png", outputfile);
}
catch (IOException e)
{
    System.out.println("File output failed");
}
You will need to add the following imports
1
2
3
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
Hope this helps.
danpost danpost

2012/12/3

#
If you have just a main actor that may be intersecting any objects, then add an 'if (actor !instanceof MainActorName.class)' to qualify not drawing it within the 'for' loop. After the loop, add code to draw its image.
erdelf erdelf

2012/12/3

#
@danpost, thx. Just a question, is it supposed to work with CellSize 1?
danpost danpost

2012/12/3

#
I guess I should have qualified that; the CellSize must be 1.
erdelf erdelf

2012/12/3

#
is there a way to change the code to cellSize 30?
danpost danpost

2012/12/3

#
Just tested, cell size does not change the results of the base background image. For the objects in your world, you only need to make the following substitutions:
  • getX() becomes getX()*getCellSize()+getCellSize()/2
  • getY() becomes getY()*getCellSize()+getCellSize()/2
erdelf erdelf

2012/12/3

#
thx for your help
You need to login to post a reply.