我用drawLine()方法画了线,现在我想删除线,该怎么办?我在Greenfoot API.PLEASE帮助中找不到任何方法!
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());
}
}发布于 2020-12-20 19:41:18
你可以这样做:
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);
}您每次都会绘制一个新图像,并将其设置为您的世界的背景。如果您现在想要更改某些内容,可以调整-array行中的值并调用函数"drawBackground()“。更好的做法是:使用列表而不是数组。它更容易使用和调整这些值。
https://stackoverflow.com/questions/15870498
复制相似问题