我使用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());
}
}
答案 0 :(得分: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);
}
您每次都绘制一个新图像并将其设置为您的世界的背景。如果您现在想要更改某些内容,您可以调整lines-Array 中的值并调用函数“drawBackground()”。 更好的是:使用列表而不是数组。使用和调整值更容易。