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

2018/10/25

Help with Fonts

Chipley Chipley

2018/10/25

#
I am making a counter and am trying to set the font but the name i made for the font is saying java.awt.Font cannot be converted to greenfoot.Font, here's the code for the counter:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    public Score()
    {
        GreenfootImage newImage = new GreenfootImage(100, 50);
        setImage(newImage);
    }
    
    public void setScore(int score)
    {
        GreenfootImage newImage = getImage();
        newImage.clear();
        
        Font f = new Font("Comic Sans MS", Font.PLAIN, 32);
        newImage.setFont(f);
        
        Color c = new Color(127, 127, 127, 127);
        
        newImage.setColor(c);
        newImage.fill();
        newImage.setColor(Color.BLACK);
        newImage.drawString("Score: " + score, 30, 30);
        setImage(newImage);
    }
}
nccb nccb

2018/10/29

#
In more recent versions of Greenfoot, we no longer use java.awt.Font and instead have our own greenfoot.Font class. Remove the line near the top that says "import java.awt.Font". Then change line 22 in the snippet to be new Font("Comic Sans MS", 32) without the Font.PLAIN part. That should get the code working.
You need to login to post a reply.