-
KeyBindings Empty Key?
Mmk... everything works... except my program only repaints when a key is pressed, My solution to this problem was to simply make an action that runs when nothing is typed. My question. How? I tried putting a null into the pressed key section. I tried making it continue off key release... that helped.. for one aditional frame. I need the paint to ALWAYS be painted, but to be painted differently if and only if, one of my keys are pressed. I Just need to know how to make this "Blank" key... Any help? heres my code...
Code:
// <applet code="KeyFire" width="400" height="400"></applet>
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class Gunmaster2 extends JApplet {
KeyFirePanel keyFirePanel;
public void init() {
keyFirePanel = new KeyFirePanel();
keyFirePanel.setFocusable(true);
add(keyFirePanel);
bindKeys();
}
private void bindKeys() {
JRootPane rp = getRootPane();
int c = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = rp.getInputMap(c);
ActionMap actionMap = rp.getActionMap();
inputMap.put(KeyStroke.getKeyStroke("UP"), "UP");
actionMap.put("UP", upAction);
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "DOWN");
actionMap.put("DOWN", downAction);
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT");
actionMap.put("RIGHT", rightAction);
inputMap.put(KeyStroke.getKeyStroke(""), "released");
actionMap.put("released", releasedAction);
}
public static void main(String[] args) {
JApplet applet = new Gunmaster();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(applet);
f.setSize(400,400);
f.setLocation(200,200);
applet.init();
f.setVisible(true);
}
private AbstractAction upAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
keyFirePanel.rotateRect(1); //-1 is needed to go up
}
};
private AbstractAction downAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
keyFirePanel.rotateRect(-1); // + 1 is needed to go down
}
};
private AbstractAction rightAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
keyFirePanel.fire();
}
};
private AbstractAction releasedAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
keyFirePanel.draw();
}
};
}
class KeyFirePanel extends JPanel {
int angle = 0;
int anglefire = 0;
int inc = 5;
int stickx = 50;
int sticky =50;
int bulx = 0;
int count2 = 1;
int count = 0;
boolean firing = false;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
double theta = Math.toRadians(angle);
Rectangle clear = new Rectangle(0,0,500,500);
Gunman stickbob = new Gunman(stickx,sticky,theta);
g2.setColor(Color.WHITE);
g2.fill(clear);
stickbob.draw(g2);
g2.setColor(Color.BLACK);
if(firing)
{
count2 = 2;
if(count == 0)
{
anglefire = angle;
}
}
if(count2 == 2 && count <= 10)
{
bulx= bulx + 10;
bullet2 projectile1 = new bullet2(stickx+16 + bulx,sticky+14);
g2.rotate(Math.toRadians(-anglefire),stickx+4,sticky+16);
projectile1.shoot(g2);
g2.rotate(Math.toRadians(anglefire),stickx+4,sticky+16);
count++;
if(count == 10)
{
anglefire = 0;
bulx = 0;
count = 0;
count2 =1;
firing = false;
}
}
}
public void rotateRect(int direction) { //rotates arm up or own
angle += direction*inc;
repaint();
}
public void fire() { //fires the bullet
firing = true;
repaint();
}
public void draw() { //draws
repaint();
}
}
-
What is a Blank key? Is that the Space bar?
If that is what you want, read the API doc for the InputMap put method. It takes a KeyStroke. There are ways to make KeyStrokes other than the one you've used. For Space bar, it would probably use VK_SPACE