Jump to content
  • Advertisement
Sign in to follow this  
Ueneth Echil

[java] Java programming PROBLEMO

This topic is 4057 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 am running into problems with my Computer Science program. I am trying to get the black box to move around, basically right now to the right with the d button. PlanetEdge.java
/*
 *	PlanetEdge
 *	Paul Adamski
 *
 *	PlanetEdge.java
 */
 
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.Timer;
import javax.swing.JFrame;
import java.util.ArrayList;
import java.lang.Character.*;
import java.awt.event.KeyListener;

public class PlanetEdge extends JFrame implements KeyListener
{
	private Timer timer;
	private Graphics dbg;			// Double buffering
	private Image dbImage;			// Double buffering
	private int SLEEP = 50;			// Higher SLEEP results in slower animation
	private int playerXSPEED = 1;
	private int playerYSPEED = 1;
	
	public PlanetEdge()
	{
		super("PlanetEdge");
		
		setSize(800,600);
		setVisible(true);
		
		ActionListener paintCaller = new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				repaint();
			}
		};
		
		timer = new Timer(SLEEP, paintCaller);
		timer.start();
		
		this.addKeyListener(this);
	}
	
	public void update (Graphics window)
	{
		// Initialize the buffer
		if (dbImage == null)
		{
			dbImage = createImage (this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics ();
		}
		
		// Clear the background image
		dbg.setColor (getBackground ());
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
			
		// Draw the frame in the background
		dbg.setColor (getForeground());
		paint (dbg);
			
		// Bring it to screen
		window.drawImage (dbImage, 0, 0, this);
	}

	
	public void keyPressed(KeyEvent e)
	{
		if (e.getKeyChar() == 'd')
			playerXSPEED += 50;
			
		System.out.println("Key hit detected");
	}

	public void keyReleased(KeyEvent e)
	{
		if (e.getKeyChar() == 'd')
			playerXSPEED += 50;
			
		System.out.println("Key hit detected");
	}

	public void keyTyped(KeyEvent e)
	{
		if (e.getKeyChar() == 'd')
			playerXSPEED += 50;
			
		System.out.println("Key hit detected");
    }
    
	public void paint(Graphics window) 
	{		
		// Set up the border and background
		window.drawRect(0,0,800,600);
		
		window.setColor(new Color(0,100,0));
		window.fillRect(1,1,799,599);
		
		System.out.println("Background drawn");
		//
		
		//Team VANU = new Team(1, 10, 10, window);
		Player PLAYER = new Player(1+playerXSPEED, 50, 300, window);
		System.out.println("Objects constructed");
	}
	
	public static void main(String args[])
	{
		PlanetEdge GAME = new PlanetEdge();
	}
}

Player.java
/*
 *	PlanetEdge
 *	Paul Adamski
 *
 *	Player.java
 */

import java.awt.*;

public class Player
{
	public Player(int teamChoice, int xPOS, int yPOS, Graphics window)
	{
		window.setColor(Color.black);
		
		window.fillRect(xPOS,yPOS,50,50);
	}
}

It compiles fine, and everything, but the movement doesn't change.

Share this post


Link to post
Share on other sites
Advertisement
Hmm, couple problems..

You never attach your ActionListener to anything with addActionListener(), so repaint() never gets called. I don't think ActionListener is what you want though. ActionListener is for listening to buttons and stuff. You probably want to call repaint() at the end of your keyPressed() method. Or set up a thread that periodically calls repaint().

This code:

new Player(1+playerXSPEED, 50, 300, window);

always draws the player at 50, 300. The only argument that changes is "teamChoice" (based on playerXSPEED), but in your actual drawing function, you don't even use teamChoice. So you're always drawing the same thing anyway.

Your "Player" class is just a glorified function that draws a box. You probably want to construct your Player object *once* (not on every repaint), and have it remember what its X and Y positions are.

Also, pressing keys changes playerXSPEED, but if you're going to be using a "speed" type variable, you need an update loop that periodically adds the "speed" to the X position.

Share this post


Link to post
Share on other sites
Thanks CaptainJester, that fixed it perfectly.

I really hate myself now for not realizing that was the problem, spent almost a whole hour trying to find out why it wasn't working.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!