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

2013/5/27

Setting an own font?

Solringolt Solringolt

2013/5/27

#
How can I change the default font of any text written in Greenfoot?
bourne bourne

2013/5/27

#
Something like:
import java.awt.Font;
...
GreenfootImage myImage = ...;
...
myImage.setFont(new Font("Courier New", Font.PLAIN, 20));
danpost danpost

2013/5/27

#
@bourne, I think what was meant was how can the DEFS be changed so that Greenfoot will automatically use a different font, instead of the one that currently is the default.
bourne bourne

2013/5/27

#
The Font of the code editor? You can change the size from preferences... But no given options to change the other aspects to it. Don't see why you would need to change the Font other than the size. I believe it uses Courier New - I nice uniform width size across characters - making it great for code.
bourne bourne

2013/5/27

#
Here I found where the default is stored (on a mac): /Applications/Greenfoot 2.2.1/Greenfoot.app/Contents/Resources/Java/greenfoot.defs Little less than 3/4 the way down: - Change the line # The editor font. This is the name of a font face with an optional # "-bold" at the end. Most used are monospaced fonts, such as Courier. # Good large fonts for screen projections are SansSerif-bold, 14pt, or # Courier-bold, 18pt. Only the font face is specified here, the font # size is specified in the BlueJ preference dialogue. Some examples: bluej.editor.font=Monospaced bluej.editor.MacOS.font=Monaco # bluej.editor.font=Monospaced-bold # bluej.editor.font=SansSerif # bluej.editor.font=SansSerif-bold # bluej.editor.font=Arial-bold
Solringolt Solringolt

2013/5/27

#
I wanted just to change for one case, for example in this code where should I put your code to get the number in my specified font ?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;  
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    private int highest;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        
        this.prefix = prefix;
        updateImage();
        
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
       GreenfootImage image = new GreenfootImage(background);
       GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
       if (text.getWidth() > image.getWidth() - 20)
       {
           image.scale(text.getWidth() + 20, image.getHeight());
       }
        
       image.drawImage(text,(image.getWidth()-text.getWidth())/2,(image.getHeight()-text.getHeight())/2);
       setImage(image);
    }
     
    public void setHighest()  
    {  
        if (target> highest) highest = target;
    }
     public int getHighest()  
    {  
        return highest;  
    }
}
bourne bourne

2013/5/27

#
Sorry my code works with GreenfootImage's drawString method. Its constructor that takes a String, size, and Colors is somewhat limited.
Solringolt Solringolt

2013/5/27

#
knowing I only have to show a number, could I change my code to use the font method?
danpost danpost

2013/5/27

#
You can import the Font class and then drawString text to images after 'setFont'. Example follows:
// import Font class
import java.awt.Font;
// create text String
String text = "Text for image here.  This is one hellaciously long string of text for an image.";
// create GreenfootImage
GreenfootImage image = new GreenfootImage(10*text.length()+20, 20);
// set Font for image
image.setFont(new Font(Font.MONOSPACED, Font.BOLD, 16));
// draw string on image
image.drawString(text, 11, 16);
You need to login to post a reply.