Reputation: 55
I'm attempting to get an ImageIcon PNG image to move on my GUI, in Java. But I get this error:
MainClass.java:31: error: <identifier> expected
x = x--;
^
1 error
This is my code:
public class MainClass extends JPanel {
public static void main(String[] args) {
JLabel label;
JPanel panel = new JPanel();
ImageIcon image = new ImageIcon("Character Face Left - Bronze.png");
int x = 300;
int y = 300;
label = new JLabel(image);
label.setLayout(null);
JFrame frame = new JFrame("Rover: Bound to Earth");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.setSize(500,500);
frame.setVisible(true);
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), LEFT);
actionMap.put(LEFT, new AbstractAction() {
x--;
} );
label.setLocation(x,y);
}
}
What I want to have happen is, when the "A" key is pressed, the ImageIcon moves to the left. I've attempted to set the location of the ImageIcon with x and y, and have the x-- happen when the "A" key is pressed, but again, I'm getting the error.
Upvotes: 1
Views: 67
Reputation: 6329
Modified your code a bit:
// originally this extended JPanel, but was never used...
public class MainClass extends AbstractAction
{
// he label to be moved
JLabel label;
// the co-ordinates
int x = 300;
int y = 300;
// constructor... moved your code from main() to here...
public MainClass()
{
// instantiating panel and label
final JPanel panel = new JPanel();
final ImageIcon image = new ImageIcon( "Character Face Left - Bronze.png" );
// but as i do no have your image, i added some text...
label = new JLabel( "IMG", image, JLabel.CENTER );
// and specified he size of the label
label.setSize( 100, 50 );
// initial positioning
label.setLocation( x, y );
final JFrame frame = new JFrame( "Rover: Bound to Earth" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
// the frame is the one hat lays out its child components
// so it is the one to set he layout to null, not the label...
frame.setLayout( null );
frame.add( label );
frame.setSize( 500, 500 );
frame.setVisible( true );
// setting up he key event, however his still not working
InputMap inputMap = panel.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW );
ActionMap actionMap = panel.getActionMap();
// your LEFT identifier should be an object, that is unique,
// i just used the string "LEFT"....
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_A, 0 ), "LEFT" );
actionMap.put( "LEFT", this );
}
// the action's listener
@Override
public void actionPerformed( ActionEvent e )
{
// decrement x, and also move the label
x -= 10;
label.setLocation( x, y );
}
// a new main()
public static void main( String[] args )
{
// AWT uses a special thread to queue, dispatch and execute events
// the SWING GUI must run on the AWT Event Dispatch Thread
SwingUtilities.invokeLater( () ->
{
new MainClass();
} );
}
}
This code will run, but still not working, as the Key Press->Acion
transition is broken somewhere. I do not know much about Action
s to be real help here on.
Try asking about it in a new question...
Upvotes: 1