/** * Sets a new grid for this world. Occupants are transferred from the old world to the new. * * @param newGrid the new grid */ public void setGrid(Grid<T> newGrid) { Grid<T> oldGrid = world.getGrid(); Map<Location, T> occupants = new HashMap<Location, T>(); for (Location loc : oldGrid.getOccupiedLocations()) occupants.put(loc, world.remove(loc)); world.setGrid(newGrid); for (Location loc : occupants.keySet()) { if (newGrid.isValid(loc)) world.add(loc, occupants.get(loc)); } display.setGrid(newGrid); repaint(); }
public void show() { if (getMessage() == null) { String defaultMessage = "Click on a grid location to construct or manipulate an actor."; setMessage(defaultMessage); } super.show(); }
/** * Overrides World<T> newGame(char). Shoots a MoveInfo with null piece and null location to the * CheckerGame if the game is still running. If the game has ended, directly changes the * CheckerGame's game status for CheckerRunner to detect. * * @param a the new game status */ public void newGame(char a) { newGame = a; playerPiece = null; setPlayerLocation(null); if (!lock.hasQueuedThreads()) // if the game has ended { game.newGame(a); } super.newGame(); // World's newGame() disposes the frame }
/** * Sets the message in the GridWorld GUI.<br> * * @param msg the message text */ @Override public void setMessage(String msg) { super.setMessage(msg); show(); // refreshes message box. w/o show, previous message would sometimes remain }
/** * Constructs a WorldFrame that displays the occupants of a world * * @param world the world to display */ public WorldFrame(World<T> world) { this.world = world; count++; resources = ResourceBundle.getBundle(getClass().getName() + "Resources"); try { System.setProperty("sun.awt.exception.handler", GUIExceptionHandler.class.getName()); } catch (SecurityException ex) { // will fail in an applet } addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent event) { count--; if (count == 0) System.exit(0); } }); displayMap = new DisplayMap(); String title = System.getProperty("info.gridworld.gui.frametitle"); if (title == null) title = resources.getString("frame.title"); setTitle(title); setLocation(25, 15); URL appIconUrl = getClass().getResource("GridWorld.gif"); ImageIcon appIcon = new ImageIcon(appIconUrl); setIconImage(appIcon.getImage()); makeMenus(); JPanel content = new JPanel(); content.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15)); content.setLayout(new BorderLayout()); setContentPane(content); display = new GridPanel(displayMap, resources); KeyboardFocusManager.getCurrentKeyboardFocusManager() .addKeyEventDispatcher( new KeyEventDispatcher() { public boolean dispatchKeyEvent(KeyEvent event) { if (getFocusOwner() == null) return false; String text = KeyStroke.getKeyStrokeForEvent(event).toString(); final String PRESSED = "pressed "; int n = text.indexOf(PRESSED); if (n < 0) return false; // filter out modifier keys; they are neither characters or actions if (event.getKeyChar() == KeyEvent.CHAR_UNDEFINED && !event.isActionKey()) return false; text = text.substring(0, n) + text.substring(n + PRESSED.length()); boolean consumed = getWorld().keyPressed(text, display.getCurrentLocation()); if (consumed) repaint(); return consumed; } }); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewport(new PseudoInfiniteViewport(scrollPane)); scrollPane.setViewportView(display); content.add(scrollPane, BorderLayout.CENTER); gridClasses = new TreeSet<Class>( new Comparator<Class>() { public int compare(Class a, Class b) { return a.getName().compareTo(b.getName()); } }); for (String name : world.getGridClasses()) try { gridClasses.add(Class.forName(name)); } catch (Exception ex) { ex.printStackTrace(); } Grid<T> gr = world.getGrid(); gridClasses.add(gr.getClass()); makeNewGridMenu(); control = new GUIController<T>(this, display, displayMap, resources); content.add(control.controlPanel(), BorderLayout.SOUTH); messageArea = new JTextArea(2, 35); messageArea.setEditable(false); messageArea.setFocusable(false); messageArea.setBackground(new Color(0xFAFAD2)); content.add(new JScrollPane(messageArea), BorderLayout.NORTH); pack(); repaint(); // to show message display.setGrid(gr); }