Re: Problem with keybinding
please post the actual error message in its entirety.
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
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
Re: Problem with keybinding
ok, i have changed the code, and now i can compile but it doesn't work
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();
}
}
}
Re: Problem with keybinding
Quote:
Originally Posted by
dendenmushi
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.
Re: Problem with keybinding
Quote:
Originally Posted by
Fubarable
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
Re: Problem with keybinding
You may be using the wrong InputMap:
Code:
InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
I would use a different one, when in focused window or something similar.
Re: Problem with keybinding
I have put the action inside the constructor but it still doesn't work,
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
Re: Problem with keybinding
Quote:
Originally Posted by
Fubarable
You may be using the wrong InputMap:
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!
Re: Problem with keybinding
Quote:
Originally Posted by
dendenmushi
THANK YOU now it works!
You're welcome!