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

2014/8/2

adding text

davemib123 davemib123

2014/8/2

#
I have created two classes. One for in-game text and another for the background images to be used. This is what I have so far: Text class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import greenfoot.*;
import java.awt.Color;
 
/**
 * Write a description of class Language here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
 
public class Text extends Tiles
{
    public String intro_text1 = "Storyline1 \n";
    public String intro_text2 = "Storyline2 \n";
    public String intro_text3 = "Storyline3";
    public String palletTown_Sign1 = "OAK POKéMON RESEARCH LABS";
    public String palletTown_Sign2 = "Pallet Town \n Shades of your journey await!";
     
    public Text(String text)
    {
        updateImage(text);
    }
 
    private void updateImage(String text)
    {
        Color txtColor = new Color(96, 96, 96);
        Color bgColor = new Color(0, 0, 0, 0);
        setImage(new GreenfootImage(text, 20, txtColor, bgColor));
    }
     
    public void setText(String text)
    {
        updateImage(text);
    }
}
Frames class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Message here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Frames extends Tiles
{
 
    public Frames(String selection)
    {
        this.selection = selection;
        this.directory = "Frames/";
 
        if (selection == "type1")
            setImage(directory + "type1.png");
        //         else if (selection == "type2")
        //             setImage(directory + "type2.png");
        //         else if (selection == "type3")
        //             setImage(directory + "type3.png");
    }
 
    public void act()
    {
        if (Greenfoot.isKeyDown("enter"))
        {
            getWorld().removeObject(this);
        }
    }
}
How would I get it so that when I add the text to the world, it picks up a specific background from the frames.class and the text shown is that which is defined at the top of text.class. My game is located here http://www.greenfoot.org/scenarios/11862
davemib123 davemib123

2014/8/3

#
I've attempted to add some text. I have a few areas that I need some help in: For one of the signs I have text aligned to centre, how do I get it aligned left? How do I get the text to appear word by word? For a multi-line text do I add it into an array?
davemib123 davemib123

2014/8/3

#
getting words to appear word by word - seems to be that you can use String.split. I have this:
1
2
3
4
5
String palletTown_Sign12 = "OAK POKéMON RESEARCH LABS";
        String[] splitText = palletTown_Sign12.split(" ");
        for (int i=0; i<splitText.length; i++) {
            System.out.println(splitText[i]);
        }
How do I get GreenfootImage using this?
danpost danpost

2014/8/3

#
Create one GreenfootImage for each of the 'split' words (maintain a 'widest' int field to track down the biggest image). Use the height of any one and multiply it by the length of the 'splitText' array and create the main GreenfootImage of size needed for the entire string. Draw the word images onto the main image along the left side.
davemib123 davemib123

2014/8/17

#
Right this is what I have managed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Text2 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Text2 extends Tiles
{
    String [] palletTown_Sign = {"OAK", "POKéMON", "RESEARCH", "LABS" }; 
    private boolean textHasShown;
    /**
     * Act - do whatever the Text2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if(!textHasShown) 
        
            for (int i = 0; i < palletTown_Sign.length; i++)   
            
                Text text = new Text(palletTown_Sign[i]);   
                getWorld().addObject(text, 45 + (i*70), 293);
                GreenfootImage img = new GreenfootImage(palletTown_Sign.length * 10, 30);
                img.clear();
                img.setColor(Color.BLACK); 
                img.drawString(palletTown_Sign[i], 45, 293); 
            
            textHasShown = true
        
 
    }   
}
It shows the text as separate images, but I find the second word is overlapped with the third word. How do I move it along as the word 3 and 4 have an adequate gap.
danpost danpost

2014/8/17

#
I have a new support class for GreenfootImage texts called TextImage Support Class. Maybe you should check it out. It allows you to programmatically draw text images using any font name, style and size, placing the images in consideration of their dimensions.
danpost danpost

2014/8/17

#
danpost wrote...
Create one GreenfootImage for each of the 'split' words (maintain a 'widest' int field to track down the biggest image). Use the height of any one and multiply it by the length of the 'splitText' array and create the main GreenfootImage of size needed for the entire string. Draw the word images onto the main image along the left side.
The following method should return an image of the given text, left-aligned:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private GreenfootImage getLeftAlignedTextImage(String text)
{
    if (text == null) return null;
    String[] splitText = text.split(" ");
    int imageCount = splitText.length;
    GreenfootImage[] images = new GreenfootImage[imageCount];
    int widest = 1;
    for (int i=0; i<imageCount; i++)   
    {
        images[i] = new GreenfootImage(palletTown_Sign[i], 20, null, null);
        if (images[i].getWidth() > widest) widest = images[i].getWidth();
    }
    GreenfootImage img = new GreenfootImage(20*imageCount, widest);
    for (int i=0; i<imageCount; i++)
    {
        img.drawImage(images[i], 0, 20*i);
    }
    return img;
}
You can add arguments for the colors (text and background) to use when creating the individual word images, if needed. You can also pass the font size. All this would already be set up for you within my support class, however (along with much more).
davemib123 davemib123

2014/8/18

#
thanks. i'll give it a go.
You need to login to post a reply.