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

2018/4/21

I can't set the Font

DavranbekRozmetov DavranbekRozmetov

2018/4/21

#
Hello there! I was trying to set a font to my text on Greenfoot like
1
txt.setFont(new Font("Helvetica", Font.PLAIN, 12));
and it shows an error like
Incompatible types: java.awt.Font cannot be converted into greenfoot.Font
what am I doing wrong here. Thank you for your answer in advance!
Super_Hippo Super_Hippo

2018/4/21

#
Don't import java.awt.
danpost danpost

2018/4/21

#
I assume your txt field holds a GreenfootImage object. The setFont method of the GreenfootImage class requires a greenfoot.Font object, not a java.awt.Font object.
DavranbekRozmetov DavranbekRozmetov

2018/4/22

#
1
2
3
4
5
6
7
GreenfootImage txt = new GreenfootImage(x, y);
        txt = new GreenfootImage("...",80, new Color(17, 17 ,17), new  Color(255,100,100,0));
        setRotation(90);
        txt.setFont(new Font("Helvetica"12));
        img.drawImage(txt, 0, 0);
         
        setImage(img);
this is all I have and i did not import anything else except greenfoot.*; but seems it is not working
danpost danpost

2018/4/22

#
DavranbekRozmetov wrote...
<< Code Omitted >> this is all I have and i did not import anything else except greenfoot.*; but seems it is not working
You are creating the image with the text, so it is already drawn before you set any font. The drawImage line (line 5) only copies the txt image, as is, onto your img image, whatever it is. The set font is not involved with it. You should instead be creating an image of given size, large enough to contain the ellipsis (using the GreenfootImage(int width, int height) constructor, and then (1) set fill color and fill; (2) set text color and font to use drawString to apply the "..." to the image; (3) set image to actor.
DavranbekRozmetov DavranbekRozmetov

2018/4/23

#
It is working now. Thank you very much :)
You need to login to post a reply.