Quick Question about a basic game template

Hello all i have two questions about a game template that i quickly wrote up… I am used to writing applets but i need to make a standalone game this time.

1- How do i load an image? “getImage(getCodeBase(), “res/image.png”);” Isn’t working…
2- How do you use a keyListener. The usual way for applets isn’t working.

Help much appreciated :slight_smile:

Here is the code:


package com.ion.rpgnoname;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable, KeyListener{
	
	private static final long serialVersionUID = 1L;
	
	private static final int SCALE = 4;
	static int WIDTH = 125;
	static int HEIGHT = 125;
	static String NAME = "GAME UNNAMED" ;

	Image guy;
	
	int x = 0;
	int y = 0;
	
	Image dbImage;
	Graphics dbg;

	public void init () {
		setSize  (WIDTH, HEIGHT);
		setFocusable(true);
		setBackground(Color.BLACK);	
		addKeyListener(this);
                guy = getImage(getCodeBase(), "res/image.png");

	}
	
	public void start() {
		Thread th = new Thread(this);
		th.start();
	}

	public void run() {
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		while(true){
			repaint();
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}    
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}	
	}

	public void paint(Graphics g) {
		g.setColor(Color.BLACK);
		g.fillOval(x, y, 12, 12);
                g.drawImage(guy, 40, 40, this);
	}
	
	public void update(Graphics g){
		if (dbImage == null)
	    {
	        dbImage = createImage (this.getSize().width, this.getSize().height);
	        dbg = dbImage.getGraphics ();
	    }

	    dbg.setColor (getBackground ());
	    dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

	    dbg.setColor (getForeground());
	    paint (dbg);

	    g.drawImage (dbImage, 0, 0, this);
	}
	
	public static void main(String[] args){
		Game game = new Game();
		game.setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
		game.setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
		game.setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
		
		JFrame frame = new JFrame(Game.NAME);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		frame.add(game);
		frame.pack();
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
		game.start();
	}

	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_SPACE){
			x++;
			y++;
		}
	}
	public void keyReleased(KeyEvent e) {	
	}
	public void keyTyped(KeyEvent e) {	
	}
}

http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

google is your friend :D.

and as a rule of thumb, if something is a method in the Applet class, it is probably a method in the JFrame class (addKeyListener for example).

as for reaching into a jar file, read this:

hope I answered your questions
h3ckboy

  1. For the pictures you should create a Spritemanager, if you use them often.

  2. You get files in your workspace-direc. with “test-folder\Test.png”

public static Icon getIcon(String path) {
		Icon icon = null;;
		try {
		    icon = new ImageIcon(ImageIO.read(new File(path)));
		} catch (IOException e) {
		}
		return icon;
	}}

hope it helps :slight_smile:

edit: Keylistner :smiley:
You may don´t want to write all in one class :wink:
I add a player object and give it a InputHandler,

p = new Player("Spieler1", new InputHandler(keys.get(0) , keys.get(1) ,keys.get(2) , keys.get(3)));

keys is an array with the Integers of the keys.

In the InputHandler

public class InputHandler implements KeyListener, MouseListener, MouseWheelListener, MouseMotionListener{

	private int k1, k2, k3, k4;

	public InputHandler(int k1, int k2, int k3, int k4) {
		this.k1 = k1;
		this.k2 = k2;
		this.k3 = k3;
		this.k4 = k4;
		
		GamePanel.gp.addMouseListener(this);
		GamePanel.gp.addMouseWheelListener(this);
		GamePanel.gp.addMouseMotionListener(this);
		
	}[...]

you add all all the Listeners to the Panel. (I don´t add the Motion listener, because I don´t need the Listener in gamepanel)
You got any questions left, feel free to ask :slight_smile:

ImageIO.read(CLASS_NAME.class.getClassLoader().getResourceAsStream(FILEPATH));

-Nathan

Never use ImageIcon to load images, use the way I outlined in that thread h3ckboy linked, which StonePickaxes also mentioned :slight_smile:

I learned from the ImageIO king himself.

-Nathan

Thx for critics you fixed a bug in my code ;D

Thanks,

Seems to be working now
Cheers for the help.

Lucas ;D