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

2019/3/19

How to instantiate a new world object

TNesler TNesler

2019/3/19

#
Hello! I am creating a program which places an object on the World grid but it does not instantiate on its own...:-( Usually, when I save, close and reopen the program it automatically instantiates the world grid, but not this time. What am I missing? Here is the code in myWorld object:
public MyWorld()
    {    
        super(800, 600, 1); 
        ColorPatch colorpatch = new ColorPatch();
        addObject(colorpatch, 200,200);
    }
And here is the construction method of my ColorPatch subclass:
    public ColorPatch()
{
    setImage (new GreenfootImage(200,200));
}
Thanks!
danpost danpost

2019/3/19

#
TNesler wrote...
I am creating a program which places an object on the World grid but it does not instantiate on its own...:-( Usually, when I save, close and reopen the program it automatically instantiates the world grid, but not this time. What am I missing? << Codes Omitted >>
I think you may be confused. I have a feeling that both the MyWorld and the ColorPatch objects are both being created and that the ColorPatch object IS being placed into the world -- you just cannot see the transparent image it is given. Pass and watch your mouse moving around the world (while paused). You should see it change from an arrow to a hand icon when passing over the object (from near the center to about half way toward the top-left corner of the frame).
TNesler TNesler

2019/3/20

#
Unfortunately, I my world object is not showing either. In other words, there is no white grid and the Act, Pause and Reset buttons are grayed out. Where the grid should be is "Instantiate a new World object". Therefore I presume the program won't start.
danpost danpost

2019/3/20

#
You will probably need to show show all your codes starting with your world class(es). Provide entire class codes.
TNesler TNesler

2019/3/20

#
Fortunately there is not much code. I am building a framework to demo the GreenfootImage class. Here is my world code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        ColorPatch colorpatch = new ColorPatch();
        addObject(colorpatch, 200,200);
    }
}
And here is the ColotPatch class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ColorPatch here.
 
*/
public class ColorPatch extends Actor
{
    /**
     * Act - do whatever the ColorPatch wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public ColorPatch()
{
    setImage (new GreenfootImage(200,200));
}    
    
    public void act() 
    {
      if (Greenfoot.isKeyDown("space")) {
      move(5);
      GreenfootImage img = getImage();
      img.clear();
      img.setColor(randomColor());
      setImage(img);
    }
    
    if (Greenfoot.isKeyDown("right")) {
        turn(5);
    }
    
    if (Greenfoot.isKeyDown("left")){
        turn(-5);
    }
    
    }    

private Color randomColor()
{
    int r = Greenfoot.getRandomNumber(256);
    int g = Greenfoot.getRandomNumber(256);
    int b = Greenfoot.getRandomNumber(256);
    int alpha = Greenfoot.getRandomNumber(256);
    return new Color(r,g,b,alpha);
}
}
Thanks for your patience!
Super_Hippo Super_Hippo

2019/3/20

#
Right click on the world subclass and create a new MyWorld.
danpost danpost

2019/3/20

#
Try creating a new scenario and copy/pasting codes into it from here.
TNesler TNesler

2019/3/22

#
Thanks for the suggestions. Creating a new program and copying the code has fixed the problem...:-) danpost mentioned that I would have trouble seeing my actor and he was right!...:-) To correct this problem in my construction method I added code to set the ColorPatch color to black.
    public ColorPatch()
{
    setImage (new GreenfootImage(200,200));
    GreenfootImage img = getImage();
    img.setColor(Greenfoot.Color(BLACK));
}
However, the compiler doesn't like my code. I know the setColor method requires a color name. What am I missing? Thanks again for your patience!
danpost danpost

2019/3/22

#
The color black is referenced by:
Color black = Color.BLACK;
It is part of greenfoot's Color class, not its Greenfoot class. A new GreenfootImage object initially has black set as its drawing color. To simplify, you can replace lines 4 and 5 with:
getImage().fill();
TNesler TNesler

2019/3/22

#
Thanks! Now I can see the dependencies a little better! However, when I run my program, I do not see my Colorpatch object on the background...:-( Do I need to use a different constructor method? One with a border? Thanks!
danpost danpost

2019/3/22

#
TNesler wrote...
when I run my program, I do not see my Colorpatch object on the background...:-(
Ahh -- you must have a black background. Okay, set the color to something else:
getImage().setColor(Color.GRAY);
getImage().fill();
TNesler TNesler

2019/3/23

#
Interesting! Changing it to gray fixed the problem. However I am not creating any background in the myWorld constructor. The background square I see is white. I assumed white was the default background. Can you explain why my program thinks the background is black? Looking at the world documentation, there is no setBackground method that uses a color. Thanks for your help with this...:-)
danpost danpost

2019/3/23

#
TNesler wrote...
Interesting! Changing it to gray fixed the problem. However I am not creating any background in the myWorld constructor. The background square I see is white. I assumed white was the default background. Can you explain why my program thinks the background is black? Looking at the world documentation, there is no setBackground method that uses a color.
Do you have an image set as the default image for objects of the MyWorld class?
TNesler TNesler

2019/3/25

#
Do you have an image set as the default image for objects of the MyWorld class?
No, I am working with a blank world image. That's why I was surprised that the default color would be black when the background on screen was white.
danpost danpost

2019/3/25

#
TNesler wrote...
I am working with a blank world image. That's why I was surprised that the default color would be black when the background on screen was white.
Well, the initial default background image should be white and its drawing color should be set to white. It is when you create a new GreenfootImage object giving width and height where its default drawing color is black.
You need to login to post a reply.