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

Comments for Tank Wars 2.2.8

Return to Tank Wars 2.2.8

GRIFFINGRIFFIN

2015/1/9

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!
lordhersheylordhershey

2015/1/9

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.
lordhersheylordhershey

2015/1/9

I used this trick in my Helicopter game in the ScoreBox class - the source is available.
GRIFFINGRIFFIN

2015/1/9

So you just copy the font file into the directory?
GRIFFINGRIFFIN

2015/1/9

And then where would I put that code?
lordhersheylordhershey

2015/1/9

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.
lordhersheylordhershey

2015/1/10

I forgot to mention this class has an image assigned to it that it uses as a canvas.
A new version of this scenario was uploaded on 2015-01-10 16:48:13 UTC Fonts may work this time not sure.
GRIFFINGRIFFIN

2015/1/10

Does the font work for you?
lordhersheylordhershey

2015/1/10

I will give it a shot later this afternoon, currently away from the PC.
lordhersheylordhershey

2015/1/10

I am not sure I am seeing the font intended - got a code snippet to look at?
GRIFFINGRIFFIN

2015/1/11

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?
lordhersheylordhershey

2015/1/12

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.
willMwillM

2015/1/12

Yes.
fiber16fiber16

2015/4/13

I am very impressed! I't a very cool game!
fiber16fiber16

2015/4/13

I am very impressed! I't a very cool game!
fiber16fiber16

2015/4/13

oops sorry
willMwillM

2015/4/13

you dont not heff to beh sorry, you just like teh game alot :3