2012-04-30 189 views
0

我有一個java程序,它在屏幕上顯示一個橢圓,並通過使用箭頭鍵來改變它的方向。我經常使用while循環在橢圓上調用repaint()。在Java中繪畫橢圓

橢圓移動,但問題是它的路徑上留下了一個省略號。我如何製作它,以便刪除舊的省略號,然後重新繪製新的省略號?

代碼:

public void run(){ 
     while (animator != null) 
     { 
      setBackground(Color.GREEN); 
      repaint(); 
          // The direction works and the rest works fine. 
      player1.move(player1, player1.direction); 
      try 
      { 
       Thread.sleep(100); 
      } 
      catch (InterruptedException e) 
      { 
       break; 
      } 
     } 
} 

// The paintComponent of my player1 is fine. 
public void paint(Graphics g){ 
     Graphics2D g2 = (Graphics2D)g; 
     player1.paintComponent(g2, player1); 
} 

回答

1

的問題可能是您的paintComponent(Graphics)覆蓋方法並沒有叫super.paintComponent(g),它清除畫布,除其他事項外:

// Bad: 
@Override 
public void paintComponent(Graphics g) { 
    // paint the ellipse, etc. 
} 

// Good: 
@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    // paint the ellipse, etc. 
} 
0

你應該在遊戲循環繪製橢圓之前繪製背景顏色:) 您也可以調用重繪(),它實際上調用update()做 髒活清除屏幕並調用paint()。 當您使用重繪更新方法繼承

public void update(Graphics g) { 
g.setColor(getBackground()); 
g.fillRect(0, 0, width, height); 
g.setColor(getForeground()); 
paint(g) 
} 

這樣的背景返回到原來的顏色和橢圓的線索將被刪除

1

下面是一些基本的代碼確實非常基礎(不使用連續while循環):

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 


@SuppressWarnings("serial") 
public class Ellipses extends JFrame { 

    public static void main(String[] args){ 
     //Ensures application is not run on the main thread 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       Ellipses myEllipses = new Ellipses(); 
       myEllipses.init(); 
      } 
     }); 
    } 

    public Ellipses(){ 
     //Set up the frame 
     this.setTitle("Ellipses Example"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 
     this.setSize(400, 400); 
    } 

    private Ellipse ellipse1; 

    public void init(){ 
     Container contentPane = this.getContentPane(); 

     //Create a new ellipse and add to the content pane 
     ellipse1 = new Ellipse(); 
     contentPane.add(ellipse1); 


     //Add the keyListener to the contentPane 
     contentPane.addKeyListener(new KeyListener() { 

      public void keyTyped(KeyEvent e) {} 
      public void keyReleased(KeyEvent e) {} 
      public void keyPressed(KeyEvent e) { 
       if(e.getKeyCode() == KeyEvent.VK_UP){ 
        ellipse1.decreaseY(); 
       } 
       if(e.getKeyCode() == KeyEvent.VK_DOWN){ 
        ellipse1.increaseY(); 
       } 
       if(e.getKeyCode() == KeyEvent.VK_LEFT){ 
        ellipse1.decreaseX(); 
       } 
       if(e.getKeyCode() == KeyEvent.VK_RIGHT){ 
        ellipse1.increaseX(); 
       } 

       //Repaint the ellipse 

       ellipse1.repaint(); 
      } 
     }); 

     //Request the focus so key presses can be detected 
     contentPane.setFocusable(true); 
     contentPane.requestFocus(); 
    } 



    //Create an ellipse which can be drawn to the screen 
    public class Ellipse extends JComponent{ 

     private int x , y; //Coordinates of the oval 

     public Ellipse(){ 
      setCoordinates(100, 100); 
     } 

     public void setCoordinates(int x, int y){ 
      this.x = x; 
      this.y = y; 
     } 

     public void increaseY(){ 
      y+=10; 
     } 

     public void increaseX(){ 
      x+=10; 
     } 

     public void decreaseY(){ 
      y-=10; 
     } 

     public void decreaseX(){ 
      x-=10; 
     } 

     public void paint(Graphics g){ 
      //Ensures previous paint is cleared 
      super.paintComponents(g); 
      g.setColor(Color.RED); 
      g.fillOval(x, y, 100, 100); 
     } 
    } 
}