public class GameMotor extends JFrame implements Runnable{
static double FPS = 30.0;
PanelJuego pj;
Vehicule vehicule;
boolean gameActive;
boolean isPressed;
public GameMotor(){
super("Race Game");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.vehicule = new Vehicule();
this.gameActive = true;
this.pj = new PanelJuego(vehicule);
this.add(pj);
this.pack();
this.setVisible(true);
Thread thread = new Thread(this);
thread.start();
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
isPressed = false;
}
@Override
public void keyPressed(KeyEvent e) {
vehicule.setVelocity(vehicule.getVelocity()+1);
System.out.println(vehicule.getVelocity());
isPressed = true;
}
});
}
@Override
public void run() {
try {
while(gameActive){
System.out.println(isPressed);
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
GameMotor gm = new GameMotor();
}
}
----------- second class ------
public class PanelJuego extends JPanel {
Vehicule vehicule;
public PanelJuego(Vehicule vehicule){
super();
this.vehicule = new Vehicule();
this.setPreferredSize(new Dimension(1000, 1000));
}
public void paintComponent(Graphics g){
super.paintComponents(g);
g.fillRect(this.vehicule.getVelocity(), 100, 100, 100);
}
}