Im making an applet that moves a red ball using the WASD keys but it doesnt work for some reason help?

package Game;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class AppletOne extends Applet implements Runnable, KeyListener{
	int x_pos = 10;
	int y_pos = 100;
	int x_speed = 3;
	int y_speed = 3;
	int radius = 20;
	int appletsize_x = 300;
	int appletsize_y = 300;
	private Image dbImage;
	private Graphics dbg;
	
	public void init(){
		
	}
	
	public void start(){
		Thread th = new Thread(this);
		th.start();
	}
	
	public void stop(){
		
	}
	
	public void destroy(){
		
	}
	
	public void run(){
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		while(true){
			repaint(); 
		try{
			Thread.sleep (20); 
		}catch(InterruptedException ex){} 
		Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 
		}
	}
	
	public void update(Graphics g){
		if(dbImage == null){
			dbImage = createImage(this.getSize().width,this.getSize().height);
			dbg = dbImage.getGraphics();
		}
		
		dbg.setColor(getBackground());
		dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
		
		dbg.setColor(getForeground());
		paint(dbg);
		g.drawImage(dbImage, 0, 0, this);
	}
	
	public void paint(Graphics g){
		g.setColor(Color.red);
		g.fillOval(x_pos - radius, y_pos - radius, radius*2, radius*2);
	}

	
	public void keyPressed(KeyEvent e) {
		if(e.getKeyChar() == KeyEvent.VK_W){
			y_pos += 3;
			repaint();
			e.consume();
		}else if(e.getKeyChar() == KeyEvent.VK_S){
			y_pos -= 3;
			repaint();
			e.consume();
		}else if(e.getKeyChar() == KeyEvent.VK_D){
			x_pos += 3;
			repaint();
			e.consume();
		}else if(e.getKeyChar() == KeyEvent.VK_A){
			x_pos -= 3;
			repaint();
			e.consume();
		}
	}

	public void keyReleased(KeyEvent e) {
		
	}

	public void keyTyped(KeyEvent e) {
		
	}
	
}
public void init(){
		this.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyChar() == KeyEvent.VK_W){
					y_pos += 3;
					repaint();
					e.consume();
				}else if(e.getKeyChar() == KeyEvent.VK_S){
					y_pos -= 3;
					repaint();
					e.consume();
				}else if(e.getKeyChar() == KeyEvent.VK_D){
					x_pos += 3;
					repaint();
					e.consume();
				}else if(e.getKeyChar() == KeyEvent.VK_A){
					x_pos -= 3;
					repaint();
					e.consume();
				}
			}
			@Override
			public void keyReleased(KeyEvent e) { }
			@Override
			public void keyTyped(KeyEvent e) { }
		});
	}

Change your init() to this and it will work. Also, note that the KeyEvent does not differentiate between upper and lowercase; it assumes all entries are upper case. Therefore...hold shift :P

HERES THE WORKING CODE

package Game;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class AppletOne extends Applet implements Runnable, KeyListener{
	int x_pos = 10;
	int y_pos = 100;
	int x_speed = 3;
	int y_speed = 3;
	int radius = 20;
	int appletsize_x = 300;
	int appletsize_y = 300;
	private Image dbImage;
	private Graphics dbg;
	
	public void init(){
		addKeyListener(this);
	}
	
	public void start(){
		Thread th = new Thread(this);
		th.start();
		
	}
	
	public void stop(){
		
	}
	
	public void destroy(){
		
	}
	
	public void run(){
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		while(true){
			repaint(); 
		try{
			Thread.sleep (20); 
		}catch(InterruptedException ex){} 
		Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 
		}
	}
	
	public void update(Graphics g){
		if(dbImage == null){
			dbImage = createImage(this.getSize().width,this.getSize().height);
			dbg = dbImage.getGraphics();
		}
		
		dbg.setColor(getBackground());
		dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
		
		dbg.setColor(getForeground());
		paint(dbg);
		g.drawImage(dbImage, 0, 0, this);
	}
	
	public void paint(Graphics g){
		g.setColor(Color.red);
		g.fillOval(x_pos - radius, y_pos - radius, radius*2, radius*2);
	}

	
	public void keyPressed(KeyEvent e) {
		if(e.getKeyChar() == KeyEvent.VK_W){
			y_pos -= 3;
			repaint();
			e.consume();
		}else if(e.getKeyChar() == KeyEvent.VK_S){
			y_pos += 3;
			repaint();
			e.consume();
		}else if(e.getKeyChar() == KeyEvent.VK_D){
			x_pos += 3;
			repaint();
			e.consume();
		}else if(e.getKeyChar() == KeyEvent.VK_A){
			x_pos -= 3;
			repaint();
			e.consume();
		}
	}

	public void keyReleased(KeyEvent e) {
		
	}

	public void keyTyped(KeyEvent e) {
		
	}
	
}
This article has been dead for over six months. Start a new discussion instead.