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

2014/3/17

Adding Text

gdwd3 gdwd3

2014/3/17

#
I am trying to make a black image generate a random number between 1 and 99 when the space bar is pressed. It should then display the number that has been generated on the image. This is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class NumberGenerator here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class NumberGenerator extends Actor
{
    public int number;
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    
    /**
     * Act - do whatever the NumberGenerator wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        GenerateNumber();
    }    
    
    public void GenerateNumber()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            number = Greenfoot.getRandomNumber(99)+1;
            setValue(number);
            Greenfoot.delay(5);
        }
    }
    
    
    
    
    
    
    //-------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
     /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
This is the error I get:
java.lang.NullPointerException
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:140)
	at NumberGenerator.updateImage(NumberGenerator.java:75)
	at NumberGenerator.setValue(NumberGenerator.java:67)
	at NumberGenerator.GenerateNumber(NumberGenerator.java:32)
	at NumberGenerator.act(NumberGenerator.java:24)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
Please help. Thanks in advance!
danpost danpost

2014/3/17

#
The reason for the error is that you never set the 'background' field to any image. You should probably have a constructor method to do this. I cannot tell if you are using this class properly or not. If I am incorrect on any of the following, let me know: (a) you only ever create one NumberGenerator object; (b) you want to have the displayed number changing quickly while the 'space' key is held down (c) the 'background' image should be a blank image (totally transparent) I do not think that some of the methods should be in the class (specifically, 'add' and 'setValue'); nor the 'target' field.
actinium actinium

2014/3/17

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Class NumberDisplayer here.
 * 
 * @author (AA) 
 * @version (1.00)
 */
public class NumberDisplayer extends Actor
{
    private GreenfootImage me;
    private int number;
    
    public NumberDisplayer(){
        me = new GreenfootImage(50,50);    
        drawBlankImage();
        number=0;
    }
    
    public void act() 
    {
        checkKey();
    }
    
    private void checkKey(){
        if (Greenfoot.isKeyDown("space")){
            number=setRandom();
            drawBlankImage();
            drawRandomNumber();
        }
    }
    
    private int setRandom(){
        return Greenfoot.getRandomNumber(99)+1;
    }
    
    private void drawRandomNumber(){
        me.setColor(Color.RED);
        me.drawString(" "+number,10,10);
        setImage(me);
    }
    
    private void drawBlankImage(){
        me.setColor(Color.BLACK);
        me.fill();
        setImage(me);
    }
}
I would do it like this. Remember yoou need to redraw the box each time you add a random number.
danpost danpost

2014/3/17

#
The way actinium shows would work for everything except the font size and colors; and there is no way to get the current number from the object. Until you resolve the 'background' image issue, I suggest the following:
import greenfoot.*;
import java.awt.Color;

public class NumberGenerator extends Actor
{
    private int number;

    public NumberGenerator()
    {
        updateImage();
    }
    
    public void act() 
    {
        if(Greenfoot.isKeyDown("space"))
        {
            number = Greenfoot.getRandomNumber(99)+1;
            updateImage();
            Greenfoot.delay(5);
        }
    }

    public int getNumber()
    {
        return number;
    }

    private void updateImage()
    {
        setImage(new GreenfootImage(""+number, 22, Color.black, null));
    }
}
gdwd3 gdwd3

2014/3/18

#
Ok it works with your code danpost. Thanks But how would i now increase the font size?
actinium actinium

2014/3/18

#
// First you need the import statement 
import java.awt.Font;

//then you can create a statement like this
//to create a font
Font font = new Font("Comic sans",Font.BOLD,20);
//then you can attach ther font to your image
//with the followwing
image.setFont(font);
danpost danpost

2014/3/18

#
gdwd3 wrote...
Ok it works with your code danpost. Thanks But how would i now increase the font size?
In line 30 of my code above, change the '22', which is the font size of the text for the image, to whatever font size you want.
gdwd3 gdwd3

2014/3/18

#
OK thanks for your help. :D
You need to login to post a reply.