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
danpost danpost

2012/10/24

#
Sorry, that is my fault. Let me post the code for both classes again. You can copy/paste both into your scenario and it should run fine. First, the SpaceWorld class:
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
import greenfoot.*;
import java.awt.Color;
 
public class SpaceWorld extends World
{
    final int starCount = 100;
     
    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 < 100; i++)
        {
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject( new Stars(), x, y);
        }
    }
}
Now, the Star class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
        img.setColor(Color.white);
        img.fillOval(0, 0, size, size);
        setImage(img);
        speed = Greenfoot.getRandomNumber(4) + 1
    }
 
    public void act()
    {
        int x = (getX() + speed) % getWorld().getWidth();
        setLocation(x, getY());
    }
}
Tezuka Tezuka

2012/10/24

#
nvm the world class it worked now,but i cant still make the background to scroll. Can i call the move() in my world class?
danpost danpost

2012/10/24

#
Copy/paste both class codes into your scenario (replacing what you have in both classes). Line 27 of the SpaceWorld class code should be
1
addObject(new Star(), x, y);
Tezuka Tezuka

2012/10/24

#
I cant compile it I think on line 10 u are missing a closin paranthese?
Tezuka Tezuka

2012/10/24

#
It worked great now but dont u need the scrolling methode to make the background rolling?
danpost danpost

2012/10/24

#
Oh, yeah. The same two correction (on lines 10 and 15) still need to be made (the ones I posted earlier).
danpost danpost

2012/10/24

#
No. The world background is a flat black color. Even if you made it scroll, you would not notice it moving; so...why scroll it?
Tezuka Tezuka

2012/10/24

#
public void act() { int x = (getX() + speed) % getWorld().getWidth(); setLocation(x, getY()); } I dont really get this line , when i try to change it setLocation (get(X)+20, getY()) and the stars just hits the edge without returning
danpost danpost

2012/10/24

#
What you probably do not understand is the mathmatical operator '%'. This operator takes what is on the left of it and divides that by what is on the right and returns the remainder portion only. So, it would be similiar to coding:
1
2
3
4
5
int x = getX() + speed;
if (x >= getWorld().getWidth())
{
    x = x - getWorld().getWidth();
}
When the new location is past the end of the world, wrap around to the beginning. Actually, replacing 'if' with 'while' in the above code more reflects what the operator does.
1
2
3
4
5
6
7
int x = (a + n) % b;
// is equivalent to
int x = a + n;
while (x >= b)
{
    x = x - b;
}
Tezuka Tezuka

2012/10/24

#
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
import greenfoot.*;
import java.awt.Color;
 
public class SpaceWorld extends World
{
    final int starCount = 300;
    String [] Star = {"Star1","Star2","Star3"};
    String[] Speed = {"Speed1","Speed2","Speed3"};
    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++)
        {
            Star star = new Star ( Star[i], Speed[i]);
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject( new Star(), x, y);
        }
    }
}
Iam trying to make the String and change the speed and color of my star with this code?
danpost danpost

2012/10/24

#
Speed and color are attributes of the star object and should be dealt with within the Star class. The stars already have varied speeds, you just want to add some variance in color. That can be done in the constructor of the Star class. Remove and replace line 12 ('img.setColor(Color.white));') with the following code
1
2
3
4
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/24

#
what I meant was to create an array and out the variable speed and color in it, and each star represents its own speed and color like in the piano scenario
danpost danpost

2012/10/24

#
As I do not know where you are going with this, it is hard for me to determine how to help you. Will each star be different from any other (or will there be similar ones)? What is the neccessity in saving the color in each Star object? Why would you want to use Strings to determine the color and speed?
danpost danpost

2012/10/24

#
I posted a demo related to this discussion, hope you like it. It is called Space Flight Demo.
Tezuka Tezuka

2012/10/24

#
thnx I appreciate the Demo. Why I want array is to keep track of all the stars
There are more replies on the next page.
1
2
3
4
5
6