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

2018/3/30

Is there a way to center the text?

Recorsi Recorsi

2018/3/30

#
Hello, this is the code to set up my score counter:
1
2
3
4
5
6
7
8
GreenfootImage img = new GreenfootImage(500, 100);
img.setColor(new Color(0, 0, 0, 0));
img.fill();
img.setColor(Color.WHITE);
img.setFont(new Font("Edit Undo Line BRK", false, false , 95));
img.drawString(""+hitscore, 10, 90);
img.scale(300, 75);
setImage(img);
and this is the code to place it in the world:
1
2
3
4
private void prepareHitCounter()
    {
       addObject(hitcounter, 579, 83);
    }
If the score counter is below 10 it is centered, but if it goes above 10 it is slightly moved to the right. Is there a way to fix this? Thanks
danpost danpost

2018/3/30

#
Recorsi wrote...
If the score counter is below 10 it is centered, but if it goes above 10 it is slightly moved to the right. Is there a way to fix this?
How do you want it -- so it extends both left and right equally or so it moves slightly to the left? (to put it more appropriately, are you saying it is left-adjusted and you want it either center-adjusted or right-adjusted) I will presume since you place the actor to the right side of your world that you would want it right-adjusted (so it extends left when more characters are added to the text displayed. You cannot easily get the length of text that is drawn onto an image. So, you might consider using the text image constructor GreenfootImage(String, int, Color, Color) and assign that to the actor displaying the text. Then when updating the text, it can compare the current text width with the updated one and shift its location when needed (by half the difference between the two widths).
Recorsi Recorsi

2018/3/30

#
Thanks for your answer, yes i want the text to be extended to the left and the right equally(so that the middle of the text is aligned with the middle of the x-axis) How exactly would i use the image constructor GreenfootImage(String, int, Color, Color)?
danpost danpost

2018/3/30

#
You would use something like this:
1
setImage(new GreenfootImage(""+hitscore, 60, Color.WHITE, new Color(0, 0, 0, 0)));
Unfortunately, however, you will not be able to set the font to your choosing this way. You can still scale this image, if desired (or just change the font size from 60 to some other value).
Recorsi Recorsi

2018/4/1

#
Okay, nevermind i fixed it like this now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void centerScore()
  {
      if (hitscore > 9 && hitscore < 20)
      {
          setLocation(562, getY());
      }
      if (hitscore > 20 && hitscore < 100)
      {
          setLocation(562, getY());
      }
      if (hitscore > 110 && hitscore < 120)
      {
          setLocation(573, getY());
      }
      if (hitscore >= 120 && hitscore < 200)
      {
          setLocation(565, getY());
      }
      if (hitscore > 200)
      {
          setLocation(546, getY());
      }
  }
You need to login to post a reply.