我使用Java g.drawstring()
方法並使用Breseham's Circle算法構建了一個Java 2d圓圈,我用它來繪製笑臉,但是當我移動笑臉時,它會在屏幕上重複出現,我確信那個PaintComponent(Graphics g)
方法不斷重新繪製simley在指定的不同位置,但如何糾正這種邏輯錯誤是我的問題。這是我寫的代碼。Java二維對象重複
public class Midsemester extends JPanel implements ActionListener {
// objects class
static Movement move = new Movement();
public int x = 0, y = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, x4 = 0, y4 = 0;
private int inix = 0, iniy = 0;
static String[] st = {"xy","x1y1","x2y2","x3y3","x4y4"};
static shapes shaper = new shapes();//object class contain the algorithms used to draw the circles using the Breseham's Circle algorithm
public Midsemester()
{
}
/****/
@Override
public void paintComponent(Graphics g)
{
super.paintComponents(g);
x = (int)move.x;
y = (int)move.y;
x1 = (int)move.x1;
y1 = (int)move.y1;
x2 = (int)move.x2;
y2 = (int)move.y2;
x3 = (int)move.x3;
y3 = (int)move.y3;
x4 = (int)move.x4;
y4 = (int)move.y4;
shaper.draw_floor(100, 350, 1350, g);
shaper.draw_wall(100, 0, 350, g);
shaper.create(x,y,50,g, Color.yellow);//creates the smileys in there different colors
shaper.create(x1,y1,50,g, Color.BLUE);
shaper.create(x2,y2,50,g, Color.pink);
shaper.create(x3,y3,50,g, Color.magenta);
shaper.create(x4,y4,50,g, Color.orange);
repaint();
}
@Override
public void actionPerformed(ActionEvent ae)
{
float[] values = move.firstscenemovement(500,200,st[0]);
repaint();
System.out.println("x:"+values[0] + "\ny:" + values[1]);
}
}
圖片重複或移動,因爲它移動會張貼圖像,但我需要10聲望做。
我該如何糾正這個錯誤?提前致謝。
+1,但它不會傷害解釋得遠一點 – Sage
即使在的paintComponent(圖形g)除去重繪()方法,它仍然沒有解決售後服務問題,但後在actionlistener代碼部分它不會移動對象 –
@daviestobialex請參閱編輯答案 – lwi