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

2020/11/13

Can Super contain user input

cxcpio cxcpio

2020/11/13

#
I am trying to make it so that the canvas in my world corresponds to what the user input is. I have tried putting the method before the "Super" but it says that "Super" needs to be first line so I don't know how to get around it. Thank you in advance
public void started()
    {
         size = Integer.parseInt(JOptionPane.showInputDialog("How big would you like your grid?"));
    }   
    public MyWorld()
    {    
        super(600, 600, 1);
        started();
        
(I would like the "size" variable to determine the size of my canvas)
danpost danpost

2020/11/13

#
What you have given looks okay. You will now need to create the world of the size supplied by the user, provided that it is a valid size:
/** initial world */
public MyWorld()
{
    super(600, 600, 1);
}

/** gets user input for size of world */
// automatically called by greenfoot when 'Run' is clicked or executed
public void started()
{
    Integer size = Integer.parseInt(JOptionPane.showInputDialog("How big would you like your grid?"));
    if (size == null || size < 1) Greenfoot.stop(); else Greenfoot.setWorld(new MyWorld(size));
}

/** world of size supplied by user */
public MyWorld(int size)
{
    super(size, size, 1);
}
cxcpio cxcpio

2020/11/13

#
Works thanks!
You need to login to post a reply.