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

2014/5/4

Font from dafont

w9ndel w9ndel

2014/5/4

#
Does anybody know if it is possible to use a font that isn't a standard one (Calibri, Arial, etc.) I am making a Flappy Bird game and want to change the font of the counter to the original flappy bird font. Thanks on forward.
danpost danpost

2014/5/4

#
There is probably a round-about way to set up a non-standard character set; but, that is beyond me at the moment. What you could do is create image(s) (in the font wanted) of the words used on the counter and an image for each of the digits. Then, have your counter class use those images to create the effect you want.
Lollygag Lollygag

2014/5/5

#
My game which use custom font
import java.awt.Color;
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.PrintStream;
import greenfoot.GreenfootImage;

/**
 * Custom Font.
 * 
 * @author Lollygag
 * @version 2014
 */
public class Font  
{
    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public static GreenfootImage drawString(String str, Color color, String font, float size)
    {
        // put your code here
        int strLength = (str.length() + 2) * (int)size;
        GreenfootImage img = new GreenfootImage(strLength, (int)size);
        img.setColor(color);
        img.setFont(getFont(font, size));
        img.drawString(str, 0, (int)size);
        return img;
    }
    
    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public static GreenfootImage drawString2(String str, Color color, String font, float size)
    {
        // put your code here
        int strLength = (str.length() + 2) * (int)size;
        GreenfootImage img = new GreenfootImage(strLength, (int)size);
        img.setColor(Common.TEAL);
        img.fill();
        img.setColor(color);
        img.setFont(getFont(font, size));
        img.drawString(str, 0, (int)size);
        return img;
    }
    
    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    private static java.awt.Font getFont(String name, float size)
    {
        // put your code here
        java.awt.Font font = null;
        try
        {
            java.awt.Font tmpFont = java.awt.Font.createFont(0, Font.class.getResourceAsStream("/fonts/" + name + ".TTF"));
            font = tmpFont.deriveFont(size);
        }
        catch (IOException e)
        {
            System.err.println(e.getMessage());
        }
        catch (FontFormatException e)
        {
            System.err.println(e.getMessage());
        }
        return font;
    }
}
You need to login to post a reply.