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

2012/6/25

Drawing words on top of actors

gusbus123 gusbus123

2012/6/25

#
right now my score is drawn but only on the background. How would u make it draw on the actor that is in front and also resize what is being drawn? Right now i have this in my world :
public void showScore(String message)
    {
        GreenfootImage bg = getBackground();
        bg.setColor(Color.WHITE);
        Player dead = isPlayerDead();
        if(dead!=null) bg.drawString(message, 365, 350);
    }
and this is the part in the player class:
public void scoreMem()
    {
        SinglePlayer world1 = (SinglePlayer) getWorld();
        String newMessage = "Player 1: " + totalScore;
        world1.showScore(newMessage);
    }
gusbus123 gusbus123

2012/6/26

#
This works but only puts it on the background and very small. How would i expand the text and also place the text not on the background but in front of everything?
gusbus123 gusbus123

2012/6/26

#
???
davmac davmac

2012/6/26

#
You could place the text in front by drawing the text on an actor instead of on the background. The easiest way to create a text image of a certain size is using the GreenfootImage constructor. From the documentation: GreenfootImage(java.lang.String string, int size, java.awt.Color foreground, java.awt.Color background) Creates an image with the given string drawn as text using the given font size, with the given foreground color on the given background color.
gusbus123 gusbus123

2012/6/26

#
could u give me an example of a code for that? sorry i dont really get much of what it says in the documentations. I know some but a lot are a bit too different for me.
davmac davmac

2012/6/26

#
From the world class, roughly:
import java.awt.Color;  // at the top of the file!
and:
Actor actor = new TextActor();
addObject(actor, 100, 100);
actor.setImage(new GreenfootImage("a message", 12, java.awt.Color.BLACK, java.awt.Color.WHITE));
gusbus123 gusbus123

2012/6/26

#
k thx ill do that.
You need to login to post a reply.