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

2012/10/22

Need help on setting bold text.

Jrm1715 Jrm1715

2012/10/22

#
I am probably either doing something wrong or I am missing something very obvious. But here is my code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Road here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Road extends World
{
    /**
     * Constructor for objects of class Road.
     * 
     */
    public Road()
    {    
        super(700, 700, 1); 
        showMessage();
        prepare();
    }

 private void showMessage()
    {
        GreenfootImage bg = getBackground();
        bg.setColor(Color.BLACK);
        bg.setFont(Font.BOLD);
        bg.drawString ("press 'w' to go forward and 'd' and 'a' to turn right and left.", 240, 350);
    }
}
When compiled I get this error "method setFont in class greenfoot.GreentootImage cannont be applied to given types; required: java.awt.font found: int reason: actual argument int cannot be converted to java.awt.Font by method invocation conversion. any help is appreciated.
danpost danpost

2012/10/22

#
'setFont' needs a Font object not a font style. You need to use 'deriveFont(fontStyle)' on a Font object and then set that Font object as the new font. Line 28
bg.setFont(bg.getFont().deriveFont(Font.BOLD));
Upupzealot Upupzealot

2012/10/22

#
Change Line 28: bg.setFont(new Font("Ariel", 12, Font.BOLD)); The first param "Ariel" is the name of your font, the second param is the size, last the style.
Upupzealot Upupzealot

2012/10/22

#
Oh! @danpost Again it's you!
davmac davmac

2012/10/22

#
"Ariel" is not a font family (at least not one I'm familiar with), I think you meant "Arial"?
Jrm1715 Jrm1715

2012/10/22

#
Thank you.
Jrm1715 Jrm1715

2012/10/23

#
next question. How might I go about removing that string once the user gives some kind of input like pressing the 'W' key or any key? This is what came to me while trying to accomplish this by my self.
private void showMessage()
    {
        if (Greenfoot.isKeyDown("w") == true)
        {
            GreenfootImage bg = getBackground();
            bg.setColor(Color.BLACK);
            bg.setFont(bg.getFont().deriveFont(Font.BOLD, 14));        
            bg.drawString("", 170, 350);                             
        }
        else
        {             
            GreenfootImage bg = getBackground();
            bg.setColor(Color.BLACK);
            bg.setFont(bg.getFont().deriveFont(Font.BOLD, 14));        
            bg.drawString("Press 'W' to go forward and 'D' and 'A' to turn right and left.", 170, 350);              
        }
    }
The above code obviously does not work the way I want it too and I am not 100% sure why.
Upupzealot Upupzealot

2012/10/23

#
My bad! I spell it wrong!
Upupzealot Upupzealot

2012/10/23

#
Then you need some "clear" method. If "bg" is blank, just call "bg.clear" is OK. If "bg" is an image, you should draw the image onto "bg" again, to cover(clear) what you'v drawn before. The "clear" method should be called before you draw anything.
danpost danpost

2012/10/23

#
If the background has a flat color where you display the message, you can change the draw color to that of the background and re-draw the same string to remove it. If the background is not a flat color, then you either must save the image area where the string is drawn and re-draw the image to remove it, or (and this would probably be easier) just use an actor object to display the message.
// add a new sub-class of Actor called Instruct
// whose constructor receives a String parameter (the message)
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class Instruct extends Actor
{
    public Instruct(String message)
    {
        GreenfootImage img = new GreenfootImage(420, 20);
        img.setColor(Color.BLACK);
        img.setFont(img.getFont().deriveFont(Font.BOLD, 14));
        img.drawString(message, 5, 17);
        setImage(img);
    }
}
// add the object from the world class with
addObject(new Instruct("Press 'W' to go forward and 'D' and 'A' to turn right and left."), 300, 300);
If you want, you can add an act() method to the Instruct class to catch specific keys down and remove the object when a key is found down.
public void act()
{
    boolean removeMe = false;
    if (Greenfoot.isKeyDown("a")) removeMe = true;
    if (Greenfoot.isKeyDown("d")) removeMe = true;
    if (Greenfoot.isKeyDown("w")) removeMe = true;
    if (removeMe) getWorld().removeObject(this);
}
danpost danpost

2012/10/23

#
The beauty of using an actor object to display the message is that it is re-usable. You can add a new object with a different message later (just adjust the x-location to center the message) and when one of the keys are pressed, it goes away.
Jrm1715 Jrm1715

2012/10/23

#
Thank you. Makes a lot more sense to just use the actor object. Appreciate the help!
You need to login to post a reply.