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

2015/3/29

How can I dynamically change the size of the world? (Only once, however)

decadence18 decadence18

2015/3/29

#
Basically, what I'm asking is, how can I multiply an integer (that may change between use) and a set number to create the size of the world? I'm sure this is possible, as I've seen it done somewhere before.
danpost danpost

2015/3/29

#
If you are wanting that the world be randomly created in one of two different sizes, then I could tell you how. If you want the user to specify 'large' or 'small', for example, at start-up, then that would be possible also and I could tell you how. You just need to explain a bit more as to what exactly you are trying to do. I mean, I now "Basically" what you are trying to do, but do not have any knowledge of the specifics that you may require ('between use' makes me think that you are asking about one of the two ways I mentioned at the beginning of this post).
decadence18 decadence18

2015/3/29

#
Let me see if I can explain this well enough that it'll make sense... As in my other questions, most recently the one you replied to about dynamically creating a set of images to be painted on the background, I'm attempting to use User Input to generate a world. Now, I have the array of values from the user; what I now have to do is expand the world dynamically to accommodate all of the images the user wants. Each image is approximately 2774 pixels in size; before I only required 3 images, so the world was a set size of 8332 pixels. Now that the user can request any amount of images, I need the world (super) to expand to accommodate each image. I attempted to simply do super(images*2774, 1000, 1); this didn't work as the variable was created AFTER the super class. Is there a way to accomplish this?
danpost danpost

2015/3/29

#
Okay. The way to accomplish this is by first creating a base world where the input is entered. Instead of directly drawing onto the background image of the world (since it may not be big enough to hold all images required by the user), draw onto a GreenfootImage object stored in a variable. Then, once all the data is given, you can create a new world that will be the final size of the image:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/** base world used to get user data defining the new world */
public MyWorld()
{
    super(1, 1, 1);
    GreenfootImage bg = null;
    int boardNum = 0;
    for (;;)
    {
        // etc. (getting 'image')
        if (bg == null) bg = image;
        else
        {
            GreenfootImage old = new GreenfootImage(bg);
            bg = new GreenfootImage(old.getWidth()+image.getWidth(), old.getHeight());
            bg.drawImage(old, 0, 0);
            bg.drawImage(image, old.getWidth(), 0);
        }
        boardNum++;
    }
    Greenfoot.setWorld(new MyWorld(bg));
}
 
/** construct a new user-defined world */
public MyWorld(GreenfootImage bg)
{
    super(bg.getWidth(), bg.getHeight(), 1);
    setBackground(bg);
    // etc.
decadence18 decadence18

2015/3/29

#
I get the basic principle of what you're saying, but I'm unsure of how to implement this in my current code. Here is the code of one of my final Background classes for when the boards are organized horizontally (meaning the Y height will stay the same, while the X will change in this world.):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class HorizontalBackground extends World
{
    private String boardPositions;
    /**
     *
     */
    public HorizontalBackground()
    {
        super(8332, 1000, 1);
         
        boardPositions = JOptionPane.showInputDialog("Enter the order of the boards, separated by a space.\nExample: 1 2 2 1");
        String[] boards = boardPositions.split(" ");
         
        if ((boardPositions != null) && (boardPositions.isEmpty() != true))
        {
            int x = 0;
            for(int i=0; i<boards.length; i++)
            {
                GreenfootImage image = new GreenfootImage("Horizontal Hexagon Board (33x10) " + boards[i] + ".png");
                getBackground().drawImage(image, x, 0); // draw image on background
                x += image.getWidth(); // set new x-coordinate for next image draw
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "One of the fields are empty, please try again!");
        }
    }
}
What would I need to do in my code in order to use what you have given me? If you need, I can post the code of my other classes as well.
danpost danpost

2015/3/29

#
There is no need to post other classes as none of them are ever referred to in this class. You will have to change what image is drawn on because the background image of the world may not be large enough for the number and size of the images chosen to use. Then you will need a second world constructor like I have in the post above.
decadence18 decadence18

2015/3/29

#
I see what you're saying, but my mind obviously has shut down for the night, as I have absolutely no idea how to begin using this code.
decadence18 decadence18

2015/3/29

#
@Danpost, I managed to do it using a globally-defined variable in a world previous to the one in which I needed the variable.
You need to login to post a reply.