this is not a java problem, what you need to do is decrease your computers repeat delay. you do that by going to the control panel and double-clicking on keyboard. then on one of the tabs will be a slider called Repeat delay. drag that all the way down as close to short as you can. hope that helps
Well, what I do is make a while loop, when the button is pressed
it will go through the loop, and do whatever that button is
suppose to do.
void up_Pressed(){
while(button_pressed){
//do animation or whatever
}
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP){
up_Pressed();}
}
here's a simple example for you:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="Example.class" width=200 height=300></applet> */
public class Example extends Applet implements Runnable, KeyListener {
int w,h,dir,spd;
Rectangle paddle;
Image buffer;
public void init() {
dir = 0; // paddle isn't moving
spd = 2; // speed of the padddle
w = getSize().width;
h = getSize().height;
paddle = new Rectangle(20,0,10,50);
paddle.y = h/2-(paddle.height/2);
buffer = createImage(w,h);
new Thread(this).start();
addKeyListener(this);
}
public void run() {
while (true) {
paddle.y += dir; // move the paddle
/* keep the paddle in bounds */
if (paddle.y < 0) paddle.y = 0;
if (paddle.y > h-paddle.height) paddle.y = h-paddle.height;
repaint();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
break;
}
}
}
public void paint(Graphics g) {
Graphics2D gfx = (Graphics2D)buffer.getGraphics();
gfx.setColor(Color.white);
gfx.fillRect(0,0,w,h);
gfx.setColor(Color.black);
gfx.drawRect(0,0,w-1,h-1);
gfx.fill(paddle);
g.drawImage(buffer,0,0,this);
}
public void update(Graphics g) {
paint(g);
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
dir = -spd; // paddle will go up now
break;
case KeyEvent.VK_DOWN:
dir = spd; // paddle will go down now
break;
default:
break;
}
}
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if (code == e.VK_UP || code == e.VK_DOWN) {
dir = 0; // paddle will go nowhere now
}
}
public void keyTyped(KeyEvent e) {}
}