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

2015/3/3

Proble with scaling world projects

fejfo fejfo

2015/3/3

#
I think it will be easier to explain what I want to acomplish if I show my code fists$
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class ScalingWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ScalingWorld extends World
{
    static int oWidth;
    static int oHeight;
    static int currentWidth;
    static int currentHeight;
    static boolean first = true;
    /**
     * Constructor for objects of class ScalingWorld.
     * 
     */
    public ScalingWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        if(first) {
            first = false;
            oWidth = getWidth();
            oHeight = getHeight();
            addObject(new Apple(),100,100); //for testing
        }
    }

    public void scale(int newWidth,int newHeight) {
        GreenfootImage background = getBackground();
        background.scale(newWidth,newHeight);
        List<Actor> actors = getObjects(null);
        double xChange = newWidth/oWidth;
        double yChange = newHeight/oHeight;
        for(Actor a:actors) {
            scaleActor(a,xChange,yChange);
        }
        ScalingWorld newWorld = new ScalingWorld();
        newWorld.setBackground(background);
        Greenfoot.setWorld(newWorld);
    }

    public void scaleActor(Actor actor, double xChange,double yChange) {
        GreenfootImage image = actor.getImage();
        int newX = (int) (image.getWidth()*xChange);
        int newY = (int) (image.getHeight()*yChange);
        if(newX <= 0) {
            newX = 1;
        }
        if(newY <= 0) {
            newY = 1;
        }
        image.scale(newX,newY);
        actor.setImage(image);
    }

    public void scaleActor(Actor actor, int xPX,int yPX,boolean scaleInPixels) {
        if(scaleInPixels) {
            GreenfootImage image = actor.getImage();
            image.scale(xPX,yPX);
            actor.setImage(image);
        }else {
            scaleActor(actor,xPX,yPX);
        }
    }
}
I basicly want to be able to do theScalingWorld.scale(new width,new lenght); and that the world automaticly changes to the new size as wel as all the actors I might haven't come to the re-adding jet (i think I will have to calc the new Co-oords). the problem is that It doesn't actually changes the size of the world only the backround image feel free to use this buggy code!
fejfo fejfo

2015/3/3

#
I mean't problem in the title
danpost danpost

2015/3/3

#
As long as you create your worlds with 'new ScalingWorld()', which uses 'super(600, 400, 1);', your world will never change size. First thing that might help is making the dimensions variable -- that is, use 'super(oWidth, oHeight, 1);'. However, you will find multiple other issues due to your way of going about it. I would suggest a static int field to hold the zoom percentage. Its value should probably be limited to between values of around 25 to 40 for a minimum and 125 to 150 for a maximum. That would have your world (with (600, 400) at 100 percent) range between (150, 100) and (900, 600), which seems reasonable. Then, you would have 'super(600*zoom/100, 400*zoom/100, 1);' to create the worlds and you can also use the value of 'zoom/100' to set the size of the images. Do not enclose 'zoom/100' within parentheses when multiplying out a new size. A note on scaling images: you should typically only scale an image once. That is, in this instance, avoid scaling an image that has previously been scaled. Grab the original image using 'new GreenfootImage('filename.png")' and scale its size by the zoom factor. If you had to scale to background image initially to fit the (600, 400) size of the world, the following will allow you to zoom it properly:
GreenfootImage oBg = new GreenfootImage("background.png"); // image saved in file
oBg.scale (600, 400); // image originally scale to original world size
GreenfootImage bg = new GreenfootImage(oBg); // this creates a new image (copy of image at 100 percent zoom factor)
bg.scale(600*zoom/100, 400*zoom/100); // this sizes the image to the zoom factor
setBackground(bg);
fejfo fejfo

2015/3/4

#
danpost wrote...
A note on scaling images: you should typically only scale an image once. That is, in this instance, avoid scaling an image that has previously been scaled. Grab the original image using 'new GreenfootImage('filename.png")' and scale its size by the zoom factor. If you had to scale to background image initially to fit the (600, 400) size of the world, the following will allow you to zoom it properly:
GreenfootImage oBg = new GreenfootImage("background.png"); // image saved in file
oBg.scale (600, 400); // image originally scale to original world size
GreenfootImage bg = new GreenfootImage(oBg); // this creates a new image (copy of image at 100 percent zoom factor)
bg.scale(600*zoom/100, 400*zoom/100); // this sizes the image to the zoom factor
setBackground(bg);
I dont agree with this I want to beable to use this as a suport class for all my projects so I dont wanna change the filename of ervery Actor or world every time what may be possible is storing the baseImage of every Actor or world in a list
Super_Hippo Super_Hippo

2015/3/4

#
For the actors, you could save the original file in a field too and access this image instead of the current image of the actor.
fejfo fejfo

2015/3/5

#
how do I sava the it I wan't it to world for eath actor in everery greenfoot so I can't use a variable in the actor ?
Super_Hippo Super_Hippo

2015/3/6

#
Sorry, but I didn't understand what you wanted to say with your last post. Please repeat and try to use proper punctuation/spelling and make clear what you want to say. Thanks.
danpost danpost

2015/3/6

#
There is too much that needs dealt with in trying to change the size of a world. It might be easier to do something like what my PIP Actor class does. That is, to keep a reference to the standard-sized world in a static field (with dimensions of current-sized world) and have the background of the current world show a scaled image of what the standard-sized world would show. The only problem I have found with this idea is that mouse functions will not work in the portrayed world (except for actions on 'null').
danpost danpost

2015/3/7

#
@fejfo, how is this project coming along?
danpost danpost

2015/3/7

#
danpost wrote...
I believe I need to create my own 'MouseInfo' type class to mimic the one provided by greenfoot. It would adjust the 'getX' and 'getY' return values and set what 'getActor' returns to what was intended
I did just that and it seems to be good. Only thing left is mouse dragging methods. EDIT: I was wondering why the ending quote tag was not there -- must have clicked on edit instead of quote (initial post had a quote -- so initial quote tag was there; but not for the reason I thought; also, did not notice until after I had made some deletions to the text, so I was not sure if I accidentally deleted the ending tag myself). So, anyway, the rest of my initial post is gone now. It said that I had a working world scaler. Only non-functional issue is mouse drag events. It requires a small amount of modifications to the classes of your projects (single word changes throughout and slight code change in initial world class.
danpost danpost

2015/3/7

#
I uploaded a demonstration of what I came up with. It is here.
fejfo fejfo

2015/3/8

#
I never really continu working on a project for a long time I get lots of new ideas an have about 30 unfinsihed projects
You need to login to post a reply.