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

2012/9/21

drawString() problems

1
2
erdelf erdelf

2012/9/21

#
if I write this:
getBackground().drawString("hi", getWidth() / 2, getHeight() / 2);
can I make it like it never happened ( remove it ) ?
danpost danpost

2012/9/21

#
The solve is easy when the background color where you draw the string is a flat color (just set the color to the color of the background where you drew the string and redraw the string). If the background color is NOT a flat color, then there are at least two possible ways to solve it. The first, and easier way (if you can get away with it -- that is, if this was the only change to the background) is:
// field
GreenfootImage savedBG = null;
// draw the string
savedBG = new GreenfootImage(getBackground());
getBackground().drawString("hi", getWidth() / 2, getHeight() / 2);
// remove the string
setBackground(savedBG);
If the above will not do, then save the image of the background where you draw the string (instead of the whole background image) and then redraw the image to remove it. You will want to, instead of drawing your string, create a new GreenfootImage for the string and draw that image onto the background so you can acquire the width and height of the image.
// import
import java.awt.Color;
// fields
GreenfootImage savedImage = null;
int savedImageX, savedImageY;
// draw "hi" onto background
//   step 1:  create the text image
int fontsize = 16;
Color txtColor = Color.black;
Color transparent = new Color(0, 0, 0, 0);
GreenfootImage txtImage = new GreenfootImage("hi", fontsize, txtColor, transparent);
//   step2:  save the background where the image will be
savedImage = new GreenfootImage(txtImage.getWidth(), txtImage.getHeight());
savedImageX = getWidth() / 2 - savedImage.getWidth() / 2;
savedImageY = getHeight() / 2 - savedImage.getHeight() / 2;
savedImage.drawImage(getBackground(), -savedImageX, -savedImageY);
//   step 3:  draw the image on the background
getBackground().drawImage(txtImage, savedImageX, savedImageY);
// remove the string from background
getBackground().drawImage(savedImage, savedImageX, savedImageY);
Of course, you could create an actor to display the text so you can remove it easily.
erdelf erdelf

2012/9/21

#
the first two options doesn't work for me. the third doesn't work, too. It seems that I have to use an actor.
Gevater_Tod4711 Gevater_Tod4711

2012/9/21

#
I think it should work if you use a GreenfootImage (maybe not by using getBackground, this could be the problem too) and overdraw the old image. So instead of setImage(image) you use drawImage(image, 0, 0);
erdelf erdelf

2012/9/21

#
I tried it, doesnt work
Gevater_Tod4711 Gevater_Tod4711

2012/9/21

#
another idea would be to clear the image before overwriting it. I'm not shure if this will work. You still have to try.
erdelf erdelf

2012/9/21

#
no this wont work, tried it.
danpost danpost

2012/9/21

#
Works fine for me! You must be having problems with your computer! Test it out: create a new scenario 'drawString test', create a sub-class of world 'Backing' and insert the code below in the 'Backing' class (it is exactly the code I supplied above except that the size of the font is 24 instead of 16).
import greenfoot.*;
import java.awt.Color;

public class Backing extends World
{
    GreenfootImage savedImage = null;
    int savedImageX, savedImageY;

    public Backing()
    {    
        super(600, 400, 1);
        drawText("hi");
    }

    private void drawText(String text)
    {
        int fontsize = 24;
        Color txtColor = Color.black;
        Color transparent = new Color(0, 0, 0, 0);
        GreenfootImage txtImage = new GreenfootImage(text, fontsize, txtColor, transparent);
        savedImage = new GreenfootImage(txtImage.getWidth(), txtImage.getHeight());
        savedImageX = getWidth() / 2 - savedImage.getWidth() / 2;
        savedImageY = getHeight() / 2 - savedImage.getHeight() / 2;
        savedImage.drawImage(getBackground(), -savedImageX, -savedImageY);
        getBackground().drawImage(txtImage, savedImageX, savedImageY);
    }
    
    public void act()
    {
        if (Greenfoot.getKey() == null) return;
        getBackground().drawImage(savedImage, savedImageX, savedImageY);
        Greenfoot.stop();
    }
}
Pressing any key after starting the scenario will (for me --should for you) remove the text and stop the scenario.
erdelf erdelf

2012/9/21

#
I trust your code in general, but I am using a background which makes every drawn Image so, that it cant be seen
danpost danpost

2012/9/21

#
I guess I am a little confused as to what you are trying to do. But, if you have a handle on it, OK. If not, try to explain exactly what you want to happen, what the obstacles are that you are experiencing, and supply a code sample of what you have tried. I am sure you know, that the more information you provide, the better armed we are at supplying a workable solution.
Gevater_Tod4711 Gevater_Tod4711

2012/9/22

#
Ok I just got one last idea: first clear the image, then load the new Image from file and overdraw the image this way. If you clear the image there should be nothing. If then in the image you use to overwrite is the same string it seems like nothing has happend (however the string got in this image). But if you get the new Image from file there shouldn't be any strings (I hope). You should not save the image as a variable. Only draw it like this:
getImage().clear();
getImage().drawImage(new GreenfootImage("filename"), 0, 0);
I realy hope this will help you.
erdelf erdelf

2012/9/22

#
no sry.
Gevater_Tod4711 Gevater_Tod4711

2012/9/22

#
It also doesn't work??? I realy don't get why. Sorry then I cant help you.
erdelf erdelf

2012/9/22

#
I know this isnt the main topic of this discussion, but now I decided to use an actor, but now I have the following problem. in class Text:
public void rename(String string);
and this in world:
public Text text;

// following is in the contructer
 text = new Text();

// this in a method
text.rename("hi");
error code is: cannot find symbol - method rename(java.lang.String);
danpost danpost

2012/9/22

#
Try this
public void rename(String string) { }
There are more replies on the next page.
1
2