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

2014/11/26

Trying to use drawLine in world subclass

CKnox CKnox

2014/11/26

#
So I'm trying to use the drawLine in my world subclass, called Ground. But when I compile it I get an error saying non-static method getBackground() cannot be referenced from a static context This is my code for Ground:
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
30
31
32
33
34
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Ground here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Ground extends World
{
    public static final int CELL_SIZE = 20;
    /**
     * Constructor for objects of class Ground.
     *
     */
    public Ground()
    {   
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(40,30,20,false );
        start();
        drawLine();
    }
    public void drawLine()
    {
        Ground.getBackground().drawLine(10,2,10,20);
    }
    public void start()
    {
        Food food = new Food();
        addObject(food, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        Worm worm = new Worm();
        addObject(worm, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
}
danpost danpost

2014/11/26

#
If you want to reference the Ground object in your statement, use the 'this' keyword, not the name of the Class (which is not an object of the class). The 'this' keyword is understood if left out of the statement altogether; so, you could use either of these:
1
2
3
this.getBackground().drawLine(10, 2, 10, 20);
// or just
getBackground().drawLine(10, 2, 10, 20);
The Ground world you created has a cellsize of 20; however, the image returned by using 'getBackground' is the entire image of the world with, in your case, a size of 800x600. Your line drawing coordinates and lengths must correspond to the image size, not your world size.
CKnox CKnox

2014/11/27

#
Thankyou it compiles now, however it isn't visible. Is there a way to set the color? 'image.setColor' doesn't work.
danpost danpost

2014/11/27

#
CKnox wrote...
'image.setColor' doesn't work.
What do you have the variable image referring to? It needs to refer to a GreenfootImage object or you can replace it with a method call that returns a reference to a GreenfootImage object:
1
getBackground().setColor(java.awt.Color.black);
uses the method call 'getBackground' which returns the GreenfootImage object you want to set the drawing color to. FYI, the default drawing color of the default World background image is white (maybe because after creating the transparent GreenfootImage object that is used for the background of the world, it is filled with that color -- the background of the world should not contain any transparency in it); however, when creating a new GreenfootImage object, its default drawing color is black
davmac davmac

2014/11/27

#
'image.setColor' doesn't work.
I can guarantee that it does in fact work. I suggest you post the code you tried, as there is a possibility that you used the method incorrectly.
CKnox CKnox

2014/12/2

#
1
Greenfoot.image.setColor(java.awt.Color.GREEN);
I beleieve this is the code I used to try to set the colour.
danpost danpost

2014/12/2

#
There is no 'image' field in the Greenfoot class, public or private, static or non-static. In fact, the only field in the entire class is a 'private static Random randomGenerator' field. All non-static methods must be executed on or for an object: objectReference.methodName( /* parameters, if any */ ) Do not be confused if you see a method called without an object referenced before it. The method will run on or for the same object the calling method was running on or for. Sometimes, the keyword 'this' is used to refer to that object. 'setColor' is a non-static method of the GreenfootImage class. So, it needs to be run on or for an object created from that class (a GreenfootImage object or instance). You cannot set the drawing color on a class, such as 'GreenfootImage'; that does not make any sense. But, you can set the drawing color on a GreenfootImage object. The Actor class has a 'getImage' method which returns a GreenfootImage object. The World class has a 'getBackground' method which returns a GreenfootImage object. You can create an image using one of several constructors of the GreenfootImage class (new GreenfootImage(600, 400), new GreenfootImage("imageFilename.jpg"), new GreenfootImage("Hello world!", 24, null, null)). You can set the drawing color on any of these objects.
CKnox CKnox

2014/12/3

#
Thankyou for that explanation danpost.
You need to login to post a reply.