簡體   English   中英

Java Applet:無法弄清楚如何在按鍵時顯示矩形

[英]Java Applet: Cant figure out how to display rectangle on keypressed

我基本上是在編寫這款基本的街機游戲,並且我需要用圓圈將小小的矩形發射出去,每當空格鍵被擊中時,子彈或導彈就會擊中壞人,但我不知道該怎么做。

到目前為止,這是我的代碼:

   import java.applet.Applet;
   import java.awt.Color;
   import java.awt.Graphics;
   import java.awt.Image;
   import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

     public class main extends Applet implements Runnable, KeyListener {

private Image i;
private Graphics doubleG;
// x and y are used to set (x,y) positions
// dx and dy are the changes in position
private int x = 850;
private int y = 850;
private int x2 = 100;
private int y2 = 100;
private int dx = 50;
private int radius = 30;
private int dx2 = 4;
private int dy2 = 4;
private int x3 = 100;
private int y3 = 200;
private int dx3 = 5;
private int dy3 = 5;
private int x4 = 100;
private int y4 = 300;
private int dx4 = 3;
private int dy4 = 3;

public void init(){
    setSize(1920,1080);
    setBackground(Color.black);
    addKeyListener(this);
}

public void start(){
    Thread thread = new Thread(this);
    thread.start();

}

public void run() {
    while(true){
        //Enemy
        if(x2 + dx2 > this.getWidth() - radius - 1){
            x2 = this.getWidth() - radius - 1;
            dx2 = -dx2;
        }
        if(x2 + dx2 < 0 + radius){
            x2 =  0 + radius;
            dx2 = -dx2;
        }
        x2 += dx2;
        // Enemy
        if(x3 + dx3 > this.getWidth() - radius - 1){
            x3 = this.getWidth() - radius -1;
            dx3 = -dx3;
        }
        if(x3 + dx3 < 0 + radius){
            x = 0 + radius;
            dx3 = -dx3;
        }
        x3 += dx3;
        // Enemy
        if(x4 + dx4 > this.getWidth() - radius - 1){
            x4= this.getWidth() - radius -1;
            dx4 = -dx4;
        }
        if(x4 + dx4 < 0 + radius){
            x4 = 0 + radius;
            dx4 = -dx4;
        }
        x4 += dx4;
        // EVERYTHING ABOVE KEEPS ASTEROIDS IN THE SCREEN ALLOWING IT TO BOUNCE OFF WALLS
        repaint();
        try{
            Thread.sleep(17);
        } catch (InterruptedException e){
            e.printStackTrace();
        }
    }   
}
public void stop(){

}

public void update(Graphics g){
    // this function stops the flickering problem every time the ball moves by copying the image instead of repainting it
    if(i == null){
        i = createImage(this.getSize().width, this.getSize().height);
        doubleG = i.getGraphics();
    }
    doubleG.setColor(getBackground());
    doubleG.fillRect(0,0,this.getSize().width, this.getSize().height);
    doubleG.setColor(getForeground());
    paint(doubleG);
    g.drawImage(i,0,0,this);
}

public void paint(Graphics g){
    g.setColor(Color.BLUE);
    g.fillOval(x, y, radius*2, radius*2);
    g.setColor(Color.RED);
    g.fillOval(x2, y2, radius + 10, radius + 10);
    g.setColor(Color.RED);
    g.fillOval(x3,y3, radius + 10, radius + 10);
    g.setColor(Color.RED);
    g.fillOval(x4, y4, radius + 10, radius + 10);
}

public void moveRight(){
    if (dx-1 > -20){
        dx += 1;
    }
    if(x + dx > this.getWidth() - radius - 1){
        x = this.getWidth() - radius - 1;
        dx = -dx;
    }
    x += dx;
}

public void moveLeft(){
    if(dx - 1 > -20){
        dx -= 1;
    }
    if(x + dx < 0 + radius){
        x =  0 + radius;
        dx = -dx;
    }
    x -= dx;

}

public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    switch(e.getKeyCode()){
        case KeyEvent.VK_LEFT:
            moveLeft();
            break;
        case KeyEvent.VK_RIGHT:
            moveRight();
            break;
    }   
}

public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

}

  1. 如果注冊到它的組件是可聚焦的並且有焦點,則KeyListener僅會引發KeyEvent
  2. 您永遠不會調用super.paint ,期待一些嚴重的繪畫瑕疵
  3. 避免覆蓋頂層容器(如Applet )的paint
  4. 考慮在AWT上使用基於Swing的組件,除了進行了更多的更新和更廣泛的使用外,默認情況下,Swing組件也被雙重緩沖。 使用JAppletJPanel的組合作為主要繪圖表面,覆蓋它的paintComponent方法。 在這種情況下,也可以考慮在Thread使用javax.swing.Timer ,除非您想嘗試在兩次更新之間保持可變的延遲。 這也將允許您使用鍵綁定API來解決與KeyListener相關的焦點問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM