Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.*;
- import javax.swing.*;
- import javax.swing.border.*;
- import java.io.File;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class Gui
- {
- private static String VERSION = "1.1";
- // fields:
- private JFrame frame;
- private JTextField widthField = new JTextField();
- private JTextField heightField = new JTextField();
- private JButton createButton;
- private JButton searchButton;
- private JButton quitButton;
- private int width = 0, height = 0;
- private Canvas canvas;
- private BoardDisplay boardDisplay;
- private Maze theMaze;
- /**
- * Create an Maze explorer and display its GUI on screen.
- */
- public Gui()
- {
- makeFrame();
- }
- /**
- * Quit function: quit the application.
- */
- private void quit()
- {
- System.exit(0);
- }
- // Some dialogues for demo and testing
- /**
- * 'About' function: show the 'about' box.
- */
- private void showAbout()
- {
- JOptionPane.showMessageDialog(frame,
- "Maze\n" + VERSION,
- "About Maze",
- JOptionPane.INFORMATION_MESSAGE);
- }
- private void showMessage(String message)
- {
- JOptionPane.showMessageDialog(frame,
- message,
- "GUI-demo",
- JOptionPane.INFORMATION_MESSAGE);
- }
- /**
- * 'About' function: show the 'about' box.
- */
- private void showValues( int w, int h )
- {
- JOptionPane.showMessageDialog(frame,
- "Width: " + w + ", height: " + h,
- "GUI-demo (REMOVE WHEN FINISHED!)",
- JOptionPane.INFORMATION_MESSAGE);
- }
- /**
- * Create a new maze in the graphics pane.
- */
- private void createMaze()
- {
- //showValues(width,height); // Please remove this call when things starts to work correctly (OW we all go crazy!)
- // Develop this method!
- searchButton.setEnabled(true);
- boardDisplay = new BoardDisplay(canvas, width, height);
- theMaze = new Maze(width, height);
- theMaze.addObserver(boardDisplay);
- theMaze.create();
- }
- /**
- * Display a graphical search through the maze.
- */
- private void searchMaze()
- {
- showMessage("Fake search"); // Please remove!
- // Develop this method!
- }
- // ---- Swing stuff to build the frame and all its components and menus ----
- /**
- * Create the Swing frame and its content.
- */
- private void makeFrame()
- {
- frame = new JFrame("Maze explorer");
- JPanel contentPane = (JPanel)frame.getContentPane();
- contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
- // Specify the layout manager with nice spacing
- contentPane.setLayout(new BorderLayout(6, 6));
- // Create the maze canvas to the right
- canvas = new Canvas(frame,Color.lightGray);
- contentPane.add(canvas);
- // Create the toolbar with the buttons
- JPanel toolbar = new JPanel();
- toolbar.setLayout(new GridLayout(7, 2, 10, 10));
- widthField.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- createButton.setEnabled(false);
- searchButton.setEnabled(false);
- width = 0;
- String text = widthField.getText();
- if ( text != null && text.length() > 0 ) {
- try {
- width = Integer.parseInt(text);
- }
- catch (NumberFormatException x) {}
- if ( width > 0 && height > 0 )
- createButton.setEnabled(true);
- }
- }
- });
- toolbar.add(new JLabel("width"));
- toolbar.add(widthField);
- heightField.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- createButton.setEnabled(false);
- searchButton.setEnabled(false);
- height = 0;
- String text = heightField.getText();
- if ( text != null && text.length() > 0 ) {
- try {
- height = Integer.parseInt(text);
- }
- catch (NumberFormatException x) {}
- if ( width > 0 && height > 0 )
- createButton.setEnabled(true);
- }
- }
- });
- toolbar.add(new JLabel("height"));
- toolbar.add(heightField);
- createButton = new JButton("Create");
- createButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) { createMaze(); }
- });
- createButton.setEnabled(false);
- toolbar.add(createButton);
- searchButton = new JButton("Search");
- searchButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) { searchMaze(); }
- });
- searchButton.setEnabled(false);
- toolbar.add(searchButton);
- quitButton = new JButton("Quit");
- quitButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) { quit(); }
- });
- quitButton.setEnabled(true);
- toolbar.add(quitButton);
- // Add toolbar into panel with flow layout for spacing
- JPanel flow = new JPanel();
- flow.add(toolbar);
- contentPane.add(flow, BorderLayout.WEST);
- // building is done - arrange the components
- frame.pack();
- // place the frame at the center of the screen and show
- Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
- frame.setSize(d.width,d.height - 40);
- frame.setVisible(true);
- canvas.setVisible(true);
- frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
- }
- public static void main(String[] args) {
- Gui g = new Gui();
- }
- }
- ------------------------
- Maze
- import java.util.*;
- public class Maze extends Board {
- private DisjointSets disSets;
- private Graph graph;
- private int previousPoint, newPoint, row, col;
- public Maze(int rows, int cols) {
- super(rows, cols);
- disSets = new DisjointSets(rows * cols);
- graph = new Graph();
- row = rows;
- col = cols;
- System.out.println("Maze construct");
- // Implement this!
- }
- private void initializeGrid() {
- setChanged();
- notifyObservers("0");
- }
- private void openEntrance() {
- setChanged();
- notifyObservers(new Pair(0, Point.Direction.LEFT));
- }
- private void openExit() {
- setChanged();
- notifyObservers(new Pair(maxCell - 1, Point.Direction.RIGHT));
- }
- /*
- private void randomWall(){
- Point p= randomPoint();
- Point.Direction dir = randomDirection();
- while(!checkIfValid()){
- Point p= randomPoint();
- Point.Direction dir = randomDirection();
- }
- setChanged();
- notifyObservers(new Pair(getCellId(p)), dir);
- }
- */
- public void create() {
- //Initalize grid
- initializeGrid();
- openEntrance();
- openExit();
- List<Integer> listOfNumbers = new ArrayList<>();
- int i = 0;
- while (i < maxCell) {
- listOfNumbers.add(i);
- i++;
- }
- int wallsLeft = row * col;
- Collections.shuffle(listOfNumbers);
- for (Integer number : listOfNumbers) {
- Point point = new Point(getRow(number), getCol(number));
- boolean numberChanged = false;
- Point.Direction dir;
- while (!numberChanged) {
- int tempInt = disSets.find(getCellId(point));
- dir = randomDirection();
- if (isValid(toPoint(number))) {
- switch (dir) {
- case UP:
- case DOWN:
- case LEFT:
- case RIGHT:
- Point tempPoint = new Point(getRow(number), getCol(number));
- tempPoint.move(dir);
- if (isValid(tempPoint)) {
- int tempInt2 = disSets.find(getCellId(tempPoint));
- if (tempInt != tempInt2) {
- System.out.println(getCellId(tempPoint));
- System.out.println(getCellId(point));
- disSets.union(tempInt, tempInt2);
- numberChanged = true;
- setChanged();
- notifyObservers(new Pair(number, dir));
- }
- }
- break;
- }
- }
- //setChanged();
- //notifyObserver(i);
- }
- }
- }
- private Point toPoint(int cellID) {
- return new Point(getRow(cellID), getCol(cellID));
- }
- // Implement this method!
- // ArrayList<Pair<Integer, Point.Direction>> maze = new ArrayList<>();
- /*
- int cell = 0;
- Point currentPoint;
- int numberOfWalls = row * col;
- while (numberOfWalls != maxCell) {
- previousPoint = cell;
- row = getRow(cell);
- col = getCol(cell);
- currentPoint = new Point(row, col);
- Point.Direction dir = randomDirection();
- newPoint = getCellId(currentPoint);
- if (disSets.find(newPoint) != disSets.find(previousPoint)) {
- Pair<Integer, Point.Direction> pair = new Pair<>(getCellId(currentPoint), dir);
- maze.add(pair);
- setChanged();
- notifyObservers(pair);
- disSets.union(disSets.find(previousPoint), disSets.find(newPoint));
- numberOfWalls--;
- cell++;
- }
- }
- while(!mazeDone()){
- randomWall();
- }
- }
- */
- private Point.Direction randomDirection() {
- Random randomNumber = new Random();
- switch (randomNumber.nextInt(4)) {
- case 0:
- return Point.Direction.UP;
- case 1:
- return Point.Direction.RIGHT;
- case 2:
- return Point.Direction.DOWN;
- case 3:
- return Point.Direction.LEFT;
- default:
- return null;
- }
- }
- public void search() {
- // Implement this method!
- }
- // ...
- }
- ---------------------------------------
- BoardDisplay
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.geom.*;
- import java.util.*;
- public class BoardDisplay extends Board implements Observer {
- private int gridSize, rowOffset, colOffset;
- private Color bkColor, lineColor, pathColor;
- private Canvas canvas;
- public BoardDisplay(Canvas canvas, int maxRow, int maxCol) {
- super(maxRow, maxCol);
- this.canvas = canvas;
- bkColor = Color.lightGray;
- lineColor = Color.blue;
- pathColor = Color.red;
- Dimension d = canvas.getSize();
- int width = d.width - 4;
- int height = d.height - 5;
- int gdRow = height/maxRow, gdCol = width/maxCol;
- gridSize = ( gdRow < gdCol ? gdRow : gdCol );
- // compute offset to center it
- rowOffset = (height-maxRow*gridSize)/2 + 2;
- colOffset = (width-maxCol*gridSize)/2 + 2;
- }
- private void drawGrid() {
- for ( int row = 0; row <= maxRow; row++ )
- myLine( 0, row*gridSize, maxCol*gridSize, row*gridSize, lineColor );
- for ( int col = 0; col <= maxCol; col++ )
- myLine( col*gridSize, 0, col*gridSize, maxRow*gridSize, lineColor );
- }
- private void fillCell( int cellId ) {
- int row = getRow(cellId), col = getCol(cellId);
- canvas.setForegroundColour(pathColor);
- if ( gridSize > 4 )
- canvas.fill( new Ellipse2D.Double( colOffset + col*gridSize + 1, rowOffset + row*gridSize + 1,
- gridSize-1, gridSize-1 ) );
- else
- canvas.fill( new Rectangle2D.Double( colOffset + col*gridSize + 1, rowOffset + row*gridSize + 1,
- gridSize-1, gridSize-1 ) );
- }
- // Knock down the wall of cell cellId in direction dir
- private void knockDownWall( int cellId, Point.Direction dir ) {
- knockDownWall( getRow( cellId ), getCol( cellId ), dir );
- }
- private void knockDownWalls(){
- Random number = new Random();
- for(int i = 0; i < 100; i+=10) {
- knockDownWall(number.nextInt(40), Point.Direction.DOWN);
- fillCell(i);
- i++;
- }
- }
- private void knockDownWall( int row, int col, Point.Direction dir ) {
- // Compute coordinates of line segment to remove
- int c0 = 0, r0 = 0, c1 = 0, r1 = 0;
- if ( dir == Point.Direction.UP ) {
- c0 = col*gridSize + 1;
- c1 = (col + 1)*gridSize - 1;
- r0 = r1 = row*gridSize;
- } else if ( dir == Point.Direction.RIGHT ) {
- r0 = row*gridSize + 1;
- r1 = (row + 1)*gridSize - 1;
- c0 = c1 = (col + 1)*gridSize;
- } else if ( dir == Point.Direction.DOWN ) {
- c0 = col*gridSize + 1;
- c1 = (col + 1)*gridSize - 1;
- r0 = r1 = (row + 1)*gridSize;
- } else if ( dir == Point.Direction.LEFT ) {
- r0 = row*gridSize + 1;
- r1 = (row + 1)*gridSize - 1;
- c0 = c1 = col*gridSize;
- }
- // Erase the line
- myLine( c0, r0, c1, r1, bkColor );
- }
- private void myLine( int c1, int r1, int c2, int r2, Color c ) {
- canvas.setForegroundColour(c);
- canvas.drawLine( colOffset + c1, rowOffset + r1, colOffset + c2, rowOffset + r2 );
- }
- public void update(Observable o, Object arg) {
- // Develop this method!
- if(arg.equals("0")){
- drawGrid();
- }
- if(arg instanceof Pair<?,?>){
- knockDownWall((int) ((Pair) arg).first, (Point.Direction) ((Pair) arg).second);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement