如何添加圖紙ii Japplet而不是將其替換爲新的圖紙?我正在使用repaint(),但它所做的只是替換我現有的矩形。什麼是正確的代碼添加另一個矩形,而不是取代它?如何在Java中的JApplet中添加並不重繪圖紙?
說我有這樣的代碼:(此代碼是不是我的)
import java.applet.Applet;
import java.awt.*;
public class MoveBox extends Applet
{
private int x = 20, y = 20;
private int width = 10, height = 10;
private int inc = 5;
private Color myColor = Color.red;
private Button up = new Button("Up");
private Button down = new Button("Down");
private Button left = new Button("Left");
private Button right = new Button("Right");
private Button increase = new Button("[+]");
private Button decrease = new Button("[-]");
// init method
public void init()
{
Panel buttons = new Panel();
buttons.setLayout(new FlowLayout());
buttons.add(up);
buttons.add(down);
buttons.add(left);
buttons.add(right);
buttons.add(increase);
buttons.add(decrease);
setLayout(new BorderLayout());
add("South", buttons);
}
// public methods
public boolean action(Event e, Object o)
{
if (e.target == up)
return handleUp();
else if (e.target == down)
return handleDown();
else if (e.target == left)
return handleLeft();
else if (e.target == right)
return handleRight();
else if (e.target == increase)
return handleIncrease();
else if (e.target == decrease)
return handleDecrease();
else
return super.action(e, o);
}
public void paint(Graphics g)
{
g.setColor(myColor);
g.fillRect(x,y,width,height);
}
// private methods
private boolean handleUp()
{
y = y - inc;
repaint();
return true;
}
private boolean handleDown()
{
y = y + inc;
repaint();
return true;
}
private boolean handleRight()
{
if (x < size().width)
x = x + inc;
else
x = 0;
repaint();
return true;
}
private boolean handleLeft()
{
if (x > 0)
x = x - inc;
else
x = size().width;
repaint();
return true;
}
private boolean handleIncrease()
{
width += 5;
height += 5;
repaint();
return true;
}
private boolean handleDecrease()
{
if (width > 5)
{
width -= 5;
height -= 5;
}
repaint();
return true;
}
}
如何添加另一個矩形當我按下按鈕,上,下,左,右?
我真的很抱歉,先生!我對Japplet非常陌生,所以我稍微不明白你的答案。我如何在集合中存儲我想要的東西? –
例如,您創建一個包含x,y,寬度和高度字段的Rectangle類,並創建一個'List'。 –
謝謝先生!我會努力的。 :d –