2013-12-08 76 views
1

我使用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

我認爲paintComponent()代碼中的repaint()會導致無限遞歸。

重繪 - >的paintComponent - >重繪 - > ...

這是搞亂Graphics對象。

編輯:

還有一個錯誤在你的代碼,您的通話super.paintComponent方法小號(G),而不是super.paintComponent(g)。嘗試使用沒有的方法

下面是根據最後點擊的位置移動的串類的一個簡單的例子:


 

public class Test extends JPanel implements MouseListener { 

    private int x = 100; 
    private int y = 100; 

    public static void main(String[] args) { 
     JFrame jFrame = new JFrame(); 
     Test test = new Test(); 
     jFrame.add(test); 
     jFrame.setBounds(0, 0, 800, 600); 
     jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     jFrame.setVisible(true); 
     jFrame.addMouseListener(test); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawString("blub", x, y); 

    } 

    @Override 
    public void mouseClicked(MouseEvent e) { 
     this.x = e.getX(); 
     this.y = e.getY(); 
     repaint(); 
    } 

    @Override 
    public void mousePressed(MouseEvent e) { 

    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 
} 
+0

+1,但它不會傷害解釋得遠一點 – Sage

+0

即使在的paintComponent(圖形g)除去重繪()方法,它仍然沒有解決售後服務問題,但後在actionlistener代碼部分它不會移動對象 –

+0

@daviestobialex請參閱編輯答案 – lwi

0

你是第一個提問我發現是誰在覆蓋調用super.paintComponent(g)paintComponent(Graphics g)功能他們的第一個發帖問題。那麼,

調用repaint()實際上將提交新的繪製請求導致paintComponenr(Graphics g)功能的另一個調用,並把repaint()paintComponent函數內部將創建一個循環,調用畫圖功能陸續增加渲染堆棧。因此請從paintComponent函數中刪除repaint()調用。

退房教程:Performing custom painting