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

2020/11/1

Need help creating a grid of squares

cxcpio cxcpio

2020/11/1

#
1
2
3
4
5
6
7
8
String size = JOptionPane.showInputDialog("How big would you like your grid? ");
        for (double x=300-((Integer.parseInt(size)/2)*30);x<300+((Integer.parseInt(size)/2)*30);x=x+30)
        {
            for (int y=10;y<y+Integer.parseInt(size)*30;y=y+30)
            {
                addObject(new Square(), (int)x, (int)y);
            }
        }
This is what I have so far. The user will input a number and a grid is created with those dimensions. For example if the user enters 3, I want a grid of 3x3 to be created from the object "Square". Since the width of my canvas is 600, I attempted to center the grid with the first for loop. When I try to compile my code, my greenfoot gets stuck on "the world is being constructed". What am I doing wrong? Thank you Edit: Forgot to mention the dimensions of the square is 30x30
danpost danpost

2020/11/1

#
Line 1, I do not think can be in your world constructor. Try putting the code given in an overriding started method.
cxcpio cxcpio

2020/11/2

#
danpost wrote...
Line 1, I do not think can be in your world constructor. Try putting the code given in an overriding started method.
I am not sure where exactly to put line 1. This is my entire code so far, could you help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import greenfoot.*;
import javax.swing.JOptionPane;
public class MyWorld extends World
{
    public MyWorld()
    {   
        super(600, 400, 1);
        grid();
    }
    public void grid()
    {
        String size = JOptionPane.showInputDialog("How big would you like your grid? ");
        for (double x=300-((Integer.parseInt(size)/2)*30);x<300+((Integer.parseInt(size)/2)*30);x=x+30)
        {
            for (int y=10;y<y+Integer.parseInt(size)*30;y=y+30)
            {
                addObject(new Square(), (int)x, (int)y);
            }
        }
    }
}
danpost danpost

2020/11/2

#
cxcpio wrote...
I am not sure where exactly to put line 1. This is my entire code so far, could you help me?
danpost wrote...
Try putting the code given in an overriding started method.
cxcpio cxcpio

2020/11/2

#
I am not sure what you mean by overriding started method.. I have tried searching online but have just gotten more confused. :(
danpost danpost

2020/11/2

#
cxcpio wrote...
I am not sure what you mean by overriding started method.. I have tried searching online but have just gotten more confused. :(
The started method is defined in the World class as an empty method. It is like the act method in that it is called automatically by greenfoot. This method is called on the active world when the scenario goes from a paused state (or stopped) to a running state. Similar to the way the empty act method defined in World can be overriden (by re--defining one in your World subclass), the empty started method can be overriden.
cxcpio cxcpio

2020/11/3

#
Alright I finally managed to make it work thank you so much!
You need to login to post a reply.