Sorry about the fonts, just realized that I coded the fonts on a mac that had different fonts installed that weren't on PC. If you guys know how to fix this without changing the names or installing a new one just tell me!
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.
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.
Well I just tried putting the tff file into the directory and then changing the name, but I guess it only works because its on my computer. There are like 8 different classes that use the font and mulitple worlds, would I have to change a lot of my code to implement the font?
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.
2015/1/9
2015/1/9
2015/1/9
2015/1/9
2015/1/9
2015/1/9
2015/1/10
2015/1/10
2015/1/10
2015/1/10
2015/1/11
2015/1/12
2015/1/12
2015/4/13
2015/4/13
2015/4/13
2015/4/13