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

2014/12/9

How do I make a string of colors?

christinem024 christinem024

2014/12/9

#
I'm so stuck on how to use a string of colors. PlayerWorld world:
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class PlayerWorld extends World
{ 
    private PlayMusicButton q;
    private PlayMusicButton[] playmusicbutton = new PlayMusicButton[5];
    private static String[] soundFiles = {"Drive Me Home", "Get It On", "Hair", "Marry The Night", "Open Up Wide"};
    private static String[] songNames = {"Drive Me Home", "Get It On", "Hair", "Marry The Night", "Open Up Wide"};
    private static String[] colors = {"Color.yellow", "Color.white", "Color.lightGray", "Color.pink", "Color.cyan"};
    
    public PlayerWorld()
    {
        super(600, 400, 1);

        addObject(new StopButton(), 480, 200);
        
        for(int i = 0; i < 5; i++)
        {
            addObject(new PlayMusicButton(soundFiles[i], songNames[i], colors[i]), 200, 50 + i * 75);
        }
    }
}
PlayMusicButton Actor Class:
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class PlayMusicButton extends Actor
{
    private String soundFiles;
    private String songNames;
    private String colors;
    
    private static final int WIDTH = 220;
    private static final int HEIGHT = 40;

    private boolean selected;
    
    public PlayMusicButton(String soundFiles, String songNames, String colors)
    {
        this.soundFiles = soundFiles;
        this.songNames = songNames;
        this.colors = colors;
        selected = false;
        
        makeImage();
    }
    
    public void act()
    {
        checkMouseClick();
    }
    
    private void checkMouseClick()
    {
        if(!selected && Greenfoot.mouseClicked(this)) {
            GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
            image.setColor(colors);
            image.fillRect(0, 0, WIDTH, HEIGHT);
            image.setColor(Color.BLACK);
            image.drawRect(0, 0, WIDTH-1, HEIGHT-1);

            image.setColor(Color.BLACK);
            Font font = image.getFont();

            image.setFont(new Font("Courier New", Font.BOLD, 22));
            image.drawString("" + songNames, 12, 22);
            setImage(image);
            selected = true;
            Greenfoot.playSound(soundFiles + ".mp3");
        }
        
        if(selected && !Greenfoot.mouseClicked(this)) {
            GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
            image.setColor(colors);
            image.fillRect(0, 0, WIDTH, HEIGHT);
            image.setColor(Color.BLACK);
            image.drawRect(0, 0, WIDTH-1, HEIGHT-1);

            image.setColor(Color.BLACK);
            Font font = image.getFont();

            image.setFont(new Font("Courier New", Font.PLAIN, 22));
            image.drawString("" + songNames, 12, 22);
            setImage(image);
            selected = true;
            Greenfoot.playSound(soundFiles + ".mp3");
            selected = false;
        }
    }
    
    public boolean isSelected()
    {
        return selected;
    }

    private void makeImage()
    {
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
        image.setColor(colors);
        image.fillRect(0, 0, WIDTH, HEIGHT);
        image.setColor(Color.BLACK);
        image.drawRect(0, 0, WIDTH-1, HEIGHT-1);

        image.setColor(Color.BLACK);
        Font font = image.getFont();

        image.setFont(new Font("Courier New", Font.PLAIN, 22));
        image.drawString("" + songNames, 12, 22);
        setImage(image);
    }
}
NikZ NikZ

2014/12/9

#
Confused with line 11. Are you trying to save awt Colors? Then don't use String, use Color.
davmac davmac

2014/12/9

#
It looks to me like you don't want a string of colors at all - you just want to use a color. Change line 11 in PlayerWorld to:
    private static Color[] colors = {Color.yellow, Color.white, Color.lightGray, Color.pink, Color.cyan};
Change line 9 in PlayMusicButton to:
    private Color colors;  
and line 16 to:
    public PlayMusicButton(String soundFiles, String songNames, Color colors)  
Finally, I suggest changing the name of the 'colors' variable and constructor parameter in the PlayMusicButton to just 'color' - since it actually only represents a single color at that point.
christinem024 christinem024

2014/12/9

#
@davmac I want 5 different song buttons in 5 different colors, and I'm confused about how to do that since I have the 2 strings (songNames and soundFiles) for the creation of the PlayMusicButtons in PlayerWorld
davmac wrote...
It looks to me like you don't want a string of colors at all - you just want to use a color. Change line 11 in PlayerWorld to:
    private static Color[] colors = {Color.yellow, Color.white, Color.lightGray, Color.pink, Color.cyan};
Change line 9 in PlayMusicButton to:
    private Color colors;  
and line 16 to:
    public PlayMusicButton(String soundFiles, String songNames, Color colors)  
Finally, I suggest changing the name of the 'colors' variable and constructor parameter in the PlayMusicButton to just 'color' - since it actually only represents a single color at that point.
davmac davmac

2014/12/9

#
I think my answer above gives you that. You use Strings for songNames and soundFiles because these are naturally represented as Strings; for colors you should use Color. In any case line 77:
        image.setColor(colors);  
will not work unless 'colors' is of type Color, because the setColor method requires a parameter of that type.
christinem024 christinem024

2014/12/9

#
Oh wow, I feel silly. Thank you so much, that makes a lot more sense. You're awesome @davmac !!!!
davmac wrote...
I think my answer above gives you that. You use Strings for songNames and soundFiles because these are naturally represented as Strings; for colors you should use Color. In any case line 77:
        image.setColor(colors);  
will not work unless 'colors' is of type Color.
You need to login to post a reply.