need help with java event listener

Started by
3 comments, last by Firetag 14 years, 2 months ago
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); } }
Advertisement
What's the problem?
ok well i figured out that i need the player.move(); in the while loop inside the run method but how do i use the event listener method inside another method?
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.
thanks i got it to work without having to do a dispatcher..although the dispatcher would probably provide for neater code...thanks for all you help

This topic is closed to new replies.

Advertisement