1.1.51. fejezet, Útvonal keresés

Útvonal keresés stack használatával

PathFinderApp.java (main):

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Stack;
import javax.swing.JFrame;
 
class StepStackElement {
    int x;
    int y;
    byte dir;
 
    public StepStackElement(int x, int y, byte dir) {
        this.x = x;
        this.y = y;
        this.dir = dir;
    }
}
 
public class PathFinderApp {
 
    static final int black = new Color(0, 0, 0).getRGB();
    static final int red = new Color(255, 0, 0).getRGB();
    static final int green = new Color(0, 255, 0).getRGB();
    static final int blue = new Color(0, 0, 255).getRGB();
    static final int white = new Color(255, 255, 255).getRGB();
    static final int gray = new Color(155, 155, 155).getRGB();
    static final int purple = new Color(255, 0, 150).getRGB();
    static final int orange = new Color(250, 140, 50).getRGB();
 
    private static int fillColor;
 
    private static BufferedImage image;
    private static MyPanel panel;
 
    public static void applyColor(StepStackElement currentPos) {
        Stack<StepStackElement> stack = new Stack<StepStackElement>();
        stack.push(currentPos);
        while (!stack.isEmpty()) {
            currentPos = stack.peek();
            if (image.getRGB(currentPos.x, currentPos.y) == white) {
                image.setRGB(currentPos.x, currentPos.y, fillColor);
                panel.repaint();
                for (int i = 0; i < 100000; i++)
                    ; // Sleep
            }
            switch (currentPos.dir) {
                // LEFT
                case 0:
                    if ((currentPos.x > 0) && (image.getRGB(currentPos.x - 1, currentPos.y) == white)) {
                        stack.push(new StepStackElement(currentPos.x - 1, currentPos.y, (byte) 0));
                    } else {
                        currentPos.dir++;
                    }
                    break;
                // RIGHT
                case 1:
                    if ((currentPos.x + 1 < image.getWidth())
                            && (image.getRGB(currentPos.x + 1, currentPos.y) == white)) {
                        stack.push(new StepStackElement(currentPos.x + 1, currentPos.y, (byte) 0));
                    } else {
                        currentPos.dir++;
                    }
                    break;
                // UP
                case 2:
                    if ((currentPos.y - 1 > 0) && (image.getRGB(currentPos.x, currentPos.y - 1) == white)) {
                        stack.push(new StepStackElement(currentPos.x, currentPos.y - 1, (byte) 0));
                    } else {
                        currentPos.dir++;
                    }
                    break;
                // DOWN
                case 3:
                    if ((currentPos.y + 1 < image.getHeight())
                            && (image.getRGB(currentPos.x, currentPos.y + 1) == white)) {
                        stack.push(new StepStackElement(currentPos.x, currentPos.y + 1, (byte) 0));
                    } else {
                        currentPos.dir++;
                    }
                    break;
                default:
                    stack.pop();
                    break;
            }
        }
    }
 
    public static void main(String args[]) throws IOException {
        JFrame frame = new MyFrame();
        panel = new MyPanel(640, 480, "test.png");
 
        image = panel.getImage();
 
        frame.add(panel);
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
 
        fillColor = blue;
        applyColor(new StepStackElement(25, 25, (byte) 0));
 
        fillColor = red;
        applyColor(new StepStackElement(345, 275, (byte) 0));
 
        fillColor = green;
        applyColor(new StepStackElement(410, 200, (byte) 0));
 
        fillColor = gray;
        applyColor(new StepStackElement(215, 215, (byte) 0));
 
        fillColor = purple;
        applyColor(new StepStackElement(485, 185, (byte) 0));
 
        fillColor = orange;
        applyColor(new StepStackElement(155, 155, (byte) 0));
    }
}

MyFrame.java:

import java.awt.event.WindowEvent;
 
import javax.swing.JFrame;
 
public class MyFrame extends JFrame {
 
 
    protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            System.exit(0);
        }
    }
}

MyPanel.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
import javax.swing.JPanel;
 
public class MyPanel extends JPanel {
 
    private int width;
    private int height;
    private String fileName;
 
    private BufferedImage image;
 
    public MyPanel(int width, int height, String fileName) {
        this.width = width;
        this.height = height;
        this.fileName = fileName;
        setPreferredSize(new Dimension(this.width, this.height));
        setBackground(Color.PINK);
        try {
            image = ImageIO.read(new File(this.fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }
 
    public BufferedImage getImage() {
        return image;
    }
}