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");
}   

}

determine whether or not a path turns or ends up in a dead-end,

By checking the adjacent pixels to see if the clear path continues or if it ends.

The paths would have to be the same width though, right? Since the paths aren't 1 pixel wide, then I guess I'd just have to scale up to something like 10 or 15 pixels wide and check adjacent pixels around this 'rectangle' of points.

Would this work?

Would the paths go in any direction or just parallel to x and y axis?

Yes, the paths would only be horizontal and verticle in relation to the X and Y axis.

How are you saving the paths you find? Two pairs of x,y values, one for each end.

Okay that would make sense.

What I think I'll do is I'll place a start marker and finish marker and make the paths a constant width in white pixels with the two markers on each end.

I'm not sure how I'll save the paths as of now. I plan to place a node at the start marker and draw a line until I find the next intersection, dead-end, or turn, connect them and continue. After I draw the nodes, I'll keep that nodes' value in a pair of x,y coordinates and length of its edges for the wieghts.

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.