Java Applet:无法弄清楚如何在keypressed上显示矩形

时间:2014-05-14 00:18:17

标签: java applet

我基本上编码这个基本的街机游戏,我需要圆圈射出看起来像子弹或导弹的小矩形,以便在空格键被击中时击中坏人,但我无法弄清楚如何。

到目前为止我的代码:

   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 个答案:

答案 0 :(得分:1)

  1. KeyListener只会提升KeyEvent,如果注册的组件是可调焦的并且有效。
  2. 你永远不会打电话给super.paint,期待一些严重的油漆文物
  3. 避免覆盖paint顶级容器(例如Applet
  4. 考虑在AWT上使用基于Swing的组件,除了更新更新和更广泛使用之外,Swing组件默认也是双缓冲的。使用JAppletJPanel的组合作为主要绘图表面,覆盖它的paintComponent方法。在这种情况下,除非您想尝试在更新之间保持可变延迟,否则还要考虑使用javax.swing.Timer而不是Thread。这样您还可以使用key bindings API克服与KeyListener
  5. 相关的焦点问题