5 Replies - 292 Views - Last Post: 05 October 2012 - 01:46 PM Rate Topic: -----

#1 Lagaam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-October 12

Can't seem to get image to load

Posted 05 October 2012 - 09:22 AM

Ok so I've gone through a few tutorials on how to make a 2D game engine in Java. I'm stuck at this one point though. I'm trying to load a buffered image, but for some reason it doesn't show when I test the game. I'll post code and hopefully someone can tell me what I'm doing wrong and point me in the right direction. I would appreciate it if someone could walk me through what I'm doing wrong and not just paste a working code for me, as I've asked for help on other sites where I've got that kind of help, but I don't end up learning much from that. I want to be able to learn from what ever mistake I've made!~ :D

engine <- (The 2D engine I'm working on)
package rand.LearningJava.Lagaan;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class engine {

	public static void start(Game game) {
		JFrame frame = new JFrame(game.getTitle());
		frame.setSize(game.width(), game.height());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GameCanvas canvas = new GameCanvas(game);
		frame.add(canvas);
		frame.setVisible(true);
		GameLoop loop = new GameLoop(game, canvas);
		loop.start();
	}
}

abstract class Game implements KeyListener, MouseListener, MouseMotionListener {
	
	protected String title = "GameEngine";
	protected int width = 640, height = 480, delay = 30;
	protected boolean over;
	
	public abstract void init();
	public abstract void update();
	public abstract void draw(Graphics2D g);
	
	public String getTitle() {
		
		return title;
	}
	
	public int width() {
		return width;
	}
	
	public int height() {
		return height;
	}
	
	public int getDelay() {
		return delay;
	}
	
	public boolean isOver() {
		return over;
	}
	
	@Override
	public void mouseDragged(MouseEvent e) {
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		
	}
	
}

class GameCanvas extends JComponent {
	
	private static final long serialVersionUID = 1L;
	private final Game game;
	
	public GameCanvas(Game game) {
		this.game = game;
		addKeyListener(this.game);
		addMouseListener(this.game);
		addMouseMotionListener(this.game);
		requestFocus();
	}
	
	public void paintComponent(Graphics2D g) {
		game.draw(g);
	}
	
}

class GameLoop extends Thread {
	
	private final Game game;
	private final GameCanvas canvas;
	
	public GameLoop(Game game, GameCanvas canvas) {
		this.game = game;
		this.canvas = canvas;
	}
	
	public void run() {
		game.init();
		
		while (!game.isOver()) {
			game.update();
			canvas.repaint();
			try {
				Thread.sleep(game.getDelay());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


gameTest <- (The actual game)
package rand.LearningJava.Lagaan;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class gameTest extends Game {
	
	public static final void main(String[] args) {
		engine.start(new gameTest());
	}
	
	public BufferedImage player;
	
	Graphics g;
	
	public gameTest() {
		title = "gameTest";
		
		try {
			player = ImageIO.read(new File("images/FinalFantasy3Sheet1.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void init() {
		
	}

	@Override
	public void update() {
		
	}

	@Override
	public void draw(Graphics2D g) {
		g.drawImage(player, 0, 0, null);
	}
}



Thank you in advance for any help you can give me!

Is This A Good Question/Topic? 0
  • +

Replies To: Can't seem to get image to load

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Can't seem to get image to load

Posted 05 October 2012 - 09:31 AM

Lets take a look at your paintComponent method.

public void paintComponent(Graphics2D g) {
	game.draw(g);
}



This is not overriding JComponent's method paintComponent, so paintComponent is NEVER being called. The correct argument for paintComponent would be a Graphics object. NOT a Graphics2D object. So you need to make your the parameter a Graphics object. Then inside the method cast the Graphics object into a Graphics2D. Then you can use that for your draw method.

If you need anymore help just ask :)
Was This Post Helpful? 1
  • +
  • -

#3 Lagaam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-October 12

Re: Can't seem to get image to load

Posted 05 October 2012 - 09:41 AM

You sir are a hero. You smell that? That's the smell of win!~ ;)

I will say I didn't quite understand how to go about what you were saying. I just changed Graphics2D to Graphics in the paintComponent method and cast Graphics2D to Graphics object "g". Thank you for helping me solve my issue, but I would like to understand:

1.) why my paintComponent wasn't working (I'm relatively new to java and been teaching myself, so maybe just a little more information about what was going wrong?)

2.) No idea what casting is. I use eclipse and I've seen it give me the suggested fix to cast something and seen what it looks like, but I still don't know what it's actually doing. Could you explain that please?

Thank you so much again!!!~
Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Can't seem to get image to load

Posted 05 October 2012 - 10:16 AM

View PostLagaam, on 05 October 2012 - 09:41 AM, said:

1.) why my paintComponent wasn't working (I'm relatively new to java and been teaching myself, so maybe just a little more information about what was going wrong?)

2.) No idea what casting is. I use eclipse and I've seen it give me the suggested fix to cast something and seen what it looks like, but I still don't know what it's actually doing. Could you explain that please?


1.)
The paintComponent(Graphics g) method is automatically called by Java when you have a Component. You can override this method by putting it in your own class, and it will automatically be called. If you choose to not override this method, then the default (nothing) will happen. However. You wrote a paintComponent(Graphics2D g) (Graphics is not a Graphics2D) This does not match the paintComponent(Graphics g). Since you did not override the method, nothing happened. It was never getting called.

