Pessoal, segue aplicativo que eu desenvolvi para brincar um pouquinho.
Modo de uso:
O aplicativo funciona da seguinte maneira, você coloca o caminho da imagem onde esta o labirinto e clica em OK, o programa vai mostra a sua imagem carregada dentro dele.
Basta dar um clique no chão branco para colocar o start point e um click novamente no chao branco para indicar o end point.
Restrições:
1 - O chão deve ser branco.
2 - As paredes devem ser pretas.
3 - A imagem não pode estar danificada (desfocada).
Só funciona perfeitamente se obedecer as condições a cima, esse labirinto que coloquei para download é perfeito, mas o aplicativo funciona tambem em outros labirintos.
Download labirinto: [url]https://docs.google.com/open?id=0B0tcetJ0NHPORnd6blFYbC1sdEE[/url]
Download aplicativo: [url]https://docs.google.com/open?id=0B0tcetJ0NHPOTmVoSU5uVWVvWTQ[/url]
Creditos:
Vinicius Zibetti Resko e 'aajjbb'
Codigo Fonte:
Classe Imagem:import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Imagem extends JPanel implements Runnable {
private static final int cor = Color.BLUE.getRGB(), way = Color.WHITE.getRGB();
private static final long serialVersionUID = 1L;
private static final int[] dx = { 0, -1, 0, 1, -1, -1, 1, 1 };
private static final int[] dy = { -1, 0, 1, 0, -1, 1, 1, -1 };
private BufferedImage bf;
ImageTreatment treat;
int w, h, sx, sy, ex, ey;
int clicks = 0;
public Imagem(String path) {
loadImage(path);
setBounds(0, 0, w, h);
setVisible(true);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (clicks == 0 && bf.getRGB(e.getX(), e.getY()) == way) {
sx = e.getX();
sy = e.getY();
clicks++;
bf.setRGB(sx, sy, cor);
} else if (clicks == 1 && bf.getRGB(e.getX(), e.getY()) == way) {
ex = e.getX();
ey = e.getY();
clicks++;
bf.setRGB(ex, ey, cor);
}
repaint();
}
});
}
private void loadImage(String path) {
try {
bf = (BufferedImage) ImageIO.read(new File(path));
w = bf.getWidth();
h = bf.getHeight();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bf, 0, 0, w, h, null);
}
private List<Point> getShortestPath() {
bf.setRGB(sx, sy, Color.WHITE.getRGB());
bf.setRGB(ex, ey, Color.WHITE.getRGB());
List<Point> ret = new LinkedList<Point>();
Point[][] aux = new Point[w + 1][h + 1];
Queue<Point> q = new LinkedList<Point>();
boolean[][] flags = new boolean[w + 1][h + 1];
int[][] dist = new int[w + 1][h + 1];
q.offer(new Point(sx, sy));
flags[sx][sy] = true;
while (!q.isEmpty()) {
Point tmp = q.poll();
int x = tmp.x;
int y = tmp.y;
for (int i = 0; i < 8; i++) {
int dxa = x + dx[i];
int dya = y + dy[i];
if (dxa >= 0 && dya >= 0 && dxa < w && dya < h && !flags[dxa][dya] && bf.getRGB(dxa, dya) == way) {
flags[dxa][dya] = true;
dist[dxa][dya] = dist[x][y] + 1;
q.offer(new Point(dxa, dya));
aux[dxa][dya] = new Point(x, y);
}
}
}
int tmp_x = ex;
int tmp_y = ey;
if (dist[tmp_x][tmp_y] == 0)
JOptionPane.showMessageDialog(null, "Não foi poossivel calcular a rota.");
while (aux[tmp_x][tmp_y] != null) {
ret.add(new Point(tmp_x, tmp_y));
int tmpX = tmp_x;
tmp_x = aux[tmp_x][tmp_y].x;
tmp_y = aux[tmpX][tmp_y].y;
}
return ret;
}
@Override
public void run() {
try {
List<Point> points = getShortestPath();
while (!points.isEmpty()) {
Point tmp = points.get(points.size() - 1);
int x = tmp.x;
int y = tmp.y;
points.remove(points.size() - 1);
bf.setRGB(x, y, cor);
Thread.sleep(3);
repaint();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.awt.Color;
import java.awt.image.BufferedImage;
public class ImageTreatment {
private Color cinza[] = new Color[16];
private int black = Color.BLACK.getRGB(), white = Color.WHITE.getRGB();
public ImageTreatment() {
fillCinza();
}
private void fillCinza() {
int count = 0;
for (int i = 0; i < 16; i++) {
cinza[i] = new Color(count, count, count);
count += 16;
}
}
public void treat(BufferedImage bf) {
for (int i = 0; i < bf.getWidth(); i++) {
for (int j = 0; j < bf.getHeight(); j++) {
int tmp = bf.getRGB(i, j);
boolean found = false;
for (int k = 0; k < 16; k++) {
if (tmp == cinza[k].getRGB()) {
bf.setRGB(i, j, black);
found = true;
break;
}
}
if (!found)
bf.setRGB(i, j, white);
}
}
}
public static void main(String[] args) {
new ImageTreatment().fillCinza();
}
}
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Lab {
private JFrame frame;
private Imagem lab;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lab window = new Lab();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Lab() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String path = JOptionPane.showInputDialog(null, "Path do labirinto:\n");
lab = new Imagem(path);
frame.setBounds(100, 100, 541, 500);
frame.getContentPane().add(lab);
lab.setLayout(null);
JButton Start = new JButton("Percorrer");
Start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (lab.clicks == 2) {
Thread t = new Thread(lab);
t.start();
lab.clicks = 0;
}
}
});
Start.setBounds(10, 428, 100, 23);
lab.add(Start);
}
}