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

2013/7/11

How do i change the font, size, and color of the text??

qsapp.3 qsapp.3

2013/7/11

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Label here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Label extends Actor
{
    public Label(String text)
    {
        GreenfootImage img = new GreenfootImage (text.length()*20, 30);
        img.drawString (text, 2, 20);
        setImage (img);
    }
    
    /**
     * Act - do whatever the Label wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
darkmist002 darkmist002

2013/7/11

#
when i made labels/buttons, i used the setImage to do it ex: setImage(new GreenfootImage("String " + minerals, 20, Color.GREEN, Color.BLUE)); where "String" is the string you want displayed, the + variable will display a variable (adding it to a string), the 20 is the font size (change to whatever size you want), the first color (green in this example) is the color of the text, and the second color (blue in this example) is the color of the background of the box created. this site has a list of the colors you can use for it: http://www.geom.uiuc.edu/~daeron/docs/apidocs/java.awt.Color.html (just make sure you make them uppercase like in the example)
qsapp.3 qsapp.3

2013/7/12

#
it says it has an error about the word "minerals"
danpost danpost

2013/7/12

#
'minerals' is an int field name that darkmist002 has in his class, not necessarily something you should put in your code. Your class code should be more like the following:
import greenfoot.*;
import java.awt.Color;

public class Label extends Actor
{
    public Label(String text)
    {
        // create the text image
        int fontsize = 20; // change as desired
        Color fontColor = Color.BLACK; // change as desired
        Color bgColor = new Color(0, 0, 0, 0); // transparent background
        GreenfootImage txtImg = new GreenfootImage(text, fontsize, fontColor, bgColor);
        // create the base image
        GreenfootImage img = new GreenfootImage(text.getWidth()+20, text,getHeight()+10);
        bgColor = Color.YELLOW; // change as desired
        img.setColor(bgColor);
        img.fill();
        // draw text image on base image
        img.drawImage(txtImg, 10, 5);
        setImage(img);
    }
    
    public void act() 
    {
    }    
}
danpost danpost

2013/7/12

#
There would be a lot more involved if you actually wanted to change the font style as well (importing the Font class and applying the font style to an image before drawing a string on it).
You need to login to post a reply.