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

2015/12/1

How to add image to a button

XxCelosiaxX XxCelosiaxX

2015/12/1

#
I am trying to make a JukeBox simulator, so make a string for all my songs and image I want to use, so it is no hard coded, now I am trying to make a button appear for each song. Now I want the button to contain the image representing each song, so I listed all the image in the string below and I included the button image part in the "makeButton" method, however the button image did not change. Here is the JukeBox or the background class
public class JukeBox extends World
{
    private String[] buttons = {"A", "S", "D", "F", "G"};
    private String[] buttonImage = {"1.png","2.png","3.png","4.png","5.png"};
    private String[] buttonSongs = {"1925.mp3", "Irony.mp3", "matryoshka.mp3", "SeasonalFeathers.mp3","Wolf&Red.mp3"};
    private int [] buttonType = {1,1,1,1,1};

    public JukeBox()
    {    
        super(600, 400, 1); 
        makeButtons();
    }

    private void makeButtons()
    {
        for(int i = 0; i < buttons.length; i++)
        {
            Button button = new Button ( buttons[i], buttonSongs[i]+ "",buttonImage[i] + "", buttonType[i]);
            addObject(button, i*63 + 50, 140);
            addObject(button, i,160);
            Stop stop = new Stop();
            addObject(stop, 100, 350);
            Play play = new Play();
            addObject(play, 180, 350);
        
        }
    }

}
This is my button (actor) class
public class Button extends Actor
{
    private String buttonName;
    private String sound;
    private int typeOfButton;
    private String buttonImage;
    private GreenfootSound gfs;
    private boolean isPlaying;
    
    public Button(String buttons, String soundFile, String image, int buttonType)
    {
        buttonName = buttons;
        sound = soundFile;
        buttonImage = image;
        typeOfButton = buttonType; 
        gfs = new GreenfootSound(sound); 
    }
How could I change the actor into the image in the string list? Im not sure if the question make sense, but please comment.
danpost danpost

2015/12/1

#
I do not see any 'setImage' line in the constructor. There are two methods with that name in the Actor class. Refer to the API documentation of the class for their uses, if needed.
You need to login to post a reply.