Advertisement
wtmhahagd

MazeApplication

Dec 3rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package falstad;
  5.  
  6. import generation.Order;
  7.  
  8. import java.awt.event.KeyListener;
  9. import java.io.File;
  10.  
  11. import javax.swing.JFrame;
  12.  
  13.  
  14. /**
  15. * This class is a wrapper class to startup the Maze game as a Java application
  16. *
  17. *
  18. * This code is refactored code from Maze.java by Paul Falstad, www.falstad.com, Copyright (C) 1998, all rights reserved
  19. * Paul Falstad granted permission to modify and use code for teaching purposes.
  20. * Refactored by Peter Kemper
  21. *
  22. * TODO: use logger for output instead of Sys.out
  23. */
  24. public class MazeApplication extends JFrame {
  25.  
  26. // not used, just to make the compiler, static code checker happy
  27. private static final long serialVersionUID = 1L;
  28.  
  29. private KeyListener kl ;
  30.  
  31. private MazeController controller ;
  32. /**
  33. * Constructor
  34. */
  35. public MazeApplication() {
  36. super() ;
  37. System.out.println("MazeApplication: maze will be generated with a randomized algorithm.");
  38. controller = new MazeController() ;
  39. init() ;
  40. }
  41.  
  42. /**
  43. * Constructor that loads a maze from a given file or uses a particular method to generate a maze
  44. */
  45. public MazeApplication(String parameter) {
  46. super() ;
  47. // scan parameters
  48. // Case 1: Prim
  49. if ("Prim".equalsIgnoreCase(parameter))
  50. {
  51. System.out.println("MazeApplication: generating random maze with Prim's algorithm");
  52. controller = new MazeController(Order.Builder.Prim) ;
  53. init() ;
  54. return ;
  55. }
  56. // Case 2: Eller
  57. if ("Eller".equalsIgnoreCase(parameter))
  58. {
  59. System.out.println("MazeApplication: generating random maze with Eller's algorithm");
  60. controller = new MazeController(Order.Builder.Eller) ;
  61. init();
  62. return;
  63. }
  64. // Case 3: a file
  65. File f = new File(parameter) ;
  66. if (f.exists() && f.canRead())
  67. {
  68. System.out.println("MazeApplication: loading maze from file: " + parameter);
  69. controller = new MazeController(parameter) ;
  70. init();
  71. return ;
  72. }
  73. // Default case:
  74. System.out.println("MazeApplication: unknown parameter value: " + parameter + " ignored, operating in default mode.");
  75. controller = new MazeController() ;
  76. init() ;
  77. }
  78.  
  79. /**
  80. * Initializes some internals and puts the game on display.
  81. */
  82. private void init() {
  83. add(controller.getPanel()) ;
  84.  
  85. setSize(400, 400);
  86. setVisible(true);
  87.  
  88. // focus should be on the JFrame of the MazeApplication and not on the maze panel
  89. // such that the SimpleKeyListener kl is used
  90. setFocusable(true) ;
  91.  
  92. controller.init();
  93.  
  94. kl = new SimpleKeyListener(this, controller);
  95. addKeyListener(kl);
  96. }
  97.  
  98. /**
  99. * Main method to launch Maze as a java application.
  100. * The application can be operated in two ways. The intended normal operation is to provide no parameters
  101. * and the maze will be generated by a particular algorithm. If a filename is given, the maze will be loaded
  102. * from that file. The latter option is useful for development to check particular mazes.
  103. * @param args is optional, first parameter is a filename with a given maze
  104. */
  105. public static void main(String[] args) {
  106. MazeApplication a ;
  107. switch (args.length) {
  108. case 1 : a = new MazeApplication(args[0]);
  109. break ;
  110. case 0 :
  111. default : a = new MazeApplication() ;
  112. break ;
  113. }
  114. a.repaint() ;
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement