1

我正在创建一个飞行模拟器,该模拟器使用事件驱动编程和动态运动(用于导弹等第四种)的组合来驱动。目前,我有一个使用 keyListener 接口和备受嘲笑的 if-else 命令结构的单人游戏。如果我按一个键,则此方法有效,但如果我按第二个键,则第一个键执行的操作将停止。我曾尝试使用键绑定来解决此问题,但是,我正在使用 breezy GUI 创建游戏窗口,并且找不到将 keyBindings 直接应用于 BreezyGUI 框架的方法。我也避免尝试将键绑定应用于按钮和文本字段,因为它们可能会产生焦点问题。

有没有办法将 KeyBindings 直接绑定到 BreezyGUI 框架,如果没有,我应该如何进行?如果我的解释不够充分,请参阅下面的节略代码。

import java.awt.*;    
import BreezyGUI.*;
import java.io.* ;
import java.util.Scanner;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.lang.Math;
import java.awt.image.BufferedImage;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.Action;



public class fltsm extends GBFrame implements KeyListener
{
    private static int fps = 0; 
    private static int score = 0;
    private final int TIMER_DELAY = 100; //milliseconds
    private static final int KEY_DOWN = 40; // KEYCODES
    private static final int KEY_UP = 38;
    private static final int KEY_RIGHT = 39;
    private static final int KEY_LEFT = 37;
    private static final int KEY_SPACE = 32;
    private static final int KEY_0 = 48;
    private static final int KEY_1 = 49;
    private static final int KEY_2 = 50;
    private static final int KEY_3 = 51;
    private static final int KEY_4 = 52;
    private static final int KEY_5 = 53;
    private static final int KEY_6 = 54;
    private static final int KEY_ENTER = 10;
    private static final int SCREEN_LENGTH = 1000;
    private static final int SCREEN_HEIGHT = 600;
    private static String keyWord;
    private static String commandString = ""; // When the user types in alphabetic keys, they are added to this String and matched to a list if executable commands.
    boolean timerStarted = false;
    boolean keyListenerAdded = false;
    boolean calledByEvent = true;
    boolean KeyPressed = false;
    boolean drawFPS = false;
    int kCode = 0;
    KeyEvent event = null; // helps clear out unwanted keyListeners(see object Listener class below)

    private static ArrayList<Missile> miss = new ArrayList<Missile>();
    private static ArrayList<Missile> Co = new ArrayList<Missile>(); 
    World w1 = new World(SCREEN_LENGTH,SCREEN_HEIGHT);
    Plane flyer = new Plane();
    Timer t;



public void paint(Graphics g)
{
    if (drawFPS == true)
        fps++;

    if (Plane.getPlanes().indexOf(flyer) == - 1) // Until I find a better solution...
    {    
        Plane.addPlane(flyer);
    }  

     addKeyListener (this); 
     Plane.processPlanes(g);
     PlaneMissile.processMissiles(g, w1, calledByEvent);
     BotMissile.processMissiles(g, w1, calledByEvent, flyer);
     TrackingBot.processBots(g, w1, flyer);
     DeathLaser.processLasers(g, flyer);
     ScanningLaser.processLasers(g, flyer);
     EMP.processEMPs(g, flyer);
     Wall.processWalls(g, w1, flyer);

     drawStatBox(g, flyer);
}      

public static void normalizeGraphics() 
{
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = env.getDefaultScreenDevice();
    GraphicsConfiguration config = device.getDefaultConfiguration();
    BufferedImage buffy = config.createCompatibleImage(SCREEN_LENGTH, SCREEN_HEIGHT, Transparency.TRANSLUCENT);
}


public static void main (String[ ] args)
{
         //normalizeGraphics();
         Frame frm = new fltsm( );
         frm.setSize (SCREEN_LENGTH, SCREEN_HEIGHT);
         frm.setTitle("Flight Simulator");
         frm.setVisible(true); 

}



public void keyPressed(KeyEvent event) 
{    
     kCode = event.getKeyCode();
     keyWord = String.valueOf(kCode);

     if (timerStarted == false)
     {
         ObjectListener x = new ObjectListener ();
         t = new Timer(TIMER_DELAY, x);
         t.start(); 
         timerStarted = true;
     }   

 }   
public void keyTyped (KeyEvent event ) 
{
    removeKeyListener(this);  // removes unwanted keyListeners (this method cannot be called in the ObjectListener class)
}

public void keyReleased ( KeyEvent event) 
{
}

public KeyEvent getEvent ()
{
    return event;
}


 class ObjectListener implements ActionListener // Called to process objects that move on the screen
{                                        // regardless of whether a key is pressed.
  public void actionPerformed(ActionEvent event)
   {
       keyTyped(getEvent());  // This line clears out the unwanted keylisteners, which would otherwise build up everytime the paint() method is called.
       repaint(); 
       calledByEvent = false;
   }     
}    




}       

` I 

- - - - - - - - -更新 - - - - - - - - - - - - - - - - -----

按照气垫船的回答,我将此代码放入我的绘画方法中。它似乎不起作用。

    if (Plane.getPlanes().indexOf(flyer) == - 1) // Until I find a better solution...
        {    
            Plane.addPlane(flyer);
            JPanel panel = new JPanel();
            add(panel);
            panel.getInputMap().put(KeyStroke.getKeyStroke("1", "doSomething");  
            panel.getActionMap().put("doSomething", new anAction());

        }  

class anAction extends AbstractAction  
{                                        
  public void actionPerformed(ActionEvent event)
   {
       TrackingBot.botMania();
   }     
}    
4

2 回答 2

2

我不使用 BreezyGUI,但似乎 GBFrame 扩展了 java.awt.Frame,一个 AWT 组件,因此无法将 Key Bindings 附加到它,因为您只能将它们附加到 Swing 组件 - 即派生自的类J组件。

我不确定这是否允许,但如果您将 JPanel 或其他 JComponent 添加到 GUI,您可以尝试将键绑定附加到它。

于 2013-06-12T21:53:54.563 回答
0

我以某种方式修复了它。我唯一改变的两件事是代码的位置和 add 方法。我使用“this.add()”而不是“add()”,并将代码放在构造函数中,而不是在paint方法中。

于 2013-06-20T01:09:20.173 回答