I can't animate spaceship.png nor update the image to animate it.
Need to animate it using paint method (could animate it using ImageIcons and labels) in order to rotate.
So, rewritten it but now i can't even update its' current position.
- How can i make animation with a key listener ?
- Why can't i animate the image that's being painted?
public class MyPanel extends JPanel implements KeyListener{
final int PANEL_WIDTH = 1920;
final int PANEL_HEIGHT = 1080;
Image spaceship;
Image spaceship2;
Image spaceship3;
Image Alien1;
Image Alien2;
Image AlienBoss;
Image Asteroid1;
Image Asteroid2;
Timer timer;
int xVelocity = 1;
int yVelocity = 10;
int x = 0;
int y = 0;
MyPanel(){
this.setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
this.setBackground(new Color(0x080e12));
this.addKeyListener(this);
spaceship = new ImageIcon("D:\\Users\\Xigmatek\\eclipse-workspace\\SpaceGameRev\\src\\spaceship.png").getImage();
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(spaceship, x, y, null);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case 87: x -=xVelocity;
repaint();
break;
case 83: x +=xVelocity;
repaint();
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Sorry if i have pasted too much of code but am kind of newbie so, sorry :/