Results 1 to 11 of 11
Like Tree2Likes
  • 2 Post By DarrylBurke

Thread: Problem with keybinding

  1. #1
    dendenmushi is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Problem with keybinding

    Hi, i'm new to java and i have a problem with a code.
    I need to move a square with the mouse and the keyboard, i have problems making keybinding, I cannot compile.

    here is the code

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class MuoviTest{
        
        public static void main(String[] args){
            EventQueue.invokeLater(new Runnable()
            {
                public void run(){
                    MuoviFrame frame = new MuoviFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    class MuoviFrame extends JFrame{
        
        // constructor
        public MuoviFrame(){
            setTitle("MuoviTest");
            setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
            
            
            //add component to the frame
            
            MouseComponent component=new MouseComponent();
            add(component);
        }
        
        public static final int DEFAULT_WIDTH = 200;
        public static final int DEFAULT_HEIGHT = 200;
         
    }
    
    class MouseComponent extends JComponent{
        
        private Rectangle2D.Double current;
        private Point2D arrivo;
        private boolean raggiunto;
        private static final int SIDELENGTH = 10;
        private static final int ARRIVOLENGTH=6;
        
        // actions
        
        Action upAction = new moveAction("up");
        Action downAction = new moveAction("down");
        Action rightAction = new moveAction("right");
        Action leftAction = new moveAction("left");
        
    
    // PROBLEM
        InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        KeyStroke leftArrowKey = KeyStroke.getKeyStroke("LEFT");
        imap.put(leftArrowKey, "left");
        KeyStroke rightArrowKey = KeyStroke.getKeyStroke("RIGHT");
        imap.put(rightArrowKey, "right");
        KeyStroke upArrowKey = KeyStroke.getKeyStroke("UP");
        imap.put(upArrowKey, "up");
        KeyStroke downArrowKey = KeyStroke.getKeyStroke("DOWN");
        imap.put(upArrowKey, "down");
        
        ActionMap amap = getActionMap();
        amap.put("left", leftAction);
        amap.put("right", rightAction);
        amap.put("up", upAction);
        amap.put("down", downAction);
        //PROBLEM
        
        
        
        public MouseComponent(){
            
            current = new Rectangle2D.Double(MuoviFrame.DEFAULT_WIDTH/2 - SIDELENGTH / 2, MuoviFrame.DEFAULT_HEIGHT/2 - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
            arrivo = new Point2D.Double(Math.random() * MuoviFrame.DEFAULT_WIDTH,Math.random() * MuoviFrame.DEFAULT_HEIGHT);
            raggiunto= false;
            
            addMouseMotionListener(new MouseMotionHandler());
        }
        
        
        
        
        public void paintComponent(Graphics g){
            Graphics2D g2 =(Graphics2D) g;
            
            //draw squares
                g2.draw(current);
                if(raggiunto==false){
                    g2.draw(new Rectangle2D.Double(arrivo.getX()- ARRIVOLENGTH / 2-1,arrivo.getY()- ARRIVOLENGTH / 2-1, ARRIVOLENGTH+1,ARRIVOLENGTH+1));
                    g2.setColor(Color.GREEN);
                    g2.fill(new Rectangle2D.Double(arrivo.getX()- ARRIVOLENGTH / 2,arrivo.getY()- ARRIVOLENGTH / 2, ARRIVOLENGTH,ARRIVOLENGTH));
                    g2.setColor(Color.BLACK);
                }
        }
        
        
        
        
        private class MouseMotionHandler implements MouseMotionListener{
            public void mouseMoved(MouseEvent event){
                //set cursor of mouse to cross hairs if it is inside a rectangle
                
                if (current.contains(event.getPoint())==false)setCursor(Cursor.getDefaultCursor());
                else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            }
            
            public void mouseDragged(MouseEvent event){
                int x = event.getX();
                int y = event.getY();
                //drag the current rectangle to center it at (x,y)
                current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2, SIDELENGTH, SIDELENGTH);
                if(current.contains(arrivo))raggiunto=true;
                repaint();
              
            }
        }
        
        public class moveAction extends AbstractAction{
            String s;
            public moveAction(String s){
                this.s=s;
            }
            
            public void actionPerformed(ActionEvent event){
                if(s.equals("up"))current.setFrame(current.getX(),current.getY()-1,SIDELENGTH, SIDELENGTH);
                if(s.equals("down"))current.setFrame(current.getX(),current.getY()+1,SIDELENGTH, SIDELENGTH);
                if(s.equals("right"))current.setFrame(current.getX()+1,current.getY(),SIDELENGTH, SIDELENGTH);
                if(s.equals("left"))current.setFrame(current.getX()-1,current.getY(),SIDELENGTH, SIDELENGTH);
                repaint();
            }
        }
        
        
    }
    the problem is between "//PROBLEM", sorry for my bad english ^^ and thank you

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    dendenmushi is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Problem with keybinding

    :59: error: <identifier> expected
    imap.put(leftArrowKey, "left");
    :59: error: <identifier> expected
    imap.put(leftArrowKey, "left");
    :59: error: illegal start of type
    imap.put(leftArrowKey, "left");
    :61: error: <identifier> expected
    imap.put(rightArrowKey, "right");
    :61: error: <identifier> expected
    imap.put(rightArrowKey, "right");
    :61: error: illegal start of type
    imap.put(rightArrowKey, "right");
    :63: error: <identifier> expected
    imap.put(upArrowKey, "up");
    :63: error: <identifier> expected
    imap.put(upArrowKey, "up");
    :63: error: illegal start of type
    imap.put(upArrowKey, "up");
    :65: error: <identifier> expected
    imap.put(upArrowKey, "down");
    :65: error: <identifier> expected
    imap.put(upArrowKey, "down");
    :65: error: illegal start of type
    imap.put(upArrowKey, "down");
    :68: error: <identifier> expected
    amap.put("left", leftAction);
    :68: error: illegal start of type
    amap.put("left", leftAction);
    :68: error: <identifier> expected
    amap.put("left", leftAction);
    :69: error: <identifier> expected
    amap.put("right", rightAction);
    :69: error: illegal start of type
    amap.put("right", rightAction);
    :69: error: <identifier> expected
    amap.put("right", rightAction);
    :70: error: <identifier> expected
    amap.put("up", upAction);
    :70: error: illegal start of type
    amap.put("up", upAction);
    :70: error: <identifier> expected
    amap.put("up", upAction);
    :71: error: <identifier> expected
    amap.put("down", downAction);
    :71: error: illegal start of type
    amap.put("down", downAction);
    :71: error: <identifier> expected
    amap.put("down", downAction);
    24 errors

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Problem with keybinding

    Executable code, apart from declaration-cum-assignment, has to be in a method, constructor or initializer. It can't sit out there in the class body like you've written it.

    I'd say your biggest mistake is writing all that code without compiling along the way. You should start small and compile each time you add a few lines.

    db
    Fubarable and JosAH like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    dendenmushi is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Problem with keybinding

    ok, i have changed the code, and now i can compile but it doesn't work

    Java Code:
    package muovitest;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class MuoviTest{
        
        public static void main(String[] args){
            EventQueue.invokeLater(new Runnable()
            {
                public void run(){
                    MuoviFrame frame = new MuoviFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    class MuoviFrame extends JFrame{
        
        // constructor
        public MuoviFrame(){
            setTitle("MuoviTest");
            setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
            
            
            //add component to the frame
            
            MouseComponent component=new MouseComponent();
            add(component);
        }
        
        public static final int DEFAULT_WIDTH = 200;
        public static final int DEFAULT_HEIGHT = 200;
         
    }
    
    class MouseComponent extends JComponent{
        
        private Rectangle2D.Double current;
        private Point2D arrivo;
        private boolean raggiunto;
        private static final int SIDELENGTH = 10;
        private static final int ARRIVOLENGTH=6;
        
        // actions
        
        Action upAction = new moveAction("up");
        Action downAction = new moveAction("down");
        Action rightAction = new moveAction("right");
        Action leftAction = new moveAction("left");
        
        
        
        
        
        public MouseComponent(){
            
            current = new Rectangle2D.Double(MuoviFrame.DEFAULT_WIDTH/2 - SIDELENGTH / 2, MuoviFrame.DEFAULT_HEIGHT/2 - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
            arrivo = new Point2D.Double(Math.random() * MuoviFrame.DEFAULT_WIDTH,Math.random() * MuoviFrame.DEFAULT_HEIGHT);
            raggiunto= false;
            InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
            KeyStroke leftArrowKey = KeyStroke.getKeyStroke("LEFT");
            imap.put(leftArrowKey, "left");
            KeyStroke rightArrowKey = KeyStroke.getKeyStroke("RIGHT");
            imap.put(rightArrowKey, "right");
            KeyStroke upArrowKey = KeyStroke.getKeyStroke("UP");
            imap.put(upArrowKey, "up");
            KeyStroke downArrowKey = KeyStroke.getKeyStroke("DOWN");
            imap.put(upArrowKey, "down");
        
            ActionMap amap = getActionMap();
            amap.put("left", leftAction);
            amap.put("right", rightAction);
            amap.put("up", upAction);
            amap.put("down", downAction);
        
            addMouseMotionListener(new MouseMotionHandler());
        }
        
        
        
        
        public void paintComponent(Graphics g){
            Graphics2D g2 =(Graphics2D) g;
            
            //draw squares
                g2.draw(current);
                if(raggiunto==false){
                    g2.draw(new Rectangle2D.Double(arrivo.getX()- ARRIVOLENGTH / 2-1,arrivo.getY()- ARRIVOLENGTH / 2-1, ARRIVOLENGTH+1,ARRIVOLENGTH+1));
                    g2.setColor(Color.GREEN);
                    g2.fill(new Rectangle2D.Double(arrivo.getX()- ARRIVOLENGTH / 2,arrivo.getY()- ARRIVOLENGTH / 2, ARRIVOLENGTH,ARRIVOLENGTH));
                    g2.setColor(Color.BLACK);
                }
        }
        
        
        
        
        private class MouseMotionHandler implements MouseMotionListener{
            public void mouseMoved(MouseEvent event){
                //set cursor of mouse to cross hairs if it is inside a rectangle
                
                if (current.contains(event.getPoint())==false)setCursor(Cursor.getDefaultCursor());
                else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            }
            
            public void mouseDragged(MouseEvent event){
                int x = event.getX();
                int y = event.getY();
                //drag the current rectangle to center it at (x,y)
                current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2, SIDELENGTH, SIDELENGTH);
                if(current.contains(arrivo))raggiunto=true;
                repaint();
              
            }
        }
        
        public class moveAction extends AbstractAction{
            String s;
            public moveAction(String s){
                this.s=s;
            }
            
            public void actionPerformed(ActionEvent event){
                if(s.equals("up"))current.setFrame(current.getX(),current.getY()-1,SIDELENGTH, SIDELENGTH);
                if(s.equals("down"))current.setFrame(current.getX(),current.getY()+1,SIDELENGTH, SIDELENGTH);
                if(s.equals("right"))current.setFrame(current.getX()+1,current.getY(),SIDELENGTH, SIDELENGTH);
                if(s.equals("left"))current.setFrame(current.getX()-1,current.getY(),SIDELENGTH, SIDELENGTH);
                repaint();
            }
        }
        
        
    }
    Last edited by dendenmushi; 05-17-2012 at 06:30 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem with keybinding

    Quote Originally Posted by dendenmushi View Post
    without the "PROBLEM" part I can compile the code and works
    Darryl already told you how to fix it: put that code in a constructor.

  7. #7
    dendenmushi is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Problem with keybinding

    Quote Originally Posted by Fubarable View Post
    Darryl already told you how to fix it: put that code in a constructor.
    I have changed the post, now i can compile and run the program but the keys do nothing

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem with keybinding

    You may be using the wrong InputMap:

    Java Code:
    InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    I would use a different one, when in focused window or something similar.

  9. #9
    dendenmushi is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Problem with keybinding

    I have put the action inside the constructor but it still doesn't work,

    Java Code:
    package muovitest;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class MuoviTest{
        
        public static void main(String[] args){
            EventQueue.invokeLater(new Runnable()
            {
                public void run(){
                    MuoviFrame frame = new MuoviFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    class MuoviFrame extends JFrame{
        
        // constructor
        public MuoviFrame(){
            setTitle("MuoviTest");
            setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
            
            
            //add component to the frame
            
            MouseComponent component=new MouseComponent();
            add(component);
        }
        
        public static final int DEFAULT_WIDTH = 200;
        public static final int DEFAULT_HEIGHT = 200;
         
    }
    
    class MouseComponent extends JComponent{
        
        private Rectangle2D.Double current;
        private Point2D arrivo;
        private boolean raggiunto;
        private static final int SIDELENGTH = 10;
        private static final int ARRIVOLENGTH=6; 
        
        public MouseComponent(){
            
            current = new Rectangle2D.Double(MuoviFrame.DEFAULT_WIDTH/2 - SIDELENGTH / 2, MuoviFrame.DEFAULT_HEIGHT/2 - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
            arrivo = new Point2D.Double(Math.random() * MuoviFrame.DEFAULT_WIDTH,Math.random() * MuoviFrame.DEFAULT_HEIGHT);
            raggiunto= false;
            InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
            
            Action upAction = new moveAction("up");
            Action downAction = new moveAction("down");
            Action rightAction = new moveAction("right");
            Action leftAction = new moveAction("left");
            KeyStroke leftArrowKey = KeyStroke.getKeyStroke("LEFT");
            imap.put(leftArrowKey, "left");
            KeyStroke rightArrowKey = KeyStroke.getKeyStroke("RIGHT");
            imap.put(rightArrowKey, "right");
            KeyStroke upArrowKey = KeyStroke.getKeyStroke("UP");
            imap.put(upArrowKey, "up");
            KeyStroke downArrowKey = KeyStroke.getKeyStroke("DOWN");
            imap.put(upArrowKey, "down");
        
            ActionMap amap = getActionMap();
            amap.put("left", leftAction);
            amap.put("right", rightAction);
            amap.put("up", upAction);
            amap.put("down", downAction);
        
            addMouseMotionListener(new MouseMotionHandler());
        }
        
        
        
        
        public void paintComponent(Graphics g){
            Graphics2D g2 =(Graphics2D) g;
            
            //draw squares
                g2.draw(current);
                if(raggiunto==false){
                    g2.draw(new Rectangle2D.Double(arrivo.getX()- ARRIVOLENGTH / 2-1,arrivo.getY()- ARRIVOLENGTH / 2-1, ARRIVOLENGTH+1,ARRIVOLENGTH+1));
                    g2.setColor(Color.GREEN);
                    g2.fill(new Rectangle2D.Double(arrivo.getX()- ARRIVOLENGTH / 2,arrivo.getY()- ARRIVOLENGTH / 2, ARRIVOLENGTH,ARRIVOLENGTH));
                    g2.setColor(Color.BLACK);
                }
        }
        
        
        
        
        private class MouseMotionHandler implements MouseMotionListener{
            public void mouseMoved(MouseEvent event){
                //set cursor of mouse to cross hairs if it is inside a rectangle
                
                if (current.contains(event.getPoint())==false)setCursor(Cursor.getDefaultCursor());
                else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            }
            
            public void mouseDragged(MouseEvent event){
                int x = event.getX();
                int y = event.getY();
                //drag the current rectangle to center it at (x,y)
                current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2, SIDELENGTH, SIDELENGTH);
                if(current.contains(arrivo))raggiunto=true;
                repaint();
              
            }
        }
        
        public class moveAction extends AbstractAction{
            String s;
            public moveAction(String s){
                this.s=s;
            }
            
            public void actionPerformed(ActionEvent event){
                if(s.equals("up"))current.setFrame(current.getX(),current.getY()-1,SIDELENGTH, SIDELENGTH);
                if(s.equals("down"))current.setFrame(current.getX(),current.getY()+1,SIDELENGTH, SIDELENGTH);
                if(s.equals("right"))current.setFrame(current.getX()+1,current.getY(),SIDELENGTH, SIDELENGTH);
                if(s.equals("left"))current.setFrame(current.getX()-1,current.getY(),SIDELENGTH, SIDELENGTH);
                repaint();
            }
        }
        
        
    }
    thanks for the help

  10. #10
    dendenmushi is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Problem with keybinding

    Quote Originally Posted by Fubarable View Post
    You may be using the wrong InputMap:

    Java Code:
    InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    I would use a different one, when in focused window or something similar.
    THANK YOU now it works!

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem with keybinding

    Quote Originally Posted by dendenmushi View Post
    THANK YOU now it works!
    You're welcome!

Similar Threads

  1. Assistance on transforming a KeyListener to a KeyBinding?
    By OurOhnlyHope in forum AWT / Swing
    Replies: 1
    Last Post: 05-04-2012, 03:25 PM
  2. KeyBinding not responsive
    By Germoose in forum New To Java
    Replies: 3
    Last Post: 04-21-2012, 11:10 PM
  3. Keybinding
    By mine0926 in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 05:33 AM
  4. KeyBinding key released doesn't work so well -.-
    By Addez in forum New To Java
    Replies: 1
    Last Post: 09-22-2010, 12:09 PM
  5. KeyBinding Help
    By Lil_Aziz1 in forum New To Java
    Replies: 12
    Last Post: 07-27-2010, 03:58 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
  •