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

2017/2/18

Set background

Fifilein Fifilein

2017/2/18

#
Hi! I want to set a background with an image of mine. The Image is a part of the folder "images". I don't know why it doesn't works. Here is my code:
 setBackground(new GreenfootImage("l1.png"));
The code is in a Worldclass. Can you help me please!? Thank you , Fifilein
Super_Hippo Super_Hippo

2017/2/18

#
Did you put this line in the constructor?
Fifilein Fifilein

2017/2/18

#
Yes
Super_Hippo Super_Hippo

2017/2/18

#
Do you get any error message while compiling or running the code? Is the world really used? Maybe you have to show the whole code of this class (or at least the parts which matter if you have too much in it).
Fifilein Fifilein

2017/2/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;   
/**
 * Write a description of class StartMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StartMenu extends World
{
     StartGame sg = new StartGame();
     //Instruc in = new Instruc(); 
    /**
     * Constructor for objects of class StartMenu.
     * 
     */
    public StartMenu()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        
         setBackground(new GreenfootImage("l1.png"));
        
        {
            addObject(sg,getWidth()/2, getHeight()/2 );
        }
       {
            addObject(in,300, 100);
       }
    }
   
}
When I want to open the world I get a message in a Greenfoot: Terminal-Window
java.lang.IllegalArgumentException: java.io.FileNotFoundException: Could not find file: l1.png
	at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:281)
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:108)
	at StartMenu.<init>(StartMenu.java:22)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at greenfoot.core.Simulation.newInstance(Simulation.java:617)
	at greenfoot.localdebugger.LocalDebugger$QueuedInstantiation.run(LocalDebugger.java:155)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502)
	at greenfoot.core.Simulation.maybePause(Simulation.java:305)
	at greenfoot.core.Simulation.runContent(Simulation.java:218)
	at greenfoot.core.Simulation.run(Simulation.java:211)
Caused by: java.io.FileNotFoundException: Could not find file: l1.png
	at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:551)
	at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:278)
	... 12 more
danpost danpost

2017/2/18

#
Make sure you have the "l1.png" file, or a copy of it, in the 'images' folder of your project folder. You can use the function of setting the default image of the class to browse and import the file and then either reset the image of the class to what it was originally or back to no image.
Nosson1459 Nosson1459

2017/2/19

#
Check the spelling and file type of the image you're trying to get.
Fifilein Fifilein

2017/2/19

#
Thank you now it works!
You need to login to post a reply.