If you could make the font globally available then you would not have to modify too much. The code that I have there exists since it was just something I wanted to try. let me poke at it to see if it can be made easier, others might have a better idea if you posted a question in the Discussion forum, lots of people have done it and probably better than I had.
The TTF file is just in the project directory. The Class that has the code looks like this (sorry there is no code tag here):
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
/**
* Write a description of class ScoreBox here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ScoreBox extends Actor
{
BufferedImage bi = null;
Graphics2D g = null;
int w = 0;
int h = 0;
private Font myFont = new Font("TimesRoman", Font.BOLD, 18);
public static Color mycolor = new Color(255,255,50,150);
FontMetrics textMetrics = null;
public ScoreBox()
{
try
{
InputStream is = this.getClass().getResourceAsStream("Outwrite.ttf");
Font customFont = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(23f);
myFont = customFont;
}
catch (Exception e)
{
e.printStackTrace();
}
GreenfootImage gi = getImage();
bi = gi.getAwtImage();
g = bi.createGraphics();
w = bi.getWidth();
h = bi.getHeight();
g.setFont(myFont);
textMetrics = g.getFontMetrics(myFont);
g.setBackground(new Color(255,255,255,0));
}
public void act()
{
g.clearRect(0,0,w,h);
g.setColor(/*Color.YELLOW*/ mycolor);
String score = "Score: " + GameManager.getScore();
g.drawString(score,w - textMetrics.stringWidth(score) ,18);
}
}
You can see I give myFont a default value, one that I know will always be around (Times Roman), in the constructor I make an attempt to overwrite it, but I have a try/catch block just in case it fails. This class monitors a static variable (the score) and keeps redrawing the text every act cycle - it is very inefficient though.
You can bundle the font file into the project and then call it up like so:
try
{
InputStream is = this.getClass().getResourceAsStream("Outwrite.ttf");
Font customFont = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(23f);
myFont = customFont;
}
catch (Exception e)
{
e.printStackTrace();
}
I have the file Outwrite.ttf in the green foot project directory so it gets bundled when I hit the share button.
2015/1/12
Tank Wars 2.2.8
2015/1/10
Tank Wars 2.2.8
2015/1/10
Tank Wars 2.2.8
2015/1/10
Tank Wars 2.2.8
2015/1/9
Tank Wars 2.2.8
2015/1/9
Tank Wars 2.2.8
2015/1/9
Tank Wars 2.2.8
2015/1/6
DragonFire
2015/1/6
DragonFire