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

lordhershey's Comments

Back to lordhershey's profile

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.
I am not sure I am seeing the font intended - got a code snippet to look at?
I will give it a shot later this afternoon, currently away from the PC.
I forgot to mention this class has an image assigned to it that it uses as a canvas.
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.
I used this trick in my Helicopter game in the ScoreBox class - the source is available.
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.
I get a null pointer exception for trying to connect to the storage server, but it is not stopping the game.
I'll check it out, can you cut/paste the error here?