import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Timer;
public class Main extends Applet implements Runnable, ActionListener {
private static final long serialVersionUID = 1L;
int p1x = 10, p1y = 10, p2x = 570, p2y = 580;
boolean p1u = false, p1d = false, p1l = false,p1r = true,
p2u = false, p2d = false, p2r = false, p2l = true;
public void init() {
addKeyListener(hello);
setFocusable(true);
}
Timer repainting = new Timer(1/10, new ActionListener() {
public void actionPerformed(ActionEvent evx) {
repaint();
}
});
Timer p1up = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evx) {
p1y -= 2;
}
});
Timer p1left = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evx) {
p1x -= 2;
}
});
Timer p1right = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evx) {
p1x += 2;
}
});
Timer p1down = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evx) {
p1y += 2;
}
});
Timer p2up = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evx) {
p2y -= 2;
}
});
Timer p2left = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evx) {
p2x -= 2;
}
});
Timer p2right = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evx) {
p2x += 2;
}
});
Timer p2down = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evx) {
p2y += 2;
}
});
public void paint(Graphics g) {
//Players
g.fillRect(p1x, p1y, 15, 15);
g.fillRect(p2x, p2y, 15, 15);
}
public KeyListener hello = new KeyListener() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
p1up.start();
repaint();
}
if (key == KeyEvent.VK_A) {
p1left.start();
repaint();
}
if (key == KeyEvent.VK_S) {
p1down.start();
repaint();
}
if (key == KeyEvent.VK_D) {
p1right.start();
repaint();
}
if (key == KeyEvent.VK_UP) {
p2up.start();
repaint();
}
if (key == KeyEvent.VK_LEFT) {
p2left.start();
repaint();
}
if (key == KeyEvent.VK_DOWN) {
p2down.start();
repaint();
}
if (key == KeyEvent.VK_RIGHT) {
p2right.start();
repaint();
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
p1up.stop();
repaint();
}
if (key == KeyEvent.VK_A) {
p1left.stop();
repaint();
}
if (key == KeyEvent.VK_S) {
p1down.stop();
repaint();
}
if (key == KeyEvent.VK_D) {
p1right.stop();
repaint();
}
if (key == KeyEvent.VK_UP) {
p2up.stop();
repaint();
}
if (key == KeyEvent.VK_LEFT) {
p2left.stop();
repaint();
}
if (key == KeyEvent.VK_DOWN) {
p2down.stop();
repaint();
}
if (key == KeyEvent.VK_RIGHT) {
p2right.stop();
repaint();
}
}
@SuppressWarnings("unused")
public void keyTyped(KeyEvent e) {
int key = e.getKeyCode();
repaint();
}
};
public void start(){}
public void run(){}
public void stop(){}
public void destroy(){}
public void actionPerformed1(ActionEvent arg0) {
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
The problem is that when I try to move a square, it lags for about a second and then starts moving. I'm pretty sure this isn't the same issue as the windows keyboard thing, where if you hold for example "f" down, it writes one "f", then writes them rapidly. Thanks for any help or feedback provided. Also, is there a way I can do this with floats?

New Topic/Question
Reply



MultiQuote




|