Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

KeyListener Being Smeggy

843807Apr 30 2008 — edited May 6 2008
Firstly, sorry if this is in the wrong board. I always seem to pick the wrong one around here. =\

More to the point, I'm quite a bit of a java n00b still and I'm trying to make a basic lil Pong game applet just to test my knowledge thus far.
I'm still trying to work on threading the paddles seperately so they can move smoothly and independant of eachother, and I think it's going ok so far but I'm having a problem with overloading (or overriding, I get the two mixed up, yeah told you I'm new at this >_> ...actually, I'm pretty sure it's overloading, I'll go with that final answer) a constructor to try and add the KeyListener.

I get the following error at compile time:
PingPong4.java:105: cannot find symbol
symbol : method addKeyListener(Paddle)
location: class Paddle addKeyListener(this);
And here's the program thus far:
/*
<applet code="PingPong4" width=500 height=300></applet>
*/

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

public class PingPong4 extends Applet {
	
	Paddle leftpaddle, rightpaddle;
	MainThread mainthread;
	
	public void init() {
	
		this.setBackground(Color.yellow);
	
	}
	
	public void start() {
	
		leftpaddle = new Paddle(20, 150, true); //creates seperate object for both paddles and gives them their initial values
		rightpaddle = new Paddle(475, 150, false);
		mainthread = new MainThread("controlthread"); //starts up main thread which'll make stuff go
	
	}
	
	public void stop() {	
	}
	
	public void destroy() {
	}
	
	public void paint(Graphics g) {	

		leftpaddle.drawPaddle(g, leftpaddle.lpx, leftpaddle.lpy);
		rightpaddle.drawPaddle(g, rightpaddle.rpx, rightpaddle.rpy);
	
	}
	
}

class MainThread extends Thread {

	Paddle paddleMethods = new Paddle();

	MainThread(String name) {	
	
		super(name);
		start();
	
	}

	public void run() {
	
		try {
		
			//infinite loop in thread to keep everything moving and to constantly run movement and painting methods
			for( ; ; ) {
			
				Thread.sleep(50);
				paddleMethods.movePaddle();
			
			}
			
		}
		
		catch(InterruptedException exc) {
		
			System.out.println("OH NOES");
			
		}
	
	}

}

//paddle class
class Paddle implements KeyListener {

	int lpx, lpy, rpx, rpy;
	boolean rol, lbu = false, lbd = false, rbu = false, rbd = false;	

	Paddle(int x, int y, boolean rol) {
	
		//determines if it is the left or right paddle
		if(rol) {
		
			lpx = x;
			lpy = y;
			
		}
		
		else {
		
			rpx = x;
			rpy = y;
			
		}
	
	}
	
	Paddle() {
	
		addKeyListener(this); //hrmmmm
	
	}
	
	//lets program know when specific buttons are no longer pushed down
	public void keyReleased(KeyEvent e) {
	
		int i = e.getKeyCode();
	
		switch(i) {
	
			case 87: // 'w' key, up
				lbu = false;
				break;
			case 83: // 's' key, down
				lbd = false;
				break;
			case 38: //cursor key, up
				rbu = false;
				break;
			case 40: //cursor key, down
				rbd = false;
				break;
			
		}
	
	}
	
	public void keyTyped(KeyEvent e) {
	}
	
	//lets program know when specific buttons are pushed down
	public void keyPressed(KeyEvent e) {
	
		int i = e.getKeyCode();
	
		switch(i) {
	
			case 87: // 'w' key, up
				lbu = true;
				break;
			case 83: // 's' key, down
				lbd = true;
				break;
			case 38: //cursor key, up
				rbu = true;
				break;
			case 40: //cursor key, down
				rbd = true;
				break;
			
		}
	
	}
	
	//paints paddle
	public void drawPaddle(Graphics g, int x, int y) {
	
		g.setColor(Color.black);
		g.fillRect(x, y, 5, 25);
		
	}
	
	//checks to see if button is pushed down and if it is moves paddle co-ordinate accordingly
	public void movePaddle() {
	
		if(lbu) lpy++;
		if(lbd) lpy--;
		if(rbu) rpy++;
		if(rbd) rpy--;
	
	}

}
Also, if anybody picks up on anything I'm doing wrong or could do better via a different way please mention.
I'm still very much learning and trying to get rid of the odd bad habit here or there. ^^

Thanks in advance!

Edited by: ThePermster on Apr 30, 2008 4:05 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 3 2008
Added on Apr 30 2008
4 comments
136 views