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

2012/10/24

#
No. When using the background of the world, you can have one layer. I am guessing, now, that you want some stars to move faster than others. It would probably be better to use an actor for the stars, let then get a randomly chosen speed and let them re-position themselves when reaching the edge of the world. The only thing you need to do in the world is color the background black and create the star objects, placing them randomly in the world.
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());
    }
}
OK, the above is right for the Star class. Now, in the world, you will call 'createStars();' from the constructor; and its code is
1
2
3
4
5
6
7
8
9
private void createStars()
{
    for (int i = 0; i < 100; i++)
    {
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new Star(), x, y);
    }
}
danpost danpost

2012/10/24

#
You can also remove pretty much all the code dealing with the background image of the world. The only thing you need, either in the constructor or in the createBackgroundImage() method called from the constructor, is the following
1
2
getBackground().setColor(Color.black);
getBackground().fill();
Tezuka Tezuka

2012/10/24

#
i get an error when i put the createStars() in the worlds constructer
danpost danpost

2012/10/24

#
What does the error message say? What does your world constructor look like? Did you add the createStars() method to the world class?
Tezuka Tezuka

2012/10/24

#
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 { GreenfootImage bgImg = null;// holds main image int bgX;//tracks offset of image in background final int starCount = 100;// change as desired String stars = {"stars"}; /** * 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(); createStars(); } 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); setBackground(img); } public void createStars() { for(int i = 0; i < 100; i++) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject( new Stars(),1, 1); } } } and the error java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0 at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016) at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186) at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:179) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:131) at Stars.<init>(Stars.java:20) at SpaceWorld.createStars(SpaceWorld.java:68) at SpaceWorld.<init>(SpaceWorld.java:25)
danpost danpost

2012/10/24

#
Just to be sure, it looks like I dropped a couple of characters in the Star class code. Line 10 should have a close parenthesis after the '4'
1
int size = Greenfoot.getRandomNumber(4) + 1;
and, line 15 is missing the semi-colon at the end
1
speed = Greenfoot.getRandomNumber(4) + 1;
Tezuka Tezuka

2012/10/24

#
I can compile now but i only got 4 stars at the top of the background and when they reaches the edge they stopped
danpost danpost

2012/10/24

#
From the error message, the sixth line 'at Stars<init>(Stars.java:20)' is the first line that references a class in your scenario ('Stars'). The '20' is the line number in the class that triggered the error. That line is probably the one that tries to create the GreenfootImage for the star. By making one of the corrections I noted in my last post, that should no longer be a problem. This is what your world class should look like now
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);
        }
    }
}
danpost danpost

2012/10/24

#
I made a correction in your world class. In the last line, you had (1, 1) as the placement location for all the stars. I changed it to (x, y) for random locations (see the previous two statements). The stars should, however be wrapping around; maybe you need to post the code you have for the star class.
Tezuka Tezuka

2012/10/24

#
why cant i compile 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 { 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(); createStars(); } public void createBackgroundImage() { Background.setColor(Color.BLACK);// its supposed to be getBackground.setColor I misstyped 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); } } }
danpost danpost

2012/10/24

#
In your createBackgroundImage() method, change the first line to
1
getBackground().setColor(Color.BLACK);
HELPING HINTS: (1) when posting code, use the 'code' tag under the reply box and copy/paste the code in the box provided (2) when copying posted code, select 'view plain' at the top of the code frame, select all the text and copy/paste into your scenario
danpost danpost

2012/10/24

#
I noticed the other line in the createBackgroundImage() method was also copied wrong. It should be
1
getBackground().fill();
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
{
    GreenfootImage background = getBackground();// 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();
        createStars();
        scrollBackground();
    }
     
    public void createBackgroundImage()
    {
      background.setColor(Color.BLACK);
      background.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);
        }
    }
     
    public void scrollBackground()
    {
         bgX = (bgX - 1) % getWidth();
         GreenfootImage img = new GreenfootImage(getWidth(), getHeight());
         img.drawImage(background, bgX,0);
         img.drawImage(background, bgX + getWidth(),0);
         setBackground(img);
     }
    }
The stars just hit the edge and stopps there
danpost danpost

2012/10/24

#
Replace your SpaceWorld class code with what I posted above using copying/pasting as I explained in 'Helping hints'. Then, if it does run properly, post the Stars class code.
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
33
34
35
36
37
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Stars here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Stars extends Actor
{
    private int speed;
    /**
     * Act - do whatever the Stars wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    public Stars()
    {
        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()
    {
        
    }   
     
    public void move()
    {
         setLocation(getX()+speed ,getY());
    }
}
Here is my Star class and I replaced the world class code it just says that it cannot find variable getbackground
There are more replies on the next page.
1
2
3
4
5