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

2017/5/11

Update Text

MichaelMac MichaelMac

2017/5/11

#
Hey all, I've been trying to find a way to delete text that I have already written. I am using the drawString() method to write, and am looking for a way to remove what I have just written, I am aware of the clear() method, however, I am looking for something that doesn't remove the entire image, strictly the text. Thanks in advance.
Yehuda Yehuda

2017/5/11

#
One thing you can do is redraw the background image without redrawing the text so that everything will be there without the text, but I don't really like that idea. I think it would be simplest to have an actor with it's image set as the text (as opposed to the World background) then you can add and remove the actor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.Actor;
import greenfoot.GreenfootImage;
import greenfoot.World;
 
public class MyWorld extends World {
 
    private Actor textActor = new Actor() {
    };
 
    public MyWorld() {
        super(600, 400, 1);
        GreenfootImage text = new GreenfootImage(20, 20);
        text.drawString("Text", 0, 18);
        textActor.setImage(text);
        addObject(textActor, 100, 100);
    }
}
When you want to remove the text you can do:
1
removeObject(textActor);
danpost danpost

2017/5/11

#
Another option is to not use 'drawString', but instead create a GreenfootImage object with the text and use 'drawImage'. With this way you can acquire the dimensions of the area drawn and save that portion of the background to replace later when you want to remove the text:
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
import greenfoot.*;
 
public class MyWorld extends World
{
    GreenfootImage textArea;
    final int textX = 50, textY = 50;
     
    public MyWorld()
    {
        super(600, 400, 1);
        showText("Text");
    }
     
    // method to show, or replace, a text image
    public void showText(String text)
    {
        removeTextImage(); // so this method can replace any previous text image shown
        GreenfootImage textImage = new GreenfootImage(text, 28, null, null);
        textArea = new GreenfootImage(textImage); // textImage used only to size the area
        textArea.drawImage(getBackground(), -textX, -textY); // replaces image with affected background area (to be retained)
        getBackground().drawImage(textimage, textX, textY); // draws text image on background image
    }
 
    // method to remove text image (replace affected background)
    public void removeTextImage()
    {
        if (textArea != null) getBackground().drawImage(textArea, textX, textY);
    }
Yehuda Yehuda

2017/5/12

#
danpost wrote...
save that portion of the background to replace later when you want to remove the text
I've never seen that done before. Line 18 would throw a NullPointerException with Greenfoot Version 3.1.0. It would have to be:
1
GreenfootImage textImage = new GreenfootImage(text, 28, Color.BLACK, new Color(0, 0, 0, 0));
The 'i' on line 21 should be capital (just a noticed typo). With using that GreenfootImage constructor the font is not specifiable. drawString can still be used, just onto textImage as opposed to getBackground().
danpost danpost

2017/5/12

#
Yehuda wrote...
With using that GreenfootImage constructor the font is not specifiable. drawString can still be used, just onto textImage as opposed to getBackground().
Unfortunately, when using drawstring, you cannot acquire the dimensions of the area drawn; so, determining the size of the image to draw on is pretty much guesswork. That is one reason why I created my TextImage Support Class; so you can create a Greenfootimage text object with a specified font, size and style.
Yehuda Yehuda

2017/5/12

#
danpost wrote...
Unfortunately, when using drawstring, you cannot acquire the dimensions of the area drawn; so, determining the size of the image to draw on is pretty much guesswork.
It is possible to check the width and height of a string with a specific font but it's not so simple - I'll get back on that. Now I see that you've done that in the TextImage class - so you should agree that it's possible to "acquire the dimensions of the area drawn".
That is one reason why I created my TextImage Support Class; so you can create a Greenfootimage text object with a specified font, size and style.
I can't use that class (anymore) since I now have Greenfoot Version 3.1.0 (The comments there from Nosson1459 were posted by me.)
danpost danpost

2017/5/12

#
Yehuda wrote...
It is possible to check the width and height of a string with a specific font but it's not so simple - I'll get back on that. Now I see that you've done that in the TextImage class - so you should agree that it's possible to "acquire the dimensions of the area drawn".
Yes, I agree. I should have said 'cannot easily acquire' them. For a novice, it is virtually impossible -- so, not far from the truth.
Yehuda Yehuda

2017/5/12

#
danpost wrote...
Yes, I agree. I should have said 'cannot easily acquire' them. For a novice, it is virtually impossible -- so, not far from the truth.
I guess so. The following method can be used to get the dimensions of text (I just created it myself, I wasn't sure what to put as the FontRenderContext or what to use for the Graphics2D):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * This method returns a <code>java.awt.Dimension</code> object with the width and
 * height of the specified text <i>drawn</i> with the specified <code>Font</code>.
 * <p>
 * The following line of code is how to get the width of the text with the font and
 * store it in an int variable:
 * {@code int stringWidth = getTextDimensions("String", new java.awt.Font("Arial", java.awt.Font.ITALIC, 18)).width;}
 *
 * @param text the text to get the dimensions for
 * @param font the font that is being used to draw the text - different fonts have
 *             different dimensions
 *
 * @return the dimensions of the given text drawn with the specified font
 *
 * @see java.awt.Dimension#width
 */
public static java.awt.Dimension getTextDimensions(String text, java.awt.Font font) {
    java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(1, 1, java.awt.image.BufferedImage.TYPE_INT_RGB);
    java.awt.Graphics2D g2D = (java.awt.Graphics2D) image.getGraphics();
    g2D.setFont(font);
    java.awt.geom.Rectangle2D stringRect = font.getStringBounds(text, g2D.getFontRenderContext());
    return new java.awt.Dimension((int) stringRect.getWidth(), (int) stringRect.getHeight());
}
You need to login to post a reply.