Jump to content
  • Advertisement
Sign in to follow this  
hazle

why is this not working?

This topic is 5471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm writing a pong java applet and i'm part way through but i can't figure out why its not working. the current versoin should have a yello ball that bounces around the screen along with a blue rectangle that moves up and down when you press the aproprete key.my applet does all those things exept the moving up and down.here's the code: Main
import java.applet.Applet;
import java.util.*;
import java.awt.*;

public class main extends Applet implements Runnable {
	private Image       offscreenImage;
	private Graphics    offscr;
	private Thread      ticker;
	private boolean     running = false;
	private	Rectangle   bounds;
	private ball        ball;
        private player_paddle        player;
	private int         width, height;

	public void init() {
		width = size().width;
		height = size().height;
		bounds = new Rectangle(width, height);
                player = new player_paddle(50f,50f,30,10,height);
        // Initialize Ball's position and velocity
		ball = new ball(width / 3f, height / 4f, 1.5f,2.3f, 12, Color.yellow);
	}
	
	public void run () {
		while (running) {
			repaint(); 
			try {
				ticker.sleep(1000 / 30);
			} catch (InterruptedException e) { ; }
		}
	}
	
	public void paint (Graphics g) {
		if (offscr == null) {
			offscreenImage = createImage(width, height);
			offscr = offscreenImage.getGraphics();
		}
		// Draw checkerboard background
                
		int x2 = width >> 1;
		int y2 = height >> 1;
		offscr.setColor(Color.black);
		offscr.fillRect(0, 0, width, height);
		//offscr.fillRect(x2, y2, width - x2, height - y2);
		offscr.setColor(Color.white);
		//offscr.fillRect(x2, 0, width - x2, height - y2);
		//offscr.fillRect(0, y2, x2, y2);
                offscr.setColor(Color.black);
                offscr.drawRect(0,0,width,height);
		ball.move(bounds);
		ball.draw(offscr);
                player.draw(offscr);
		g.drawImage(offscreenImage, 0, 0, null);
	}

	public void update (Graphics g) {
		paint(g);
	}
	
	public synchronized void start () {
		if (ticker == null  ||  !ticker.isAlive()) {
			running = true;
			ticker = new Thread(this);
			ticker.setPriority(Thread.MIN_PRIORITY + 1);
			ticker.start();
		}
	}
	
	public synchronized void stop () {
		running = false;
	}
}
ball
import java.applet.Applet;
import java.util.*;
import java.awt.*;

public class ball {
	public float    x, y, dx, dy;
	private Color   color;
	private int     size;
	
	ball (float x, float y, float dx, float dy, int size, Color color) {
		this.x = x;
		this.y = y;
		this.dx = dx;
		this.dy = dy;
		this.color = color;
		this.size = size;
	}
	
	public void move (Rectangle bounds) {
		// Add velocity values dx/dy to position to get
		// ball's new position
		x += dx;
		y += dy;
		// Check for collision with left edge
		if (x < bounds.x && dx < 0) {
                        dx++;
			dx = -dx;
			x -= 2 * (x - bounds.x);
		}
		// Check for collision with right edge
		else if ((x + size) > (bounds.x + bounds.width) && dx > 0) {
                        dx++;
			dx = -dx;
			x -= 2 * ((x + size) - (bounds.x + bounds.width));
		}
		// Check for collision with top edge
		if (y < bounds.y && dy < 0) {
                        dy++;
			dy = -dy;
			y -= 2 * (y - bounds.y);
		}
		// Check for collision with bottom edge
		else if ((y + size) > (bounds.y + bounds.height) && dy > 0) {
                        dy++;
			dy = -dy;
			y -= 2 * ((y + size) - (bounds.y + bounds.height));
		}
	}
	
	public void draw (Graphics g) {
		g.setColor(color);
		g.fillOval((int) x, (int) y, size, size);
	}
}
player_paddle
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.*;

public class player_paddle implements KeyListener {
    
    float x;
    float y;
    int hight;
    int width;
    int roomhight;
    
    /** Creates a new instance of player_paddle */
    public player_paddle(float x,float y,int hight,int width,int roomhight){
        this.x = x;
        this.y = y;
        this.hight = hight;
        this.width = width;
        this.roomhight = roomhight;
    }
    public void draw(Graphics g){
        g.setColor(Color.blue);
        g.fillRect((int)x,(int)y,width,hight);
    }
    
    public void keyPressed(java.awt.event.KeyEvent keyEvent) {
        if((keyEvent.getKeyChar() == keyEvent.VK_UP)){
            y -= 5;
            // && (y > 0)
        }
        if((keyEvent.getKeyChar()== keyEvent.VK_DOWN)){
            y += 5;
            // && (y < roomhight)
        }
    }
    
    public void keyReleased(java.awt.event.KeyEvent keyEvent) {
    }
    
    public void keyTyped(java.awt.event.KeyEvent keyEvent) {
    }
    
}

thanks in advanced!

Share this post


Link to post
Share on other sites
Advertisement
Well, the first thing to find out is if your keypress event is occuring. Try having it put up a message box if it gets any key. If nothing happens, it means you're not receiving keypress info.

If the message box appears, figure out what keypress you've captured. Is it the right key?

And finally, if it's the right key, then are you moving the paddle correctly? There might be a problem there too.

Share this post


Link to post
Share on other sites
ok i tried that and found out that the key pressed event hasn't bean happening,but like i said before i still can't figure out why the program isn't working. i hope that some body can show me the error in the code.

Share this post


Link to post
Share on other sites
That's it. In the main Applet you need to addKeyListener(player), presumably as part of the init(). Currently you have an object that can handle keypresses, but the system has no idea that that's the object you want to *use* to handle the keypresses that are observed by the system.

When a key is pressed, one of the methods inside the Applet implementation (that you extend from) will search for a KeyListener attached to your Applet, which in your case you would like to be a player_paddle instance.

Share this post


Link to post
Share on other sites
i tried this.addKeyListener(player) but it still isn't working.
i'm sure part of it is that the key listener wasn't added though.

Share this post


Link to post
Share on other sites
Hey dont know if you have figured the problem out already but in case you havent.


The problem is that the up and down arrows do not generate characters. Which means that in the player paddle class you need to change getKeyChar to getKeyCode. Just for a sanity check I tried it out myself and I know it works. Just dont forget to add the addKeyListener in the main class.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!