I have noticed that while holding a key down, there is a short delay between the key being read as being held down. What I mean is this; hold down any key and observe closely. The pattern goes like this: h.....hhhhhhhhhhhhhhhhhhhhhh
where .
symbolizes a pause. This is fine, because when we type, it would be harder trying to write a single letter e.g. 'i'.
But what if we want to make a game? I mean, we wouldn't like the delay. This is exacly my problem. So now, let me show you my code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tutorial3{
static MyPanel panel;
public static void main(String[] agrs){
createAndStartGui();
new gameplay();
}
static void createAndStartGui(){
JFrame f = new JFrame("tutorial 3");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setPreferredSize(new Dimension(500, 300));
f.addKeyListener(new MyKeyListener());
panel = new MyPanel();
f.add(panel);
f.pack();
f.setVisible(true);
}
}
class gameplay {
static String current_key = "stop";
public gameplay(){
begin();
}
void begin(){
while (true){
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Tutorial3.class.getName()).log(Level.SEVERE, null, ex);
}
Tutorial3.panel.user.move(current_key);
Tutorial3.panel.repaint();
System.out.println(current_key);
current_key = "stop";
}
}
static void key_event(){
}
}
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
switch(keyCode){
case KeyEvent.VK_UP:
gameplay.current_key = "up";
break;
case KeyEvent.VK_DOWN:
gameplay.current_key = "down";
break;
case KeyEvent.VK_LEFT:
gameplay.current_key = "left";
break;
case KeyEvent.VK_RIGHT:
gameplay.current_key = "right";
break;
default:
gameplay.current_key = "stop";
}
}
}
class MyRectangle{
int x;
int y;
public MyRectangle(int x, int y){
this.x = x;
this.y = y;
}
void move(String direction){
switch (direction){
case "up":
this.y -= 10;
break;
case "down":
this.y += 10;
break;
case "left":
this.x -= 10;
break;
case "right":
this.x += 10;
break;
default:
break;
}
}
}
class MyPanel extends JPanel{
MyRectangle user = new MyRectangle(10, 10);
public MyPanel(){
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g.drawRect(user.x, user.y, 10, 10);
}
}
The code basically creates a square on a JPanel and moves it around according to the user's inputs (right arrow would move the square 10 pixels to the right). Everything works fine, except that there is a delay which ruins the experience.
I had a read over this question: Ignoring delay when a key is held down? but sadly enough, it appears to only apply to JavaScript. Any help?
I would also appreciate links to existing questions of this type (which I might have missed).