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

2012/1/10

#
I just need a basic skeleton of code for this. I'm very new to programming so I have almost no experience, and have been working on this for quite a while. This is what I have, and I need some guidance: /** * Creates stars to be displayed on the background image. * The parameter shows the number of stars on the background image. */ public void createStars(int number) { GreenfootImage background = getBackground(); for(int i = 0; i < number; i++) { int x = Greenfoot.getRandomNumber(300); int y = Greenfoot.getRandomNumber(300); GreenfootImage image = new GreenfootImage(2, 2); image.setColor(Color.WHITE); image.fillOval(2, 2, x, y); setBackground(image); } } }
danpost danpost

2012/1/10

#
OK, your x and y are where in the world background you want your star; good so far. Your GreenfootImage image will be a star; ok here. Setting the draw color for the star to white; still good. Using fillOval on the image with oval width 2 and height 2 to be drawn at x and y; now we got problems. First off, x and y are the coordinates in the background image where you want the star, not coordinates of the image, like this method needs. Instead of using 'fillOval', just use 'fill, as the image is only 2 x 2 to begin with. Next, what you need is to use the 'drawImage' method to actually put the star on the background image (we will get to that in a minute). The last statement 'setBackground(image);' is not what you want either; as that will make your star the backgound image. You want your background image with stars. The last thing is this: all of your stars are the same size and use the same basic image; let us simplify the code by moving that outside the 'for' loop. As such:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void createStars(int number)
{
    GreenfootImage background = getBackground();  // getting a reference to the background image
    GreenfootImage image = new GreenfootImage(2, 2);  // creating a new image for the star
    image.setColor(Color.WHITE);  // to give the star brightness
    image.fill();  //  star image complete
    for (int i = 0; i < number; i++)
    {
        int x = Greenfoot.getRandomNumber(300);
        int y = Greenfoot.getRandomNumber(300);
        background.drawImage(image, x, y);  // putting the star on the background
    }
    // setBackground(background); is not needed as background is already set to 'background'
}
danpost danpost

2012/1/10

#
Of course, this works too:
1
2
3
4
5
6
7
8
9
10
11
public void createStars(int number)
{
    GreenfootImage background = getBackground();
    background.setColor(Color.WHITE);
    for (int i = 0; i < number; i++)
    {
        int x = Greenfoot.getRandomNumber(300);
        int y = Greenfoot.getRandomNumber(300);
        background.fillOval(2, 2, x, y);
    }
}
theDoctor theDoctor

2012/1/12

#
Thanks a lot danpost. This has seriously helped me.
Tezuka Tezuka

2012/10/23

#
Hi Danpost I have a question. have can u make so the stars to move to the right and when it hits the edge it dissapears and a new star will pop in the same place but in the left side?
danpost danpost

2012/10/23

#
You will need two instance world fields, besides the one that holds the number of stars to place in the world; one to hold the background image and another to track the offset of the image in the background. Let your world constructor create the initial image and save it in the GreenfootImage field or call a method to do so. The act() method in your world should call a method to deal with the scrolling of the background. That method should increment the offset and create and set the new image for the background.
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
35
// In your world class
// at the top
import java.awt.Color;
// instance fields
GreenfootImage bgImg = null; // holds main image
int bgX; // tracks offset of image in background
final int starCount = 200; // change as desired
// in your world constructor
createBackgroundImage();
// the createBackgroundImage() method
private void createBackgroundImage()
{
    bgImg = new GreenfootImage(getWidth(), getHeight());
    bgImg.setColor(Color.black);
    bgImg.fill();
    bgImg.setColor(Color.WHITE);
    for (int i = 0; i < starCount; i++)
    {
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        bgImg.fillOval(2, 2, x, y);
    }
    setBackground(bgImg);
}
// in your act method
scrollBackground();
// the scrollBackground() method
private void scrollBackground()
{
    bgX = (bgX + 1) % getWidth();
    GreenfootImage img = new GreenfootImage(getWidth(), getHeight());
    img.drawImage(bgImg, bgX, 0);
    img.drawImage(bgImg. bgX - getWidth(), 0);
    setImage(img);
}
The stars will then re-appear on the left when they disappear on the right.
Tezuka Tezuka

2012/10/23

#
Hi do I need the code from the creater of this discuss? cuz it didnt work with only your code
danpost danpost

2012/10/23

#
You shouldn't. Let me double check what I have here. Shouldn't take too long!
danpost danpost

2012/10/23

#
Yeah, I found three things wrong with it (1) Line 21: change 'bgImg.fillOval(2, 2, x, y);' to 'bgImg.fillOval(x, y, 2, 2);' (2) Line 33: change the period '.' to a comma ',' (3) Line 34: change 'setImage' to 'setBackground'
Tezuka Tezuka

2012/10/23

#
i followed ur code but it ddint draw any stars just a oval with white color
Tezuka Tezuka

2012/10/23

#
yea it worked now but the stars arnt moving?
danpost danpost

2012/10/23

#
If your started the scenario and they do not move, then it must be something else in your code. I created a new scenario with only the code above (with the three fixes) and when I ran it, it worked properly. The stars move from left to right and re-appear at the left.
Tezuka Tezuka

2012/10/23

#
Here is my code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class SpaceWorld here. * * @author (your name) * @version (a version number or a date) */ public class SpaceWorld extends World { int color; GreenfootImage bgImg = null;// holds main image int bgX;//tracks offset of image in background final int starCount = 100;// change as desired /** * Constructor for objects of class SpaceWorld. * */ public SpaceWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); createBackgroundImage(); } public void act() { scrollBackground(); } public void createBackgroundImage() { bgImg = new GreenfootImage(getWidth(), getHeight()); bgImg.setColor(Color.black); bgImg.fill(); bgImg.setColor(Color.WHITE); for( int i = 0; i< starCount; i++) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); bgImg.fillOval(x,y,2,2); } setBackground(bgImg); } public void scrollBackground() { bgX = (bgX + 1) % getWidth(); GreenfootImage img = new GreenfootImage(getWidth(), getHeight()); img.drawImage(bgImg, bgX,0); img.drawImage(bgImg, bgX - getWidth(),0); } }
danpost danpost

2012/10/23

#
You are missing the last statement in the scrollBackground() method
1
setBackgound(img);
You have declared and 'int color' variable in the world class, yet you are not using it (just to let you know)
Tezuka Tezuka

2012/10/23

#
ok thx it worked now . If i want to add another layer of stars , do I just need to do the same method?
There are more replies on the next page.
1
2
3
4