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

2015/11/30

\n

Vellyxenya Vellyxenya

2015/11/30

#
Hi I'm trying the following code that makes the objet show the text letter by letter until the sentence is completed
String writing = "";
    int i = 0;
    public void writeText(String text)
    {
        char[] letters = text.toCharArray();
        int length = text.length();
        if(i<length)
        {
            writing+= letters[i];
            i++;
        }
        GreenfootImage write = new GreenfootImage(writing, 24, Color.GREEN, new Color(0,0,0,0));
        image.drawImage(write, 15, 0);
    }
There are 2 problems: 1) the \n works but the following text is written in the middle of the image (i want it written on the left) 2) when the 2nd line is completed it begin writing on the 1st line again... This is the code in my World class:
addObject(new Informations2("Ceci est un test \n il y a ici un retour \n à la ligne", 250, 150), 300, 300);
Thank you!!
Game/maniac Game/maniac

2015/11/30

#
import java.wt.Font;
image.setColor(Color.GREEN);
image.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 24));
image.drawText(writing, 15, 0);
danpost danpost

2015/11/30

#
Please show the entire Informations2 class code. Something really seems off with the method given. One thing is that if you want the text to show "letter by letter", then that is something the 'act' method should be doing. It would seem more appropriate to have a 'setText' method, rather than a 'writeText' method.
Vellyxenya Vellyxenya

2015/11/30

#
Thank you very much, it works perfectly (the method is "drawString" btw) :D
Vellyxenya Vellyxenya

2015/11/30

#
Yes this method is in the act method. Il redraws the image each act with an additional letter. Here's the entire class:
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class Informations2 extends SmoothMover
{
    int WIDTH;
    int HEIGHT;
    GreenfootImage image;
    int width = 10;
    String text;
    
    String writing = " ";
    int i = 0;

    public Informations2(String text, int width, int height)
    {
        this.text = text;
        this.WIDTH = width;
        this.HEIGHT = height;
        image = new GreenfootImage(width, height);
        image.setTransparency(250);
        drawBackground(0, this.width, Color.GREEN);
        drawBackground(4, this.width, Color.BLACK);
        setImage(image);
    }

    public void act() 
    {
        writeText(text);
        setImage(image);
    }

    public void drawBackground(int space, int width, Color color)
    {
        image.setColor(color);
        image.fillOval(space+0, space+0, width, width);
        image.fillOval(space+0, image.getHeight()-width-space, width, width);
        image.fillOval(image.getWidth()-width-space, space+0, width, width);
        image.fillOval(image.getWidth()-width-space, image.getHeight()-width-space, width, width);
        image.fillRect(space+width/2, space+0, WIDTH-width-2*space, HEIGHT-2*space);
        image.fillRect(space+0, space+width/2, WIDTH-2*space, HEIGHT-width-2*space);
    }

    public void writeText(String text)
    {
        char[] letters = text.toCharArray();
        int length = text.length();
        if(i<length)
        {
            writing+= letters[i];
            i++;
        }
        image.setColor(Color.GREEN);
        image.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
        image.drawString(writing, 15, 35);
    }

}
danpost danpost

2015/11/30

#
Vellyxenya wrote...
Yes this method is in the act method. Il redraws the image each act with an additional letter.
Okay. It just seemed a bit string to have the 'text' string passed to the method. This is something you do not need to do as the 'text' field is a member of the class and is readily accessible to other members within the class. In other words, the method can be declared as:
private void writeText()
and called from the 'act' method by
writeText();
It is also not needed to set the image to the actor in the 'act' method. You are not changing the image -- only modifying it (with the current code). Now, from what I could tell, the 'drawString' method does not create new lines with the "\n" (new line) escape sequence. However, you indicated above that it was working perfectly. If you are still having some issues, please post back and explain them.
You need to login to post a reply.