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

2012/1/10

Painting Starts onto a Background Image

1
2
3
4
5
6
7
danpost danpost

2012/10/24

#
Why do you want to keep track of them?
Tezuka Tezuka

2012/10/24

#
well its in my school task
danpost danpost

2012/10/24

#
OK, add another instance variable (field; ) to the world class 'Star stars = new Star;' somewhere AFTER 'final int starCount = 300;'. That will declare the variable array. Then in the createStars() method, replace the line 'addObject(new Star(), x, y);' with
stars[i] = new Star();
addObject(stars[i], x, y);
The first line sets the new star at that index of the array and the second adds it to the world.
Tezuka Tezuka

2012/10/24

#
import greenfoot.*; 
import java.awt.Color;

public class SpaceWorld extends World
{
    final int starCount = 300;
    Star[] stars = new Star[starCount];
    public SpaceWorld()
    {    
        super(600, 400, 1); 
        createBackgroundImage();
        createStars();
    }
    
    public void createBackgroundImage()
    {
        getBackground().setColor(Color.BLACK);
        getBackground().fill();
    }
    
    public void createStars()
    {
        for (int i = 0; i < starCount; i++)
        {
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            stars[i] = new Star();
            addObject( stars[i], x, y);
        }
    }
}
this is my code it didnt work I also declared this in the fields Star stars = new Star;
danpost danpost

2012/10/24

#
Why do you think it did not work? Does it not run? Do you get an error message (if so, what)?
Tezuka Tezuka

2012/10/24

#
The error says that the constructor Star in class Star cannot be applied to given types; required java.lang, String found no arguments
danpost danpost

2012/10/24

#
Please post your Star class. You must have changed something there.
Tezuka Tezuka

2012/10/24

#
nvm i worked now but when i compiled it there are no stars here is my code for star class import greenfoot.*; import java.awt.Color; public class Star extends Actor { private int speed; public Star() { int size = Greenfoot.getRandomNumber(4) +1; GreenfootImage img = new GreenfootImage(size, size); int r = Greenfoot.getRandomNumber(100); int g = Greenfoot.getRandomNumber(100); int w = Greenfoot.getRandomNumber(100); img.setColor(Color.white); img.fillOval(0, 0, size, size); setImage(img); speed = Greenfoot.getRandomNumber(4) + 1; } public void act() { move(); } public void move() { int x = (getX() + speed) % getWorld().getWidth(); setLocation(x, getY()); } }
Tezuka Tezuka

2012/10/24

#
import greenfoot.*; 
import java.awt.Color;

public class SpaceWorld extends World
{
    final int starCount = 300;
    Star[] stars = new Star[starCount];
    public SpaceWorld()
    {    
        super(600, 400, 1); 
        createBackgroundImage();
        createStars();

    }
    
    public void createBackgroundImage()
    {
        getBackground().setColor(Color.BLACK);
        getBackground().fill();
    }
    
    public void createStars()
    {
        for (int i = 0; i < starCount; i++)
        {
            stars[i] = new Star();
            addObject( stars[i], getWidth(), getHeight());
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
        }
    }
}
here is my world class
danpost danpost

2012/10/25

#
This should be your createStars() method in the world class
public void createStars()
{
    for (int i = 0; i < starCount; i++)
    {
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        stars[i] = new Star();
        addObject( stars[i], x, y);
    }
}
danpost danpost

2012/10/25

#
I am not saying that will get it to work, only that I had noticed you had changed it.
danpost danpost

2012/10/25

#
Also, I noticed you had veered from the following in the Star constructor:
int r = Greenfoot.getRandomNumber(256); // red
int g = Greenfoot.getRandomNumber(256); // green
int b = Greenfoot.getRandomNumber(256); // blue
img.setColor(new Color(r, g, b));
Tezuka Tezuka

2012/10/25

#
yea it worked but i dont really know what the difference in having the star and addobject before int x and int y. But thx for the help I appreaciate it.
danpost danpost

2012/10/25

#
You cannot add a star at the random location (x, y) until you have x and y randomly set.
Tezuka Tezuka

2012/10/25

#
Could u tell me how can I call my move() in my world class?
There are more replies on the next page.
1
2
3
4
5
6
7