Text Object Class

import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
//Added to allow text...
import java.awt.*;
/**
* Write a description of class EndGameScreen here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TextObject extends Actor
{
private int length;
private String currentText ;

public TextObject(String text)
{
// setImage("rect.png") ;
// getImage().scale(55,25) ;
int fontSize = 14;

int stringLength = (text.length() + 2) * fontSize;
// setLocation(x, y);
Font font = new Font("Dialog", 1, fontSize);
int height = (int) ( fontSize * 1.5 );
setImage(new GreenfootImage(stringLength, height));
getImage().setFont(font);
getImage().setColor(Color.BLACK);
//System.out.println("length " +stringLength);
//System.out.println(stringLength/3);
getImage().drawString(text, (int)(stringLength/4), (int)(getImage().getHeight()/2) +2);
}

public void act()
{

}

public void changeText(String text)
{
// setImage("rect.png") ;
// getImage().scale(55,25) ;
int fontSize = 14;

int stringLength = (text.length() + 2) * fontSize;
// setLocation(x, y);
Font font = new Font("Dialog", 1, fontSize);
int height = (int) ( fontSize * 1.5 );
setImage(new GreenfootImage(stringLength, height));
getImage().setFont(font);
getImage().setColor(Color.BLACK);
//System.out.println("length " +stringLength);
//System.out.println(stringLength/3);
getImage().drawString(text, (int)(stringLength/4), (int)(getImage().getHeight()/2) +2);

}

}

Login or register to tag items