To show this I can create this.

public class GameTest extends MethodTest{

	public static void main(String[] args) {
		new GameTest();
	}
	
	public GameTest() {
		print("Hello"); //prints Defualt Print: Hello
	}
	
	public void print(int i) {
		System.out.println("Printing a Integer: " + i);
	}
}

class MethodTest{
	public void print(String s) {
		System.out.println("Defualt Print: " + s);
	}
}



So here we have GameTest extending MethodTest. Since GameTest does not have its own print method with a String argument it just uses MethodTest's default method. Now lets say we don't want the default print(String s) method.

public class GameTest extends MethodTest{

	public static void main(String[] args) {
		new GameTest();
	}
	
	public GameTest() {
		print("Hello");
	}
	
	public void print(String s) {
		System.out.println("GameTest's print String: " + s); //prints GameTest's print String: Hello.
	}
	
	public void print(int i) {
		System.out.println("Printing a Integer: " + i);
	}
}

class MethodTest{
	public void print(String s) {
		System.out.println("Defualt Print: " + s);
	}
}



Notice how even though we had a print method in GameTest the entire time but it did nothing since it takes a int and not a String so it was never called.



2)
Casting is a way to make a broader object into a more specific object. Just like Graphics and Graphics2D.
Graphics2D extends Graphics which makes Graphics2D a Graphics object, but Graphics is not a Graphics2D object. Since Graphics is a broader form of Graphics2D we can make it into a Graphics2D object.

Graphics2D g2 = (Graphics2D) g;


Was This Post Helpful? 1
  • +
  • -

#5 Lagaam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-October 12

Re: Can't seem to get image to load

Posted 05 October 2012 - 01:21 PM

Oh I see! Even though Graphics2D extends Graphics, it's still its own class with its own methods. The paintComponent() method in the JComponent class uses Graphics when running when running, and since I was trying to use Graphics2D which wasn't in the original method, it didn't have the Graphics class to work with and so had nothing to do. Is that right?

In real life a cast is supposed to support a broken leg, and java the theory is relatively the same, right? If I make an object from a class and set it equal to the object made from a broader class, I can give it the same properties as the broader object. However, I would get an error cause there would be some data that the more precise object couldn't use, so when setting the narrow object to equal the broader object, I cast the broader object with the narrower "Class" (the class used to make the narrower object) so that it only supports the data that the narrow class can use, and the rest sort of falls away. Is my understanding correct?

And thank you again. Thats been causing me so much trouble over the last week, and I couldn't find anyone else on the internet that posted about the same problem.
Was This Post Helpful? 0
  • +
  • -

#6 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Can't seem to get image to load

Posted 05 October 2012 - 01:46 PM

View PostLagaam, on 05 October 2012 - 01:21 PM, said:

Oh I see! Even though Graphics2D extends Graphics, it's still its own class with its own methods. The paintComponent() method in the JComponent class uses Graphics when running when running, and since I was trying to use Graphics2D which wasn't in the original method, it didn't have the Graphics class to work with and so had nothing to do. Is that right?

In real life a cast is supposed to support a broken leg, and java the theory is relatively the same, right? If I make an object from a class and set it equal to the object made from a broader class, I can give it the same properties as the broader object. However, I would get an error cause there would be some data that the more precise object couldn't use, so when setting the narrow object to equal the broader object, I cast the broader object with the narrower "Class" (the class used to make the narrower object) so that it only supports the data that the narrow class can use, and the rest sort of falls away. Is my understanding correct?

And thank you again. Thats been causing me so much trouble over the last week, and I couldn't find anyone else on the internet that posted about the same problem.


You are close on the casting part. I don't really have the time to type up something about casting. I did find a nice article you can read on it.

Type Casting
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1