Created
November 13, 2012 00:57
-
-
Save chintanparikh/4063162 to your computer and use it in GitHub Desktop.
Dungeon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.event.*; | |
| import javax.swing.*; | |
| import java.awt.Graphics; | |
| public class DisplayPanel extends JPanel | |
| { | |
| private Tile[][] tiles; | |
| private Creature[] creatures; | |
| private Dungeon dungeon; | |
| private Player player; | |
| public DisplayPanel(Dungeon dungeon, Tile[][] tiles, Creature[] creatures, Player player) | |
| { | |
| this.tiles = tiles; | |
| this.creatures = creatures; | |
| this.dungeon = dungeon; | |
| this.player = player; | |
| this.addKeyListener(new DungeonKeyListener()); | |
| } | |
| protected void paintComponent(Graphics g) | |
| { | |
| int maximum = (getWidth() < getHeight()) ? getWidth() : getHeight(); | |
| for (Tile[] row : tiles) | |
| { | |
| for (Tile tile : row) | |
| { | |
| if (tile != null && tile instanceof Tile) | |
| { | |
| tile.draw(g, maximum/tiles.length, maximum/tiles[0].length); | |
| } | |
| } | |
| } | |
| for (Creature creature : creatures) | |
| { | |
| if (creature != null && creature instanceof Creature) | |
| { | |
| creature.draw(g, maximum/tiles.length, maximum/tiles[0].length); | |
| } | |
| } | |
| if (player != null && player instanceof Player) | |
| { | |
| player.draw(g, maximum/tiles.length, maximum/tiles[0].length); | |
| } | |
| } | |
| private class DungeonKeyListener extends KeyAdapter | |
| { | |
| public void keyReleased(KeyEvent e) | |
| { | |
| System.out.println("Key pressed!"); | |
| dungeon.press(e.getKeyCode()); | |
| repaint(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment