Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package falstad;
- import generation.Order;
- import java.awt.event.KeyListener;
- import java.io.File;
- import javax.swing.JFrame;
- /**
- * This class is a wrapper class to startup the Maze game as a Java application
- *
- *
- * This code is refactored code from Maze.java by Paul Falstad, www.falstad.com, Copyright (C) 1998, all rights reserved
- * Paul Falstad granted permission to modify and use code for teaching purposes.
- * Refactored by Peter Kemper
- *
- * TODO: use logger for output instead of Sys.out
- */
- public class MazeApplication extends JFrame {
- // not used, just to make the compiler, static code checker happy
- private static final long serialVersionUID = 1L;
- private KeyListener kl ;
- private MazeController controller ;
- /**
- * Constructor
- */
- public MazeApplication() {
- super() ;
- System.out.println("MazeApplication: maze will be generated with a randomized algorithm.");
- controller = new MazeController() ;
- init() ;
- }
- /**
- * Constructor that loads a maze from a given file or uses a particular method to generate a maze
- */
- public MazeApplication(String parameter) {
- super() ;
- // scan parameters
- // Case 1: Prim
- if ("Prim".equalsIgnoreCase(parameter))
- {
- System.out.println("MazeApplication: generating random maze with Prim's algorithm");
- controller = new MazeController(Order.Builder.Prim) ;
- init() ;
- return ;
- }
- // Case 2: Eller
- if ("Eller".equalsIgnoreCase(parameter))
- {
- System.out.println("MazeApplication: generating random maze with Eller's algorithm");
- controller = new MazeController(Order.Builder.Eller) ;
- init();
- return;
- }
- // Case 3: a file
- File f = new File(parameter) ;
- if (f.exists() && f.canRead())
- {
- System.out.println("MazeApplication: loading maze from file: " + parameter);
- controller = new MazeController(parameter) ;
- init();
- return ;
- }
- // Default case:
- System.out.println("MazeApplication: unknown parameter value: " + parameter + " ignored, operating in default mode.");
- controller = new MazeController() ;
- init() ;
- }
- /**
- * Initializes some internals and puts the game on display.
- */
- private void init() {
- add(controller.getPanel()) ;
- setSize(400, 400);
- setVisible(true);
- // focus should be on the JFrame of the MazeApplication and not on the maze panel
- // such that the SimpleKeyListener kl is used
- setFocusable(true) ;
- controller.init();
- kl = new SimpleKeyListener(this, controller);
- addKeyListener(kl);
- }
- /**
- * Main method to launch Maze as a java application.
- * The application can be operated in two ways. The intended normal operation is to provide no parameters
- * and the maze will be generated by a particular algorithm. If a filename is given, the maze will be loaded
- * from that file. The latter option is useful for development to check particular mazes.
- * @param args is optional, first parameter is a filename with a given maze
- */
- public static void main(String[] args) {
- MazeApplication a ;
- switch (args.length) {
- case 1 : a = new MazeApplication(args[0]);
- break ;
- case 0 :
- default : a = new MazeApplication() ;
- break ;
- }
- a.repaint() ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement