Hey,
I have a problem with my keyboard event dispatchment. I thought Thread.yield() would be enough to give the keyboard time to react but I thought wrong. Is it possible to give the eventhread/ eventlistener ( not sure what it’s called) more time?
Without my loop the keyboard work great.
package net.tangledcode.mtm_game.game;
import java.awt.AWTEvent;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import net.tangledcode.mtm_game.entities.Player;
public class Game {
private static boolean[] keys = new boolean[1024];
private JFrame frame;
private Canvas canvas;
private BufferStrategy buffer;
private BufferedImage bi;
private GraphicsEnvironment ge;
private GraphicsDevice gd;
private GraphicsConfiguration gc;
public Game(JFrame frame) {
this.frame = frame;
this.frame.getContentPane().removeAll();
this.frame.update(this.frame.getGraphics());
this.frame.setIgnoreRepaint(true);
this.canvas = new Canvas();
this.canvas.setIgnoreRepaint(true);
this.canvas.setBounds(0, 0, 1200, 800);
this.frame.add(this.canvas);
this.frame.pack();
this.frame.setVisible(true);
this.canvas.createBufferStrategy(2);
this.buffer = this.canvas.getBufferStrategy();
this.ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
this.gd = this.ge.getDefaultScreenDevice();
this.gc = this.gd.getDefaultConfiguration();
this.bi = this.gc.createCompatibleImage(1200, 800);
this.canvas.requestFocus();
Keyboard.init();
}
public void start() {
Graphics graphics = null;
Graphics2D g2d = null;
Color background = Color.BLACK;
int fps = 0;
int frames = 0;
long totalTime = 0;
long timeDiff = 0;
long currentTime = System.currentTimeMillis();
long lastTime = currentTime;
Player player = new Player("../data/images/ships/player_01.png", 600, 400);
while(true) {
try {
lastTime = currentTime;
currentTime = System.currentTimeMillis();
timeDiff = currentTime - lastTime;
totalTime += currentTime - lastTime;
if(totalTime > 1000) {
totalTime -= 1000;
fps = frames;
frames = 0;
}
++frames;
g2d = this.bi.createGraphics();
g2d.setColor(background);
g2d.fillRect(0, 0, 1200, 800);
// draw Stuff here
player.draw(g2d);
player.doLogic(timeDiff);
player.doMovement(timeDiff); // checks if keys are pressed
g2d.setFont( new Font( "Helvetica", Font.PLAIN, 14 ) );
g2d.setColor( Color.GREEN );
g2d.drawString( String.format( "FPS: %s", fps ), 20, 20 );
graphics = this.buffer.getDrawGraphics();
graphics.drawImage(this.bi, 0, 0, null );
if(!this.buffer.contentsLost()) {
this.buffer.show();
}
} finally {
if(graphics != null) {
graphics.dispose();
}
if(g2d != null) {
g2d.dispose();
}
}
}
}
}
package net.tangledcode.mtm_game.game;
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Keyboard {
private static boolean[] keys = new boolean[1024];
public static void init() {
Toolkit.getDefaultToolkit().addAWTEventListener(new KeyHandler(), AWTEvent.KEY_EVENT_MASK);
}
public static void init(Component comp) {
comp.addKeyListener(new KeyHandler());
}
public static boolean isPressed(int key) {
return Keyboard.keys[key];
}
public static void setPressed(int key,boolean pressed) {
Keyboard.keys[key] = pressed;
}
private static class KeyHandler extends KeyAdapter implements AWTEventListener {
public void keyPressed(KeyEvent e) {
if (e.isConsumed()) {
return;
}
Keyboard.keys[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e) {
if (e.isConsumed()) {
return;
}
KeyEvent nextPress = (KeyEvent) Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent(KeyEvent.KEY_PRESSED);
if ((nextPress == null) || (nextPress.getWhen() != e.getWhen())) {
Keyboard.keys[e.getKeyCode()] = false;
}
}
public void eventDispatched(AWTEvent e) {
System.out.println("event dispatched");
if (e.getID() == KeyEvent.KEY_PRESSED) {
keyPressed((KeyEvent) e);
}
if (e.getID() == KeyEvent.KEY_RELEASED) {
keyReleased((KeyEvent) e);
}
}
}
}
