Since I have used the thread.sleep() method, my graphics is updating way to slowly. How would I convert this into a timer?
The point of the applet is just to move the black dot around the screen based on the arrow keys that are pressed. (I am eventually going to make this into a snake game, but am taking it one step at a time.)
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.Timer; import java.applet.*; import java.util.*; import static java.lang.System.*; public class SnakeSimple extends Applet implements KeyListener{ int x = 0; int y = 0; byte direction; final byte UP = 1; final byte RIGHT = 2; final byte DOWN = 3; final byte LEFT = 4; public void init() { x = 100; y = 100; addKeyListener(this); } public void paint(Graphics g) { g.setColor(Color.black); g.fillOval(x, y, 10, 10); run(); } public void run(){ try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(direction==UP){ y-=10; } if(direction==DOWN){ y+=10; } if(direction==LEFT){ x-=10; } if(direction==RIGHT){ x+=10; } repaint(); } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==37){ direction = LEFT; } if(e.getKeyCode()==38){ direction = UP; } if(e.getKeyCode()==39){ direction = RIGHT; } if(e.getKeyCode()==40){ direction = DOWN; } repaint(); } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } }