2013-04-13 66 views
1

我目前正在製作遊戲,但由於某些原因,某些Graphics2D組件未移動且程序最終崩潰。 KeyListener和移動之間是否存在一些干擾?或者,也許是因爲我已經重新粉刷了移動中的所有面板?Graphics2D不移動

我已經評論了代碼的每個部分都做了什麼。

下面是代碼: `

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 

public class Interface extends JPanel implements ActionListener, KeyListener{ 

Timer timer; 

Player p; 
Alien[] a; 

ImageIcon img, msl, al; 
Image i, ms, ali; 
Graphics2D g2d; 

boolean allowUp = true, allowDown = true, allowAlienMove = false, firstDraw = true; 
boolean[] allowAlien; 
int mx, my = 0; 
static int[] ay, ax, olday, oldax; 

public Interface(){ 

    setFocusable(true); 
    setBackground(Color.BLACK); 
    setDoubleBuffered(true); 

    addKeyListener(this); 

    img = new ImageIcon(this.getClass().getResource("player.jpg")); 
    al = new ImageIcon(this.getClass().getResource("alien.jpg")); 

    p = new Player(this.getWidth()/6, this.getHeight()/6,10,this.getHeight()/2,img); 
    timer = new Timer(1000, this); 

    allowAlien = new boolean[5]; 
    a = new Alien[5]; 
    ay = new int[5]; 
    ax = new int[5]; 
    olday = new int[5]; 
    oldax = new int[5]; 

    for(int i = 0; i < 5; i++){ 

     ay[i] = -1; 
     olday[i] = -1; 

     ax[i] = -1; 
     oldax[i] = -1; 

    } 

    for(int i = 0; i < allowAlien.length; i++){ 

     allowAlien[i] = false; 

    } 

    spawnAliens();   


} 

//SPAWN ALIENS 

public void spawnAliens(){  

    int[] area = new int[5]; 
    area[0] = 70; 
    area[1] = 170; 
    area[2] = 270; 
    area[3] = 370; 
    area[4] = 445; 

    for(int j = 0; j < ax.length; j++){ 

     ax[j] = 700; 
     oldax[j] = ax[j]; 

    } 

    for(int i =0; i < a.length; i++){ 

     if(i > 0){ 
      ay[i] = ((int)(Math.random()*area[i] + area[i-1])); 
     }else{ 
      ay[i] = ((int)(Math.random()*area[i] + 5)); 
     } 

     olday[i] = ay[i]; 


     //THIS COMPONENT NOT MOVING 
     a[i] = new Alien(this.getWidth()/6, this.getHeight()/6,ax[i],ay[i],img); 
     allowAlien[i] = true; 

     repaint(); 

    } 

    allowAlienMove = true; 



} 

//THIS IS THE DRAWING PART 

public void paint(Graphics g){ 
    super.paint(g); 

    i = img.getImage(); 
    ms = msl.getImage(); 
    ali = al.getImage(); 

    g2d = (Graphics2D)g; 
    g2d.drawImage(i, p.getX(), p.getY(), null); 
    if(allowMissle){  
     g2d.drawImage(ms, m.getX(), m.getY(), null); 
    } 

    for(int i = 0; i < a.length; i++){ 
     if(allowAlien[i]){   
      g2d.drawImage(ali, ax[i], ay[i], null); 


     } 
    } 


    if(firstDraw){ 
     timer.start(); 
    } 

    firstDraw = false; 
} 

//THIS IS THE MOVEMENT PART 

public void actionPerformed(ActionEvent e){ 


    while(ax[0] > 10){ 

     ax[0] += 10; 
     repaint(); 
    } 


} 

//KEY LISTENER INTERFACE METHODS 

public void keyReleased(KeyEvent e){ 



} 
public void keyPressed(KeyEvent e){ 

    int key = e.getKeyCode(); 

    if (key == KeyEvent.VK_W) { 

     if(p.getY() <= 5){ 

      allowUp = false; 

     }else{ 

      allowUp = true; 

     } 

     if(allowUp){   
      p.move(0, -5); 
      my = p.getY(); 

      repaint(); 
     } 
    }else if (key == KeyEvent.VK_S) { 

     if(p.getY() >= 410 - 5){ 

      allowDown = false; 

     }else{ 

      allowDown = true; 

     } 
     if(allowDown){ 
      p.move(0, 5); 
      my = p.getY(); 
      repaint(); 
     } 
    } 

} 
public void keyTyped(KeyEvent e){ 


} 
} 

感謝提前:)

`

+0

請發佈Alien和Player類 – Aubin

+0

自定義繪畫應該通過覆蓋'paintComponent()'方法來完成,而不是paint()方法。不要使用KeyListener。而是使用'鍵綁定'。搜索論壇這個建議是每天進行的。 – camickr

回答

1

因爲你沒有停頓在那裏,這意味着一切都將在瞬間發生,這將導致其崩潰。你還開始了一個計時器,但從來沒有使用它是否有你想用這個來實現的東西?

+0

我在paint方法中啓動了Timer。 –

+0

是的,但如果你剛開始它,它什麼都不會做。你需要以某種方式使用它,否則它什麼也不做。 – phcoding

+0

永遠不要在paint()方法中啓動Timer。 – camickr