Sign in to follow this  
Firetag

need help with java event listener

Recommended Posts

I need help with this java event listener. I am making a pong game and I already did a simpler version using only methods in the main class, but now I am trying to make it with classes and more object oriented programming. I think I am just passing variables wrong or something, but I am not sure so I'll just post all the code here. the main class import java.awt.*; import java.applet.*; public class AdvancedPong extends Applet implements Runnable { Thread thread; PongBall ball; ComPaddle computer; Player player; private Graphics dbg; private Image dbImage; public void init(){ ball = new PongBall(250,250,3,3); computer = new ComPaddle(); player = new Player(15, ball, 3); } public void start(){ thread = new Thread(this); thread.start(); } public void stop(){ thread = null; } public boolean keyDown(Event e, int key, boolean which){ if(key == Event.DOWN){which=true;player.move(e, key, which);} else if(key == Event.UP){which=false;player.move(e, key, which);} return true; } public void run(){ Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(true){ repaint(); ball.move(); try{ Thread.sleep(10); } catch(InterruptedException e){} Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void paint(Graphics g){ ball.paint(g); player.paint(g); } public void update (Graphics g){ if(dbImage == null){ dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics(); } //clear screen in bg dbg.setColor(getBackground()); dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); //draw elements in bg dbg.setColor(getForeground()); paint(dbg); //draw img on screen g.drawImage(dbImage,0,0,this); } } the player class import java.awt.Color; import java.awt.Event; import java.awt.Graphics; public class Player { private int score; private int side; //which side to put player on '15' or '485' private int y_pos ; private int real_y_pos; private int speed; private static final int size_x = 10; private static final int size_y = 50; private static PongBall ball; public Player(int side, PongBall ball,int speed){ this.ball = ball; this.side = side; this.speed = speed; this.y_pos = 100; } public boolean move(Event e, int key, boolean which){ if(which){ y_pos +=speed; } else if(!which){ y_pos -= speed; } return true; } public int getScore(){ return score; } public int getYPos(){ return y_pos; } public void paint(Graphics g){ g.setColor(Color.blue); g.fillRect(side, y_pos, size_x, size_y); } }

Share this post


Link to post
Share on other sites
You need to use the control's dispatcher to execute any code that might access your swing components. The dispatcher will ensure that your code is run in the appopriate UI thread. Google for "swing dispatcher" for more information.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this