I am creating a flight simulator that is driven using a combination of event driven programming and dynamic movement(for missiles and so fourth). Presently, I have a working one player game using a keyListener interface and the much-derided if-else command structure. This works if I am pressing one key but if I press a second key, the action performed by the first key will stop. I have attempted to solve this problem using key bindings, however, I am using breezy GUI to create the game window and I cannot find a way to apply keyBindings directly to the BreezyGUI frame. I've also refrained from trying to apply a keybindings to buttons and text fields since they might create problems with focus.
Is there a way to tie KeyBindings directly to a BreezyGUI frame and, if not, how should I proceed? Please refer to my abridged code below if my explanation is not sufficient.
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
-----------------UPDATE-------------------------------------
Following Hovercraft's Answer I put this code in my paint method. It does not seem to work.
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();
}
}
I don't use BreezyGUI, but it appears that the GBFrame extends java.awt.Frame, an AWT component, and so Key Bindings cannot be attached to it since you can only attach them to Swing components -- i.e., classes that derive from JComponent.
I'm not sure if this is allowed, but if you add a JPanel or other JComponent to the GUI, you could try attaching Key Bindings to it.