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

2017/4/27

Creating a world with resized images

alittleteacher alittleteacher

2017/4/27

#
Hello, I am trying to resize an actor when the world is created, but I cannot make it happen. I have tried putting the resizing code in the constructor for the actor, but it will not work. Additionally, I cannot call a "resizing" method from the World superclass because one method is static and the other is not. Any help would be appreciated.
danpost danpost

2017/4/27

#
alittleteacher wrote...
I am trying to resize an actor when the world is created, but I cannot make it happen. I have tried putting the resizing code in the constructor for the actor, but it will not work.
Please show what you tried.
Additionally, I cannot call a "resizing" method from the World superclass because one method is static and the other is not.
More details, please. Would be nice to know what methods, exactly, you are referring to.
alittleteacher alittleteacher

2017/4/27

#
Here is a picture of the code for the World creation and that is where I would like for the resizing to happen.
public WombatWorld() 
    {
        super(10, 10, 60);        
        setBackground("cell.jpg");
        setPaintOrder(Wombat.class, Leaf.class);  // draw wombat on top of leaf
        populate();
        randomLeaves(20);
        randomPlums(2);
    }

    /**
     * Populate the world with a fixed scenario of wombats and leaves.
     */    
    public void populate()
    {
        addObject(new Wombat(), 7, 1);
        addObject(new Wombat(), 6, 6);
        addObject(new Wombat(), 1, 7);
        
    }
    
    /**
     * Place a number of leaves into the world at random places.
     * The number of leaves can be specified.
     */
    public void randomLeaves(int howMany)
    {
        for (int i=0; i<howMany; i++) {
            Leaf leaf = new Leaf();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(leaf, x, y);
        }
    }
alittleteacher alittleteacher

2017/4/27

#
Here is the code to shrink the plum (and the plum generates just like the leaves in the previous code).
public class Plum extends Actor
{
    /**
     * Act - do whatever the Plum wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void Plum() 
    {
        int percentage = 10;
        GreenfootImage image = getImage();
        image.scale(image.getWidth()*percentage/100, image.getHeight()*percentage/100);
    }    
}
Xmin_Terminator Xmin_Terminator

2017/4/27

#
Easy, but this is done in the actors code, it will resize the image to what you want wherever it is placed.
public Plum()
    {
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/2;
        int myNewWidth = (int)myImage.getWidth()/2;
        myImage.scale(myNewWidth, myNewHeight);
    }
Put this before your public act() the /2 divides the image size by 2, if you want to divide it my say 10 you put /10 instead. say you want like 60% of the images size, then do *3/5 (which is multiply by 3 divide by 5), which is 60%.
danpost danpost

2017/4/27

#
You can scale the image to size before the world is created; however, you would need to use static content and it will happen during compilation of the project (before any World object is created):
/** in Plum class */
// static content
private static GreenfootImage image = new GreenfootImage("plum.png"); // adjust filename to match the file in your 'images' folder
static {
    image.scale(image.getWidth()/10, image.getHeight()/10);
}

// the constructor
public Plum() {
    setImage(image);
}
You need to login to post a reply.