Results 1 to 2 of 2
  1. #1
    Unome is offline Member
    Join Date
    Oct 2008
    Posts
    44
    Rep Power
    0

    Default 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...

    PHP 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();
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    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

Similar Threads

  1. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:36 PM
  2. Deleting an empty directory
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:17 AM
  3. Not Empty check of texboxes using javascript
    By Anju Jose in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-09-2008, 12:04 PM
  4. Removing empty lines from code using Eclipse
    By javaplus in forum Eclipse
    Replies: 1
    Last Post: 12-14-2007, 09:21 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •