Reputation: 3
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.
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 :/
Upvotes: 0
Views: 56
Reputation: 66
The problem with the JPanel you created is that it is not focusable, i.e it does not register key events, it can be fixed with a simple line of code setFocusable(true)
, and I noticed that you update the frame only once the space ship moves, but it is always a good practice to update it once every 60'th of a second, or any frames per second, you can do this by implementing ActionListener
and passing the class to a Timer
.
and I changed the keys to their respective variable(VK_W and VK_S)
Here is the updated code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
// in order to repeat after set delay, the class shall implement Action Listener and pass it to a timer(shown below)
public class MyPanel extends JPanel implements KeyListener, ActionListener {
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);
// set this JComponent focusable so that it can register keyEvents
setFocusable(true);
spaceship = new ImageIcon("res\\spaceship.png").getImage();
// pass this class to the timer to repeat after 1000/60 eth of a second(60 fps)
Timer timer = new Timer(1000/60, this);
timer.start();
}
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) {
// I changed 87 and 83 to their respective VK value
switch(e.getKeyCode()) {
case KeyEvent.VK_W: x -=xVelocity;
repaint();
break;
case KeyEvent.VK_S: x +=xVelocity;
repaint();
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
// Override this method from ActionListener
@Override
public void actionPerformed(ActionEvent e){
repaint();
}
}
Please watch this video, I demonstrated the output in this video.
By the way, you used w and s for horizontal movement(gamedev advice).
Upvotes: 1