Hey guys, I am having trouble with my java pacman game. What I have currently is the basics. I am reading in my maze from a txt file which is working. I am stumpted on how to replace the W for my walls with my jpeg of a blue wall(imgGhost in this case). Can anyone point me in the right direction ? I have attached my maze.txt to demonstrate.

Thanks in advance.

Here is my code so far:

import java.awt.*;
import javax.swing.*;
import java.io.*;

public class pacman3 extends JFrame {
	private static final long serialVersionUID = 1L;
	private static final Dimension WindowSize = new Dimension(600,600);
	private Image imgGhost;
    private ImageIcon icon;
	public pacman3() {
		this.setTitle("Pacman, or something..");
		Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		int x = screensize.width/2 - WindowSize.width/2;
		int y = screensize.height/2 - WindowSize.height/2;
		setBounds(x, y, WindowSize.width, WindowSize.height);
		icon = new ImageIcon("C:/Ghost1.png");
		imgGhost = icon.getImage();
	public void paint(Graphics g) {
		g.setFont(new Font("Courier New", Font.PLAIN, 18));
		int ypos = 45;
		String line=null;

		try {
			BufferedReader reader = new BufferedReader(new
			do {
				try {
					if(line !=null && line.startsWith("#")){
					else if(line !=null){
						g.drawString(line, 20, ypos);
						ypos += 20;
					line = reader.readLine();
				} catch (IOException e) { }
			while (line != null);
			try {
			} catch (IOException e) { }
		} catch (FileNotFoundException e) { }
	public static void main(String [ ] args) {
		pacman3 w = new pacman3();

You can draw your image anywhere in your JFrame using drawImage(...) in the paintComponent method (you should normally override paintComponent, not paint).
eg see:
Divide your JFrame (conceptually) into a grid of element size equal to the size of your image, then use the text file to determine which at which grid coordinates to draw the image

Edited 6 Years Ago by JamesCherrill: n/a

This article has been dead for over six months. Start a new discussion instead.