2016-04-01 15 views
1

私はJavaで正方形をアニメーション化しようとしていますが、キーを押すと正方形の後ろにトレイルが残ります。私は蛇のような道を離れることなく広場自体を動かしたい。Javaのアニメーションが機能しない

どうすればこの問題を解決できますか?それを修正するために何かできることはありますか?更新された座標と新しい四角形を描画する前に(つまりは背景色で上書き)あなたはいつもすでにキャンバスに描かれた四角形を消去する必要があり

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.image.BufferStrategy; 

import javax.swing.JFrame; 

public class Main extends Canvas implements KeyListener, Runnable { 

    Thread t; 
    boolean running = false; 

    int x = 200; 
    int y = 200; 
    int velx; 
    int vely; 

    public Main() { 
     setFocusable(true); 
     requestFocus(); 
     addKeyListener(this); 
    } 

    public void run() { 
     while (running) { 
      render(); 
      tick(); 
     } 
     stop(); 
    } 

    synchronized void start() { 
     if (running) return; 

     running = true; 
     t = new Thread(this); 
     t.start(); 
    } 

    synchronized void stop() { 
     try { 
      t.join(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     System.exit(1); 
    } 

    public void render() { 

     BufferStrategy bs = this.getBufferStrategy(); 
     if (bs == null) { 
      createBufferStrategy(3); 
      return; 
     } 
     Graphics g = bs.getDrawGraphics(); 

     g.setColor(Color.CYAN); 
     g.fillRect(x, y, 300, 300); 

     g.dispose(); 
     bs.show(); 

    } 

    public void tick() { 
     x += velx; 
     y += vely; 
    } 

    @Override 
    public void keyPressed(KeyEvent e) { 
     int key = e.getKeyCode(); 
     if (key == KeyEvent.VK_RIGHT) { 
      velx += 5; 
     } else if (key == KeyEvent.VK_LEFT) { 
      velx -= 5; 
     } else if (key == KeyEvent.VK_DOWN) { 
      vely += 5; 
     } else if (key == KeyEvent.VK_UP) { 
      vely -= 5; 
     } 
    } 

    @Override 
    public void keyTyped(KeyEvent e) {} 

    @Override 
    public void keyReleased(KeyEvent e) { 
     velx = 0; 
     vely = 0; 
    } 

    public static void main (String args[]){ 

     JFrame frame = new JFrame("Animation"); 
     Main main = new Main(); 

     frame.setSize(1200, 800); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     frame.add(main); 
     frame.setResizable(false); 
     frame.setVisible(true); 

     main.start(); 

    } 

} 

答えて

0

は、ここに私のコードです。これを行うには、コードを次のように変更します。

int prevX, prevY; 

public void render() { 
    BufferStrategy bs = this.getBufferStrategy(); 
    if (bs == null) { 
     createBufferStrategy(3); 
     return; 
    } 
    Graphics g = bs.getDrawGraphics(); 

    // erase the previous square 
    g.setColor(getBackground()); 
    g.fillRect(prevX, prevY, 300, 300); 

    // draw the new square 
    g.setColor(Color.CYAN); 
    g.fillRect(x, y, 300, 300); 

    g.dispose(); 
    bs.show(); 
} 

public void tick() { 
    // backup the previous coordinates 
    prevX = x; 
    prevY = y; 

    x += velx; 
    y += vely; 
} 
関連する問題