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..");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBackground(Color.BLACK);
		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);
		setVisible(true);
		icon = new ImageIcon("C:/Ghost1.png");
		imgGhost = icon.getImage();
		repaint();
	}
	public void paint(Graphics g) {
		g.setFont(new Font("Courier New", Font.PLAIN, 18));
		g.setColor(Color.WHITE);
		int ypos = 45;
		String line=null;

		try {
			BufferedReader reader = new BufferedReader(new
					FileReader("C:/maze.txt"));
			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 {
				reader.close();
			} catch (IOException e) { }
		} catch (FileNotFoundException e) { }
	}
	public static void main(String [ ] args) {
		pacman3 w = new pacman3();
	}
}

Recommended Answers

All 2 Replies

bump ?

You can draw your image anywhere in your JFrame using drawImage(...) in the paintComponent method (you should normally override paintComponent, not paint).
eg see:
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html
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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.