1

I drew lines by using the method drawLine(),now I want to delete the lines,how can I do? I can't find any methods in Greenfoot API.PLEASE HELP!

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 
import java.awt.Color;
import java.lang.Thread;
/**
 * Write a description of class pen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class pen extends Actor
{
    /**
     * Act - do whatever the pen wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private TestWorld world;
    private GreenfootImage image;
    public pen()
    {

    }
    public void act() 
    {
        // Add your action code here.
        try{
            Thread.sleep(5000);
           }catch(Exception e){}
        world=(TestWorld)this.getWorld();
        image=world.getBackground();
        image.setColor(Color.RED);
        image.drawLine(0,0,getX(),getY());
    }    

}
user2002948
  • 63
  • 1
  • 1
  • 4

1 Answers1

0

You can do it like this:

public int[][] lines = {
    {10,10,100,20},
    {10,10,40,40}
};

public void drawBackground(){
    world=(TestWorld)this.getWorld();
   
    GreenfootImage img = new GreenfootImage(world.getWidth(), world.getHeight());
    for(int[] x : lines){
        img.setColor(new Color(10,255,20));
        img.drawLine(x[0],x[1],x[2],x[3]);
    }
    world.setBackground(img);
}

You draw a new image every time and set it as the background for your world. If you now want to change something you can adjust the values in the lines-Array and call the function "drawBackground()". Even better: use a List instead of an array. It's easier to work with and to adjust the values.

Bene
  • 1
  • 1
  • 3