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

2013/6/11

Colors

Gingervitis Gingervitis

2013/6/11

#
How can I set the text color of my label class to be white? I already imported java.awt.Color package but I don't know how to use it very well.
What does your Text class look like? That way we know how to draw the image to be white. It would probably be something like setColor(Color c), but I don't know what the class looks like.
Gingervitis Gingervitis

2013/6/11

#
This is my Label class
public Label(String text)
    {
        GreenfootImage img = new GreenfootImage (100,30);
        img.drawString(text ,2 , 20);
        setImage(img);
    }
Add this the line before img.drawString(...) (I'm looking at a Text class I took from a friend and then modified myself)
img.setColor(Color.white); //Or whatever other color you want.
Gingervitis Gingervitis

2013/6/11

#
I tried that after line 5 and it did nothing but let me putting it before.
Put it before img.drawString(). Line 4
Gingervitis Gingervitis

2013/6/11

#
That worked! Thank you! Do you know the mouse methods? I am working on a game where you drag the labels kinda like a matching game, but I don't know how to exactly do that.
http://www.greenfoot.org/files/javadoc/greenfoot/MouseInfo and http://www.greenfoot.org/files/javadoc/greenfoot/Greenfoot The greenfoot API is your friend. Use it like your friends. Actually, use it more than your friends. http://www.greenfoot.org/files/javadoc/
Gingervitis Gingervitis

2013/6/11

#
I have already looked at that, but I don't really understand how to use the methods.
Alright, I'll explain them better: Greenfoot.getMouseInfo() gives you the MouseInfo currently, or null if no mouse is on the screen. Greenfoot.mouseClicked(Object obj) returns true if the mouse just clicked on the object provided. Greenfoot.mouseDragged(Object obj) returns true if the mouse is dragging on the object provided. Greenfoot.mousePressed(Object obj) returns true if the mouse just pressed (went from unpressed to pressed) on the object provided. Use the above methods to determine for different objects if they had those current actions were applied to the mouse over the object provided. For a MouseInfo object (These methods won't be used much, unless you want more specific details): getActor() returns the actor the mouse is associated with getButton() returns the number of a pressed number, from 0 being none, and 1, 2, and 3 being left, right, and middle click (i think... either that or left middle right) getX() and getY() gets the current X and Y of the mouse Some examples of using these methods:
//Changing image when clicked
if (Greenfoot.mouseClicked(this))
    setImage(newImage);

//Dragging (I know you'll want this one)
if (Greenfoot.mouseDragged(this))
{
    MouseInfo mi = Greenfoot.getMouseInfo();
    setLocation(mi.getX(), mi.getY());
}
Gingervitis Gingervitis

2013/6/11

#
If I wanted to have my label move when I click and drag the mouse over it, would the code look like the code above? Or would additional code be needed?
Yep, it would look like what I have written down.
You need to login to post a reply.