2013-10-03 62 views
0

我的圖片不動。
這裏是應用程序的整個代碼:圖片不動

package Game; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Game extends JPanel { 

    private static final long serialVersionUID = 1L; 

    private BufferedImage image; 

    public static final int WIDTH = 600; 
    public static final int HEIGHT = 500; 
    private int x = 1; 
    private int y = 1; 

    private static int dx; 
    private static int dy; 

    private boolean leftPressed = false; 
    private boolean rightPressed = false; 
    private boolean topPressed = false; 
    private boolean downPressed = false; 

    public static void main(String avg[]) throws IOException { 
     Game abc = new Game(); 

    } 

    public Game() { 
     try { 
      JFrame frame = new JFrame(); 
      image = ImageIO.read(new File(
        "C:\\Users\\дНМ\\workspace\\Game\\image\\heroG.png")); 

      frame.setSize(WIDTH, HEIGHT); 
      frame.setVisible(true); 
      frame.getContentPane().add(this); 
      this.setBackground(Color.BLACK); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setTitle("Tittle - Game"); 
      addKeyListener(new KeyInputHandler()); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 



    public void move() { 

     x += dx; 
     y += dy; 

     if (x < 1) { 
      x = 1; 
     } 

     if (y < 1) { 
      y = 1; 
     } 
    } 

    public void update(long delta) { 
     if (leftPressed == true) { 
      x--; 
     } 
     if (rightPressed == true) { 
      x++; 
     } 
    } 

    public void update2(long delta) { 
     if (downPressed == true) { 
      y--; 
     } 
     if (topPressed == true) { 
      y++; 
     } 
    } 

    private class KeyInputHandler extends KeyAdapter { 
     public void keyPressed(KeyEvent e) { 
      if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
       leftPressed = true; 
       dx = -1; 
      } 
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
       rightPressed = true; 
       dx = 1; 
      } 
      if (e.getKeyCode() == KeyEvent.VK_UP) { 
       topPressed = true; 
       dy = -1; 
      } 
      if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
       downPressed = true; 
       dy = 1; 
      } 
     } 

     public void keyReleased(KeyEvent e) { 
      if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
       leftPressed = false; 
       dx = 0; 
      } 
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
       rightPressed = false; 
       dx = 0; 
      } 
      if (e.getKeyCode() == KeyEvent.VK_UP) { 
       topPressed = false; 
       dy = 0; 
      } 
      if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
       downPressed = false; 
       dy = 0; 
      } 
     } 
    } 
    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawImage(image, x, y, null); 
    } 
} 

這有什麼錯呢?
請指出您可能需要添加的錯誤。

+0

所有的節目,但是當你按下畫面不動 –

+0

你爲什麼認爲它應該移動? –

+0

那麼,我寫了一個運動的方法,但他們顯然不工作...你能告訴什麼是錯的? –

回答

1

首先:您的面板沒有攔截的關鍵事件,檢查其他類似這樣的問題:addKeyListener() doesn't work for JPanel

:你改變的變量dxdy當一個鍵被按下,但你是不是要求move()方法

第三個您在按下每個鍵後沒有重新繪畫。

這些都是修改:

遊戲構造

public Game() { 
    try { 
     JFrame frame = new JFrame(); 
     image = ImageIO.read(new File("Zombatar.jpg")); 

     frame.setSize(WIDTH, HEIGHT); 
     frame.setVisible(true); 
     frame.getContentPane().add(this); 
     this.setBackground(Color.BLACK); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("Tittle - Game"); 

     addKeyListener(new KeyInputHandler()); 

     //this is important to intercept key events 
     setFocusable(true); 
     requestFocusInWindow(); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 

KeyAdapter

private class KeyInputHandler extends KeyAdapter { 

    public void keyPressed(KeyEvent e) { 
     if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
      dx = -1; 
     } 
     else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
      dx = 1; 
     } 
     else if (e.getKeyCode() == KeyEvent.VK_UP) { 
      dy = -1; 
     } 
     else if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
      dy = 1; 
     } 

     //first call move to update x and y and later repaint tha JPanel 
     move(); 
     repaint(); 
    } 
} 
+0

非常感謝! –

+0

@IvanCern不客氣的男士 – omainegra