Hi all,
I need to write an algorithm that can find intersections, dead-ends, and turns within a maze. Below is the code I currently have. You're welcome to read through it, though I'm not sure if it will help in any way.
The basic information:
- Maze image file is a jpeg of two tones (colored background with white paths)
- Maze is loaded in as a bufferedImage
After loading it in as a bufferedImage, I've messed around with changing pixel color by looping through each pixel of the maze (in the paint method); however, I do not know how I could loop through and determine whether or not a path turns or ends up in a dead-end, etc.
I'm just not sure how I can iterate through a 2D array of pixels to determine turns and intersections to place nodes.
Any help would be much appreciated.
Program so far:
import java.awt.image.*;
import java.awt.*;
import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
public class PictureReader extends JFrame{
private BufferedImage myImage; //Initial maze you choose
private JButton openFile;
private JFileChooser fc;
private JLabel picture1;
private JPanel picturePanel1;
private Graphics2D g2d;
public PictureReader(String n){
super(n);
Container contents = getContentPane();
contents.setLayout(new FlowLayout());
openFile = new JButton("Choose Maze");
picture1 = new JLabel();
//Panel that holds the maze image
picturePanel1 = new JPanel();
picturePanel1.add(picture1);
fc = new JFileChooser();
ButtonHandler bh = new ButtonHandler();
openFile.addActionListener(bh);
contents.add(openFile);
contents.add(picturePanel1);
setVisible(true);
setSize(500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Image tempImage = new Image();
//myImage = new BufferedImage();
}
//Inner class for events
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
int returnVal = fc.showOpenDialog(PictureReader.this);
//Filechooser gets image selected.
if(e.getSource() == openFile){
File file = fc.getSelectedFile();
try{
//return bufferedImage from ImageIO.read(file) to manipulate later
myImage = ImageIO.read(file);
g2d = myImage.createGraphics();
}
catch (IOException ex) {//Do Nothing
}
}
//Update JLabel of image
picture1.setIcon(new ImageIcon(myImage));
paint(g2d);
}
}//End inner class
//Test paint method
public void paint(Graphics2D g){
int h = myImage.getHeight();
int w = myImage.getWidth();
//Print size of w and h
System.out.println("Height in pixels is: " + h);
System.out.println("Width in pixels is: " + w);
int rgb = myImage.getRGB(0,0);
Color myColor = new Color(rgb);
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
if(myImage.getRGB(i,j) != rgb){
myImage.setRGB(i, j, 14315734);
}
}
}
}
//Test
public static void main(String[] args){
PictureReader myReader = new PictureReader("Mazes");
}
}