if I write this:
can I make it like it never happened ( remove it ) ?
1 | getBackground().drawString( "hi" , getWidth() / 2 , getHeight() / 2 ); |
1 | getBackground().drawString( "hi" , getWidth() / 2 , getHeight() / 2 ); |
1 2 3 4 5 6 7 | // field GreenfootImage savedBG = null ; // draw the string savedBG = new GreenfootImage(getBackground()); getBackground().drawString( "hi" , getWidth() / 2 , getHeight() / 2 ); // remove the string setBackground(savedBG); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // 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); |
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.*; 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(); } } |
1 2 | getImage().clear(); getImage().drawImage( new GreenfootImage( "filename" ), 0 , 0 ); |
1 | public void rename(String string); |
1 2 3 4 5 6 7 | public Text text; // following is in the contructer text = new Text(); // this in a method text.rename( "hi" ); |