Results 1 to 11 of 11
Thread: Problem with keybinding
- 05-17-2012, 04:57 PM #1
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
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
the problem is between "//PROBLEM", sorry for my bad english ^^ and thank youJava 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(); } } }
-
Re: Problem with keybinding
please post the actual error message in its entirety.
- 05-17-2012, 05:10 PM #3
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
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
- 05-17-2012, 05:49 PM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-17-2012, 06:23 PM #5
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
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.
-
Re: Problem with keybinding
- 05-17-2012, 06:37 PM #7
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
-
Re: Problem with keybinding
You may be using the wrong InputMap:
I would use a different one, when in focused window or something similar.Java Code:InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
- 05-17-2012, 06:45 PM #9
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Problem with keybinding
I have put the action inside the constructor but it still doesn't work,
thanks for the helpJava 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(); } } }
- 05-17-2012, 06:48 PM #10
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
-
Similar Threads
-
Assistance on transforming a KeyListener to a KeyBinding?
By OurOhnlyHope in forum AWT / SwingReplies: 1Last Post: 05-04-2012, 03:25 PM -
KeyBinding not responsive
By Germoose in forum New To JavaReplies: 3Last Post: 04-21-2012, 11:10 PM -
Keybinding
By mine0926 in forum New To JavaReplies: 4Last Post: 01-11-2011, 05:33 AM -
KeyBinding key released doesn't work so well -.-
By Addez in forum New To JavaReplies: 1Last Post: 09-22-2010, 12:09 PM -
KeyBinding Help
By Lil_Aziz1 in forum New To JavaReplies: 12Last Post: 07-27-2010, 03:58 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks