Over the past few months, I have been using Java.Awt.Frame to create a screen and keyListeners for keyboard input. However, my program has become to complicated for keylisteners, so I recently decided to use KeyBinds instead. The problem is, I cannot figure out how to add them to the AWT frame. I attempted to create a JPanel and add that to the frame(using add()), however, this did not appear to do anything. Any input would be appreciated; please refer to my code if you need to.
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);
JPanel panel = new JPanel();
add(panel);
panel.getInputMap().put(KeyStroke.getKeyStroke("1", "doSomething");
panel.getActionMap().put("doSomething", new anAction());
}
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 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;
}
}
class an implements AbstractAction // Called to process objects that move on the screen
{ // regardless of whether a key is pressed.
public void actionPerformed(ActionEvent event)
{
TrackingBot.botMania();
}
}
}
private static final int KEY_0 = 48;
Don't use magic values. Nobody knows what 48 is. I'm assuming KEY_0 is representing KeyEvent.VK_0, so use that variable and don't make up your own.
I recently decided to use KeyBinds instead. The problem is, I cannot figure out how to add them to the AWT frame
You can't add binding to a Frame. Key Bindings are for Swing components only. Don't mix AWT and Swing. Just use a JFrame with a JPanel, then you can use Key Bindings easily.
public void paint(Graphics g)
Don't override the paint() method of a top level container. Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent). Then you add the panel to the frame.
JPanel panel = new JPanel();
add(panel);
Don't create and add components to a frame in a painting method. A painting method should only do painting.
panel.getInputMap().put(KeyStroke.getKeyStroke("1", "doSomething");
panel.getActionMap().put("doSomething", new anAction());
The default InputMap will only support KeyBindings with the component has focus. By default a JPanel doesn't have focus. The easier approach is to use one of the other InputMaps. Read the Swing tutorial on How to Use Key Bindings to find out more about these other InputMaps.