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

2020/10/2

Setting background

yourboiRISHI yourboiRISHI

2020/10/2

#
I am doing lab on greenfoot. /** Creates new Beach world * with 10 X 10 cells with * cells size of 60 pixels * */ public Beach() { super (10, 10, 60); Beach turtleBeach = new Beach("backgrounds/sand2.jpg"); setBackground(turtleBeach); } when I compile, it says constructor Beach cannot be applied to given types; required: no arguments; found: java.lang.String; reason: actual and formal argument lists differ in length.
danpost danpost

2020/10/2

#
yourboiRISHI wrote...
I am doing lab on greenfoot. << Code Omitted >> when I compile, it says constructor Beach cannot be applied to given types; required: no arguments; found: java.lang.String; reason: actual and formal argument lists differ in length.
First , you should almost never create an object of the type already being created. That is, you are calling "new Beach", which creates a Beach object, from within a Beach constructor, which is already creating a Beach object. Second, you are trying to set a World object (of type Beach) as a background image. You need a GreenfootImage object to set the background to. As a shortcut, you can use the file string name as the parameter for setting the background. As far as the error, it happened because you have no Beach constructor that has a String type argument -- not that you need one.
RcCookie RcCookie

2020/10/2

#
Based on your comment‘s title I guess you want to set the background image to a given one. By saying „new Beach()“ with whatever argument in it you are actually trying to create a new instance of „Beach“. However, you only want to set the background image. So, what you actually want to do is create a new instance of GreenfootImage with the path of the image as parameter. That is also the type of parameter that „setBackground()“ expects. So, the closest to your Version would be:
1
2
3
4
5
public Beach() {
    super(10, 10, 60);
    GreenfootImage background = new GreenfootImage(„background/sand2.jpg“);
    setBackground(background);
}
You can write that a little shorter by not first saving the image into a variable:
1
2
3
4
public Beach() {
    super(10, 10, 60);
    setBackground(new GreenfootImage(„background/sand2.jpg“));
}
Actually, there is another valid parameter for „setBackground()“: The path to the image file like you create the GreenfootImage. This simplifies the code even more:
1
2
3
4
public Beach() {
    super(10, 10, 60);
    setBackground(„background/sand2.jpg“);
}
At this point you have a simple way of setting the background image. However, the names „sand2“ and „turtleBeach“ imply that you may want to have multiple beach instances working similarly but using different backgrounds. In this case you can use a parameter in the constructor:
1
2
3
4
public Beach(String imagePath) {
    super(10, 10, 60);
    setBackground(imagePath);
}
Now you can specify the image when constructing the beach. However having a parameter in the constructor will stop Greenfoot from opening this world by default. You can still open it by right-clicking onto Beach in the menu and selecting new Beach(String), and then inputting the image you want, but Greenfoot does not do that automatically. To workaround this problem we have to add a constructor without parameters. Basically you can think of this like a „default“ constructor if you do not want to specify a certain image. In this case sand2 should be used as background. This is achieved by using this ADDITIONALLY:
1
2
3
public Beach() {
    this(„background/sand2.jpg“);
}
„this(„...“);“ calls the other constructor of the class. This is very useful because we do not have to write the whole code from the main constructor into the second one. It is simply gonna call it with a predefined value.
You need to login to post a reply.