Please Help Debug This Simple "game"

JFrame won't appear but program compiles/runs

Page 1 of 1

2 Replies - 530 Views - Last Post: 26 June 2009 - 04:46 PM Rate Topic: -----

#1 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Please Help Debug This Simple "game"

Post icon  Posted 26 June 2009 - 03:41 PM

Hello,

I'm trying to learn java 2d game programming and have written a simple program consisting of 3 classes, the sole purpose of which is to get a fighter plane image drawn onto the screen which the user can control with the arrow keys. I know there are better ways to do things than I have so far in this code -- the issue I want to focus on first with this post, however, is: why doesn't my JFrame appear on the screen with the JPanel drawn into it? Nothing appears. The prgram does compile and run though.

Here is my code:

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.*;
import java.io.IOException;

public class Fighter {
	BufferedImage fighter;
	int fighterSpeed = 5;
	int x;
	int y;


	public Fighter(){
		try{
			fighter = ImageIO.read(getClass().getResource("fighter.png"));
		}
		catch(IOException e) {
			System.out.println("Load image error: " + e.getMessage());
		}

	}
}



import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.awt.Graphics;

public class MoveFighter extends JPanel implements KeyListener  {
	private static final int PWIDTH = 500;
	private static final int PHEIGHT = 500;
	private Thread animator;
	Fighter fighter;
	boolean gameRunning = true;

	public MoveFighter() {
		setBackground(Color.BLACK);
		setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
		setFocusable(true);
		requestFocus();

		addKeyListener(this);
		fighter = new Fighter();

	}

	public void addNotify() {
		super.addNotify();
		startGame();
	}
	public void keyPressed(KeyEvent key){
		int keyCode = key.getKeyCode();

		switch(keyCode) {
			case KeyEvent.VK_LEFT:
			fighter.x -= fighter.fighterSpeed;
			break;

			case KeyEvent.VK_RIGHT:
			fighter.x += fighter.fighterSpeed;
			break;

			case KeyEvent.VK_UP:
			fighter.y += fighter.fighterSpeed;
			break;

			case KeyEvent.VK_DOWN:
			fighter.y -= fighter.fighterSpeed;
			break;

		}
	}
	public void keyReleased(KeyEvent key) {
		//
	}
	public void keyTyped(KeyEvent key) {
		//
	}
	private void startGame() {
		//gameUpdate();
		//gameRender();

		while(gameRunning == true){
		repaint();
	}

	}
	private void gameUpdate(){
		//
	}
	private void gameRender(){

	}
	public void paintComponent(Graphics g) {
		g.drawImage(fighter.fighter, fighter.x, fighter.y, null);
	}

}



import javax.swing.*;

public class FighterFrame extends JFrame {
	MoveFighter moveFighter = new MoveFighter();

	public FighterFrame(){

		add(moveFighter);
		setTitle("Move The Red Dot, Dude!");
		setLocationRelativeTo(null);
		setVisible(true);
		setSize(500,500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
	public static void main(String[] args){
		FighterFrame fighterFrame = new FighterFrame();

	}
}




Thanks in advance for all your help!!

Irongleet

Is This A Good Question/Topic? 0
  • +

Replies To: Please Help Debug This Simple "game"

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Please Help Debug This Simple "game"

Posted 26 June 2009 - 03:54 PM

Just by briefly looking at it, it looks like your not actually placing your stuff onto somthing like a content pane, but rather the frame itself. Start by trying this change, to see if you JFrame shows up
import javax.swing.*;

public class FighterFrame extends JFrame {
	MoveFighter moveFighter = new MoveFighter();
	private JPanel contentPane;

	public FighterFrame(){
	  
		this.setTitle("Move The Red Dot, Dude!");
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setSize(500,500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
		contentPane = (JPanel)this.getContentPane();
		contentPane .add(moveFighter);

	}
	public static void main(String[] args){ 
	SwingUtilities.invokeLater(new Runnable(){
		public void run(){
			new FighterFrame();
		}
	});
	
}
}


This post has been edited by nick2price: 26 June 2009 - 03:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Please Help Debug This Simple "game"

Posted 26 June 2009 - 04:46 PM

That seems to have helped a bit -- a frame does show up onscreen now, only its full of this weird jumble of gobbledygook, stuff from other windows on my system, etc... I've never seen this happen before.

Any thoughts?

Irongleet
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1