1

我用drawLine()方法画线,现在想删除线,怎么办?我在Greenfoot API中找不到任何方法。请帮助!

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());
    }    

}
4

1 回答 1

0

你可以这样做:

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);
}

您每次都绘制一个新图像并将其设置为您的世界的背景。如果您现在想要更改某些内容,您可以调整行数组中的值并调用函数“drawBackground()”。更好的是:使用 List 而不是数组。使用和调整值更容易。

于 2020-12-20T11:41:17.657 回答