Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.03 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. import javax.swing.*;
  5. import javax.swing.border.*;
  6. import java.io.File;
  7. import java.util.List;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10.  
  11. public class Gui
  12. {
  13. private static String VERSION = "1.1";
  14. // fields:
  15. private JFrame frame;
  16. private JTextField widthField = new JTextField();
  17. private JTextField heightField = new JTextField();
  18. private JButton createButton;
  19. private JButton searchButton;
  20. private JButton quitButton;
  21. private int width = 0, height = 0;
  22. private Canvas canvas;
  23. private BoardDisplay boardDisplay;
  24. private Maze theMaze;
  25.  
  26. /**
  27. * Create an Maze explorer and display its GUI on screen.
  28. */
  29. public Gui()
  30. {
  31. makeFrame();
  32. }
  33.  
  34. /**
  35. * Quit function: quit the application.
  36. */
  37. private void quit()
  38. {
  39. System.exit(0);
  40. }
  41.  
  42. // Some dialogues for demo and testing
  43.  
  44. /**
  45. * 'About' function: show the 'about' box.
  46. */
  47. private void showAbout()
  48. {
  49. JOptionPane.showMessageDialog(frame,
  50. "Maze\n" + VERSION,
  51. "About Maze",
  52. JOptionPane.INFORMATION_MESSAGE);
  53. }
  54.  
  55. private void showMessage(String message)
  56. {
  57. JOptionPane.showMessageDialog(frame,
  58. message,
  59. "GUI-demo",
  60. JOptionPane.INFORMATION_MESSAGE);
  61. }
  62.  
  63. /**
  64. * 'About' function: show the 'about' box.
  65. */
  66. private void showValues( int w, int h )
  67. {
  68. JOptionPane.showMessageDialog(frame,
  69. "Width: " + w + ", height: " + h,
  70. "GUI-demo (REMOVE WHEN FINISHED!)",
  71. JOptionPane.INFORMATION_MESSAGE);
  72. }
  73.  
  74.  
  75. /**
  76. * Create a new maze in the graphics pane.
  77. */
  78. private void createMaze()
  79. {
  80. //showValues(width,height); // Please remove this call when things starts to work correctly (OW we all go crazy!)
  81. // Develop this method!
  82. searchButton.setEnabled(true);
  83. boardDisplay = new BoardDisplay(canvas, width, height);
  84. theMaze = new Maze(width, height);
  85. theMaze.addObserver(boardDisplay);
  86. theMaze.create();
  87. }
  88.  
  89. /**
  90. * Display a graphical search through the maze.
  91. */
  92. private void searchMaze()
  93. {
  94. showMessage("Fake search"); // Please remove!
  95. // Develop this method!
  96. }
  97.  
  98. // ---- Swing stuff to build the frame and all its components and menus ----
  99.  
  100. /**
  101. * Create the Swing frame and its content.
  102. */
  103. private void makeFrame()
  104. {
  105. frame = new JFrame("Maze explorer");
  106. JPanel contentPane = (JPanel)frame.getContentPane();
  107. contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
  108.  
  109. // Specify the layout manager with nice spacing
  110. contentPane.setLayout(new BorderLayout(6, 6));
  111.  
  112. // Create the maze canvas to the right
  113. canvas = new Canvas(frame,Color.lightGray);
  114. contentPane.add(canvas);
  115.  
  116. // Create the toolbar with the buttons
  117. JPanel toolbar = new JPanel();
  118. toolbar.setLayout(new GridLayout(7, 2, 10, 10));
  119.  
  120. widthField.addActionListener(new ActionListener() {
  121. public void actionPerformed(ActionEvent e) {
  122. createButton.setEnabled(false);
  123. searchButton.setEnabled(false);
  124. width = 0;
  125. String text = widthField.getText();
  126. if ( text != null && text.length() > 0 ) {
  127. try {
  128. width = Integer.parseInt(text);
  129. }
  130. catch (NumberFormatException x) {}
  131. if ( width > 0 && height > 0 )
  132. createButton.setEnabled(true);
  133. }
  134. }
  135. });
  136. toolbar.add(new JLabel("width"));
  137. toolbar.add(widthField);
  138.  
  139. heightField.addActionListener(new ActionListener() {
  140. public void actionPerformed(ActionEvent e) {
  141. createButton.setEnabled(false);
  142. searchButton.setEnabled(false);
  143. height = 0;
  144. String text = heightField.getText();
  145. if ( text != null && text.length() > 0 ) {
  146. try {
  147. height = Integer.parseInt(text);
  148. }
  149. catch (NumberFormatException x) {}
  150. if ( width > 0 && height > 0 )
  151. createButton.setEnabled(true);
  152. }
  153. }
  154. });
  155. toolbar.add(new JLabel("height"));
  156. toolbar.add(heightField);
  157.  
  158. createButton = new JButton("Create");
  159. createButton.addActionListener(new ActionListener() {
  160. public void actionPerformed(ActionEvent e) { createMaze(); }
  161. });
  162. createButton.setEnabled(false);
  163. toolbar.add(createButton);
  164.  
  165. searchButton = new JButton("Search");
  166. searchButton.addActionListener(new ActionListener() {
  167. public void actionPerformed(ActionEvent e) { searchMaze(); }
  168. });
  169. searchButton.setEnabled(false);
  170. toolbar.add(searchButton);
  171.  
  172. quitButton = new JButton("Quit");
  173. quitButton.addActionListener(new ActionListener() {
  174. public void actionPerformed(ActionEvent e) { quit(); }
  175. });
  176. quitButton.setEnabled(true);
  177. toolbar.add(quitButton);
  178.  
  179. // Add toolbar into panel with flow layout for spacing
  180. JPanel flow = new JPanel();
  181. flow.add(toolbar);
  182.  
  183. contentPane.add(flow, BorderLayout.WEST);
  184.  
  185. // building is done - arrange the components
  186. frame.pack();
  187.  
  188. // place the frame at the center of the screen and show
  189. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  190. frame.setSize(d.width,d.height - 40);
  191. frame.setVisible(true);
  192. canvas.setVisible(true);
  193. frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
  194. }
  195.  
  196. public static void main(String[] args) {
  197. Gui g = new Gui();
  198.  
  199. }
  200. }
  201. ------------------------
  202.  
  203. Maze
  204.  
  205. import java.util.*;
  206.  
  207. public class Maze extends Board {
  208. private DisjointSets disSets;
  209. private Graph graph;
  210. private int previousPoint, newPoint, row, col;
  211.  
  212. public Maze(int rows, int cols) {
  213. super(rows, cols);
  214. disSets = new DisjointSets(rows * cols);
  215. graph = new Graph();
  216. row = rows;
  217. col = cols;
  218.  
  219. System.out.println("Maze construct");
  220.  
  221. // Implement this!
  222. }
  223.  
  224. private void initializeGrid() {
  225. setChanged();
  226. notifyObservers("0");
  227.  
  228. }
  229.  
  230. private void openEntrance() {
  231. setChanged();
  232. notifyObservers(new Pair(0, Point.Direction.LEFT));
  233. }
  234.  
  235. private void openExit() {
  236. setChanged();
  237. notifyObservers(new Pair(maxCell - 1, Point.Direction.RIGHT));
  238. }
  239.  
  240. /*
  241. private void randomWall(){
  242. Point p= randomPoint();
  243. Point.Direction dir = randomDirection();
  244.  
  245. while(!checkIfValid()){
  246. Point p= randomPoint();
  247. Point.Direction dir = randomDirection();
  248. }
  249.  
  250.  
  251. setChanged();
  252. notifyObservers(new Pair(getCellId(p)), dir);
  253. }
  254. */
  255. public void create() {
  256. //Initalize grid
  257. initializeGrid();
  258.  
  259. openEntrance();
  260. openExit();
  261. List<Integer> listOfNumbers = new ArrayList<>();
  262. int i = 0;
  263. while (i < maxCell) {
  264. listOfNumbers.add(i);
  265. i++;
  266. }
  267. int wallsLeft = row * col;
  268. Collections.shuffle(listOfNumbers);
  269. for (Integer number : listOfNumbers) {
  270. Point point = new Point(getRow(number), getCol(number));
  271. boolean numberChanged = false;
  272. Point.Direction dir;
  273. while (!numberChanged) {
  274. int tempInt = disSets.find(getCellId(point));
  275. dir = randomDirection();
  276. if (isValid(toPoint(number))) {
  277. switch (dir) {
  278. case UP:
  279. case DOWN:
  280. case LEFT:
  281. case RIGHT:
  282. Point tempPoint = new Point(getRow(number), getCol(number));
  283. tempPoint.move(dir);
  284. if (isValid(tempPoint)) {
  285. int tempInt2 = disSets.find(getCellId(tempPoint));
  286. if (tempInt != tempInt2) {
  287. System.out.println(getCellId(tempPoint));
  288. System.out.println(getCellId(point));
  289. disSets.union(tempInt, tempInt2);
  290. numberChanged = true;
  291. setChanged();
  292. notifyObservers(new Pair(number, dir));
  293. }
  294. }
  295. break;
  296. }
  297. }
  298. //setChanged();
  299. //notifyObserver(i);
  300. }
  301.  
  302. }
  303. }
  304.  
  305. private Point toPoint(int cellID) {
  306. return new Point(getRow(cellID), getCol(cellID));
  307. }
  308. // Implement this method!
  309. // ArrayList<Pair<Integer, Point.Direction>> maze = new ArrayList<>();
  310. /*
  311. int cell = 0;
  312. Point currentPoint;
  313. int numberOfWalls = row * col;
  314.  
  315. while (numberOfWalls != maxCell) {
  316. previousPoint = cell;
  317. row = getRow(cell);
  318. col = getCol(cell);
  319. currentPoint = new Point(row, col);
  320. Point.Direction dir = randomDirection();
  321. newPoint = getCellId(currentPoint);
  322. if (disSets.find(newPoint) != disSets.find(previousPoint)) {
  323. Pair<Integer, Point.Direction> pair = new Pair<>(getCellId(currentPoint), dir);
  324. maze.add(pair);
  325. setChanged();
  326. notifyObservers(pair);
  327. disSets.union(disSets.find(previousPoint), disSets.find(newPoint));
  328. numberOfWalls--;
  329. cell++;
  330. }
  331. }
  332.  
  333. while(!mazeDone()){
  334. randomWall();
  335. }
  336.  
  337. }
  338. */
  339.  
  340.  
  341. private Point.Direction randomDirection() {
  342. Random randomNumber = new Random();
  343. switch (randomNumber.nextInt(4)) {
  344. case 0:
  345. return Point.Direction.UP;
  346. case 1:
  347. return Point.Direction.RIGHT;
  348. case 2:
  349. return Point.Direction.DOWN;
  350. case 3:
  351. return Point.Direction.LEFT;
  352. default:
  353. return null;
  354. }
  355. }
  356.  
  357. public void search() {
  358. // Implement this method!
  359. }
  360.  
  361. // ...
  362. }
  363. ---------------------------------------
  364. BoardDisplay
  365.  
  366. import java.awt.Color;
  367. import java.awt.Dimension;
  368. import java.awt.geom.*;
  369. import java.util.*;
  370.  
  371. public class BoardDisplay extends Board implements Observer {
  372.  
  373. private int gridSize, rowOffset, colOffset;
  374. private Color bkColor, lineColor, pathColor;
  375. private Canvas canvas;
  376.  
  377. public BoardDisplay(Canvas canvas, int maxRow, int maxCol) {
  378. super(maxRow, maxCol);
  379. this.canvas = canvas;
  380. bkColor = Color.lightGray;
  381. lineColor = Color.blue;
  382. pathColor = Color.red;
  383. Dimension d = canvas.getSize();
  384. int width = d.width - 4;
  385. int height = d.height - 5;
  386. int gdRow = height/maxRow, gdCol = width/maxCol;
  387. gridSize = ( gdRow < gdCol ? gdRow : gdCol );
  388. // compute offset to center it
  389. rowOffset = (height-maxRow*gridSize)/2 + 2;
  390. colOffset = (width-maxCol*gridSize)/2 + 2;
  391. }
  392.  
  393. private void drawGrid() {
  394. for ( int row = 0; row <= maxRow; row++ )
  395. myLine( 0, row*gridSize, maxCol*gridSize, row*gridSize, lineColor );
  396. for ( int col = 0; col <= maxCol; col++ )
  397. myLine( col*gridSize, 0, col*gridSize, maxRow*gridSize, lineColor );
  398.  
  399. }
  400.  
  401. private void fillCell( int cellId ) {
  402. int row = getRow(cellId), col = getCol(cellId);
  403. canvas.setForegroundColour(pathColor);
  404. if ( gridSize > 4 )
  405. canvas.fill( new Ellipse2D.Double( colOffset + col*gridSize + 1, rowOffset + row*gridSize + 1,
  406. gridSize-1, gridSize-1 ) );
  407. else
  408. canvas.fill( new Rectangle2D.Double( colOffset + col*gridSize + 1, rowOffset + row*gridSize + 1,
  409. gridSize-1, gridSize-1 ) );
  410. }
  411.  
  412. // Knock down the wall of cell cellId in direction dir
  413. private void knockDownWall( int cellId, Point.Direction dir ) {
  414. knockDownWall( getRow( cellId ), getCol( cellId ), dir );
  415. }
  416. private void knockDownWalls(){
  417. Random number = new Random();
  418. for(int i = 0; i < 100; i+=10) {
  419. knockDownWall(number.nextInt(40), Point.Direction.DOWN);
  420. fillCell(i);
  421. i++;
  422. }
  423. }
  424. private void knockDownWall( int row, int col, Point.Direction dir ) {
  425. // Compute coordinates of line segment to remove
  426. int c0 = 0, r0 = 0, c1 = 0, r1 = 0;
  427. if ( dir == Point.Direction.UP ) {
  428. c0 = col*gridSize + 1;
  429. c1 = (col + 1)*gridSize - 1;
  430. r0 = r1 = row*gridSize;
  431. } else if ( dir == Point.Direction.RIGHT ) {
  432. r0 = row*gridSize + 1;
  433. r1 = (row + 1)*gridSize - 1;
  434. c0 = c1 = (col + 1)*gridSize;
  435. } else if ( dir == Point.Direction.DOWN ) {
  436. c0 = col*gridSize + 1;
  437. c1 = (col + 1)*gridSize - 1;
  438. r0 = r1 = (row + 1)*gridSize;
  439. } else if ( dir == Point.Direction.LEFT ) {
  440. r0 = row*gridSize + 1;
  441. r1 = (row + 1)*gridSize - 1;
  442. c0 = c1 = col*gridSize;
  443. }
  444. // Erase the line
  445. myLine( c0, r0, c1, r1, bkColor );
  446. }
  447.  
  448. private void myLine( int c1, int r1, int c2, int r2, Color c ) {
  449. canvas.setForegroundColour(c);
  450. canvas.drawLine( colOffset + c1, rowOffset + r1, colOffset + c2, rowOffset + r2 );
  451. }
  452.  
  453. public void update(Observable o, Object arg) {
  454. // Develop this method!
  455. if(arg.equals("0")){
  456. drawGrid();
  457. }
  458. if(arg instanceof Pair<?,?>){
  459.  
  460. knockDownWall((int) ((Pair) arg).first, (Point.Direction) ((Pair) arg).second);
  461. }
  462. }
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement