/** Set the cavern to be all light (light = true) or all dark. */ public void setLighting(boolean light) { for (int i = 0; i < cavern.getRowCount(); i++) { for (int j = 0; j < cavern.getColumnCount(); j++) { visited[i][j] = light; } } repaint(); }
/** The screen size has changed. Adjust the maze panel to (width, height). */ void updateScreenSize(int width, int height) { TILE_WIDTH = (int) (width * 1.0 / cavern.getColumnCount()); TILE_HEIGHT = (int) (height * 0.95 / cavern.getRowCount()); // Force tiles to be square TILE_WIDTH = Math.min(TILE_WIDTH, TILE_HEIGHT); TILE_HEIGHT = Math.min(TILE_WIDTH, TILE_HEIGHT); repaint(); }
/** * Create a new MazePanel of a given size * * @param cav The Cavern to display * @param screenWidth The width of the panel, in pixels * @param screenHeight The height of the panel, in pixels * @param gui The GUI that owns this MazePanel */ public MazePanel(Cavern cav, int screenWidth, int screenHeight, GUI gui) { cavern = cav; visited = new boolean[cavern.getRowCount()][cavern.getColumnCount()]; // Compute the dimensions of an individual tile TILE_WIDTH = (int) (screenWidth * 1.0 / cavern.getColumnCount()); TILE_HEIGHT = (int) (screenHeight * 0.95 / cavern.getRowCount()); // Force tiles to be square TILE_WIDTH = Math.min(TILE_WIDTH, TILE_HEIGHT); TILE_HEIGHT = Math.min(TILE_WIDTH, TILE_HEIGHT); // Load content try { path = ImageIO.read(new File(PATH_PATH)); wall = ImageIO.read(new File(WALL_PATH)); orb = ImageIO.read(new File(ORB_PATH)); coinSheet = new Sprite(COIN_PATH, 32, 32, 1); entrance = ImageIO.read(new File(ENTRANCE_PATH)); tasty = ImageIO.read(new File(TASTY_PATH)); background = ImageIO.read(new File(BACKGROUND_PATH)); } catch (IOException e) { throw new IllegalArgumentException("Can't find input file : " + e.toString()); } // Create the dark path darkness = new Color(0, 0, 0, (int) (256 - 256 * DARK_FACTOR)); // Add listener for clicking tiles addMouseListener( new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { int row = e.getY() / TILE_HEIGHT; int col = e.getX() / TILE_WIDTH; if (row < cavern.getRowCount() && col < cavern.getColumnCount()) { gui.selectNode(cavern.getNodeAt(row, col)); } } }); }
/** Draw the maze on the screen. */ @Override public void paintComponent(Graphics page) { super.paintComponent(page); for (int i = 0; i < getWidth(); i += 100) { page.drawImage(background, i, 0, 100, getHeight(), null); } page.setColor(darkness); // Draw the maze tiles for (int row = 0; row < cavern.getRowCount(); row++) { for (int col = 0; col < cavern.getColumnCount(); col++) { // This is a walkable tile if (cavern.getTileAt(row, col).getType() != Tile.Type.WALL) { // Draw the path image to the background page.drawImage(path, TILE_WIDTH * col, TILE_HEIGHT * row, TILE_WIDTH, TILE_HEIGHT, null); // Darken this tile if we haven't been there yet if (!visited[row][col]) { page.fillRect(TILE_WIDTH * col, TILE_HEIGHT * row, TILE_WIDTH, TILE_HEIGHT); } // If this is the goal, draw the orb if (cavern.getTileAt(row, col).getType() == Tile.Type.ORB) { page.drawImage(orb, TILE_WIDTH * col, TILE_HEIGHT * row, TILE_WIDTH, TILE_HEIGHT, null); } // If there is a coin here, draw it if (cavern.getTileAt(row, col).getGold() > 0) { page.drawImage( getGoldIcon(cavern.getNodeAt(row, col)), TILE_WIDTH * col, TILE_HEIGHT * row, TILE_WIDTH, TILE_HEIGHT, null); } // If this tile is the entrance, draw the graphic if (cavern.getTileAt(row, col).getType() == Tile.Type.ENTRANCE) { page.drawImage( entrance, TILE_WIDTH * col, TILE_HEIGHT * row, TILE_WIDTH, TILE_HEIGHT, null); } } // This is a wall else { page.drawImage(wall, TILE_WIDTH * col, TILE_HEIGHT * row, TILE_WIDTH, TILE_HEIGHT, null); } } } }