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

2017/2/4

Text written to image with DrawString has white outline?

mehanix mehanix

2017/2/4

#
I am making a timer and i want it to be a specific gray color:
1
2
3
4
5
6
7
8
9
10
11
12
public void update(int timeSpent)
{
    GreenfootImage img = getImage();
    img.clear();
    Color gray = new Color(333333);
    img.setColor(gray);
    img.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
    String t = String.valueOf(timeSpent);
    digitNumber=t.length();
     
    img.drawString(t, (834-768)/2-fontSize*(digitNumber)/2, 20);
}
However. the numbers show up with a weird, unsightly white outline: What can I do? Am I doing something wrong when setting the color? It's not just gray, any color does this, except white.
Super_Hippo Super_Hippo

2017/2/4

#
I just tried it with this code: (version 3.1.0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
 
public class World1 extends World
{
    public World1()
    {   
        super(40, 30, 1);
        GreenfootImage img = new GreenfootImage(40, 30);
        img.clear();
        img.setColor(Color.GRAY);
        img.fill();
        img.setColor(Color.BLACK);
        img.setFont(new Font("Times New Roman", false, false, 20));
        img.drawString("183", (834-768)/2-20*(3)/2, 20);
        setBackground(img);
    }
}
Results in this: I don't see this problem here. I can't test it with this line 5 though in the new version. Try it with setting a background and then (lines 10+11) and then write your numbers on it and report back if anything changed. (Not that it would matter, but I don't really understand your calculation for the x-position.)
mehanix mehanix

2017/2/4

#
Thank you for this workaround! I changed a few things(most notably, the background gray color as it didn't fit with the rest of the HUD i'm working on) and it's working as intended. The x-position calculation is just a bit weird, i was trying to figure out how to make the timer always be centered even when switching from a number with fewer digits from a number with more (99 to 100 for example, or 999 to 1000). That calculation just pushes the new digit back instead, but it's better than nothing.
danpost danpost

2017/2/4

#
mehanix wrote...
The x-position calculation is just a bit weird, i was trying to figure out how to make the timer always be centered even when switching from a number with fewer digits from a number with more (99 to 100 for example, or 999 to 1000). That calculation just pushes the new digit back instead, but it's better than nothing.
That is one of the drawbacks. Using 'drawString', you do not know the exact size of the image being created; and using 'new GreenfootImage(String, int, Color, Color)', the image is made before you can set the font. I wrote an extention of GreenfootImage that would be useful here. It is my TextImage class. You can find it here.
mehanix mehanix

2017/2/4

#
Thanks! However, i decided to stick with the drawText() method which fixes this at the cost of having the timer look slightly out of place.
You need to login to post a reply.