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

2017/4/19

Can't set a custom font

CubeGamer6 CubeGamer6

2017/4/19

#
Hi, i've tried everything but i can't seem to set a custom font with the solutions i've found recently. If i try the following code:
1
notification.setFont(new Font("Courier New", java.awt.Font.PLAIN, 12));
Greenfoot won't let me compile the code because 'incompatible types: java.lang.String cannot be converted to boolean'. I find this very confusing because i don't get why it tries to convert to a boolean. Can anyone show an example where the font can be changed? Here's the rest of the act() if necessary:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public void act()
    {
        World world = getWorld();
        currentTime=System.currentTimeMillis();
        if(currentTime>=timerBegin)
        {
            if(currentTime>=timer2Begin)
            {
                timer2Begin=timer2Begin+fadeSpeed;
                transparency=transparency-fadeStepsize;
                //this is a debugger, it does nothing else but display variables
                world.removeObjects(world.getObjects(Announcement.class));
                world.addObject (new Announcement("custom announcement", "-Terminal window-\n"+transparency),500,500);
                if(transparency<=0)
                {
                    transparency=0;
                    world.removeObject(this);
                }
            }
        }
        else
        {
            if(currentTime>=timer2Begin)
            {
                //this is a debugger, it does nothing else but display variables
                world.removeObjects(world.getObjects(Announcement.class));
                world.addObject (new Announcement("custom announcement", "-Terminal window-\n"+transparency),500,500);
                timer2Begin=timer2Begin+fadeSpeed;
                if(isTouching(Rocket.class))
                {
                    transparency=transparency-fadeStepsize;
                    if(transparency<100)
                    {
                        transparency=100;
                    }
                }
                else
                {  
                    transparency=transparency+fadeStepsize;
                    if(transparency>255)
                    {
                        transparency=255;
                    }
                }
            }      
        }
        GreenfootImage notification = new GreenfootImage("notificationboard.gif");
        notification.setTransparency(transparency);
        notification.setFont(new Font("Courier New", java.awt.Font.PLAIN, 12));
        notification.drawString(text, 30,30);
        setImage(notification);
    }
danpost danpost

2017/4/19

#
Are you importing java.awt.Font or using the greenfoot.Font class? It appears you may be mixing the two because you use 'java.awt.font.PLAIN' in your code. Try removing that parameter.
Yehuda Yehuda

2017/4/19

#
Change line 49 to:
1
notification.setFont(new Font("Courier New", 12));
There is no constructor for greenfoot.Font such as Font(String fontName, int style, in size), for setting the font for a GreenfootImage you have to use the proper font. You did use the proper font since what's required is a greenfoot.Font and that's what you used. The only constructor in Greenfoot that has three parameters is Font(boolean bold, boolean italic, int size) which means that you're putting a String in place of a boolean.
CubeGamer6 CubeGamer6

2017/4/20

#
thanks @Yehuda, that helped!
You need to login to post a reply.