summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/graphics/Display.java
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-21 10:44:42 +0100
committerPacien TRAN-GIRARD2015-11-21 10:44:42 +0100
commit90bd766f361083f6fd9e39ff080c83fcc606832f (patch)
treedf81a931c9565a4b227068680087f58fdace1859 /src/ch/epfl/maze/graphics/Display.java
parent655ac88f4e73b2df532a451aedf5a561ea1b0d2c (diff)
downloadmaze-solver-90bd766f361083f6fd9e39ff080c83fcc606832f.tar.gz
Reformat imported code
Diffstat (limited to 'src/ch/epfl/maze/graphics/Display.java')
-rw-r--r--src/ch/epfl/maze/graphics/Display.java1030
1 files changed, 503 insertions, 527 deletions
diff --git a/src/ch/epfl/maze/graphics/Display.java b/src/ch/epfl/maze/graphics/Display.java
index ea08a06..2fddcf1 100644
--- a/src/ch/epfl/maze/graphics/Display.java
+++ b/src/ch/epfl/maze/graphics/Display.java
@@ -1,15 +1,12 @@
1package ch.epfl.maze.graphics; 1package ch.epfl.maze.graphics;
2 2
3import java.awt.Canvas; 3import ch.epfl.maze.physical.World;
4import java.awt.Color; 4import ch.epfl.maze.simulation.Simulation;
5import java.awt.EventQueue; 5
6import java.awt.Graphics2D; 6import javax.imageio.ImageIO;
7import java.awt.RenderingHints; 7import javax.swing.*;
8import java.awt.event.ActionEvent; 8import java.awt.*;
9import java.awt.event.ActionListener; 9import java.awt.event.*;
10import java.awt.event.KeyEvent;
11import java.awt.event.WindowAdapter;
12import java.awt.event.WindowEvent;
13import java.awt.image.BufferStrategy; 10import java.awt.image.BufferStrategy;
14import java.awt.image.BufferedImage; 11import java.awt.image.BufferedImage;
15import java.io.File; 12import java.io.File;
@@ -17,527 +14,506 @@ import java.io.IOException;
17import java.util.HashMap; 14import java.util.HashMap;
18import java.util.Map; 15import java.util.Map;
19 16
20import javax.imageio.ImageIO;
21import javax.swing.JCheckBoxMenuItem;
22import javax.swing.JFrame;
23import javax.swing.JMenu;
24import javax.swing.JMenuBar;
25import javax.swing.JMenuItem;
26import javax.swing.JOptionPane;
27import javax.swing.KeyStroke;
28import javax.swing.WindowConstants;
29
30import ch.epfl.maze.physical.World;
31import ch.epfl.maze.simulation.Simulation;
32
33/** 17/**
34 * Handles the display of a {@code Simulation} on a window. 18 * Handles the display of a {@code Simulation} on a window.
35 *
36 */ 19 */
37 20
38public final class Display implements Runnable { 21public final class Display implements Runnable {
39 22
40 /* constants */ 23 /* constants */
41 public static final Color BACKGROUND_COLOR = Color.GRAY; 24 public static final Color BACKGROUND_COLOR = Color.GRAY;
42 public static final int SQUARE_SIZE = 42; 25 public static final int SQUARE_SIZE = 42;
43 public static final int BUFFERS_NUMBER = 2; 26 public static final int BUFFERS_NUMBER = 2;
44 public static final int MAX_SPEED = 32; 27 public static final int MAX_SPEED = 32;
45 public static final int DEFAULT_SPEED = 2; 28 public static final int DEFAULT_SPEED = 2;
46 public static final int MIN_SPEED = 1; 29 public static final int MIN_SPEED = 1;
47 public static final int ANIMATION_SLEEP = 10; 30 public static final int ANIMATION_SLEEP = 10;
48 31
49 /* lock for mutual exclusion between human interactions and display */ 32 /* lock for mutual exclusion between human interactions and display */
50 private final Object mLock = new Object(); 33 private final Object mLock = new Object();
51 34
52 /* simulation and animation handlers */ 35 /* simulation and animation handlers */
53 private final Simulation mSimulation; 36 private final Simulation mSimulation;
54 private final Animation mAnimation; 37 private final Animation mAnimation;
55 private volatile float mSpeed; 38 private volatile float mSpeed;
56 39
57 /* actual window frame and canvas */ 40 /* actual window frame and canvas */
58 private JFrame mFrame; 41 private JFrame mFrame;
59 private JMenuBar mMenuBar; 42 private JMenuBar mMenuBar;
60 private Canvas mCanvas; 43 private Canvas mCanvas;
61 44
62 /* drawing buffers */ 45 /* drawing buffers */
63 private BufferStrategy mStrategy; 46 private BufferStrategy mStrategy;
64 private Map<Integer, BufferedImage> mTiles; 47 private Map<Integer, BufferedImage> mTiles;
65 48
66 /* control variables */ 49 /* control variables */
67 private boolean mRunning; 50 private boolean mRunning;
68 private boolean mPaused; 51 private boolean mPaused;
69 private boolean mShowGrid; 52 private boolean mShowGrid;
70 private boolean mDebug; 53 private boolean mDebug;
71 private boolean mFinished; 54 private boolean mFinished;
72 55
73 /** 56 /**
74 * Constructs a {@code Display} that will display a simulation. 57 * Constructs a {@code Display} that will display a simulation.
75 * 58 *
76 * @param simulation 59 * @param simulation A {@code Simulation} to display
77 * A {@code Simulation} to display 60 */
78 */ 61
79 62 public Display(Simulation simulation) {
80 public Display(Simulation simulation) { 63 // sanity check
81 // sanity check 64 if (simulation == null) {
82 if (simulation == null) { 65 throw new IllegalArgumentException("Simulation must be defined.");
83 throw new IllegalArgumentException("Simulation must be defined."); 66 }
84 } 67 if (simulation.getWorld() == null) {
85 if (simulation.getWorld() == null) { 68 throw new IllegalArgumentException("World in Simulation must be defined.");
86 throw new IllegalArgumentException("World in Simulation must be defined."); 69 }
87 } 70
88 71 // initiates instances
89 // initiates instances 72 mSimulation = simulation;
90 mSimulation = simulation; 73 mAnimation = new Animation(simulation.getWorld().getAnimals());
91 mAnimation = new Animation(simulation.getWorld().getAnimals()); 74 mSpeed = DEFAULT_SPEED;
92 mSpeed = DEFAULT_SPEED; 75
93 76 // default control variables
94 // default control variables 77 mRunning = true;
95 mRunning = true; 78 mPaused = false;
96 mPaused = false; 79 mShowGrid = false;
97 mShowGrid = false; 80 mDebug = false;
98 mDebug = false; 81 mFinished = false;
99 mFinished = false; 82
100 83 // creates menu
101 // creates menu 84 createMenu();
102 createMenu(); 85
103 86 // creates canvas
104 // creates canvas 87 createCanvas();
105 createCanvas(); 88
106 89 // creates window
107 // creates window 90 createWindow();
108 createWindow(); 91
109 92 // sets canvas and menu on frame
110 // sets canvas and menu on frame 93 mFrame.setJMenuBar(mMenuBar);
111 mFrame.setJMenuBar(mMenuBar); 94 mFrame.add(mCanvas);
112 mFrame.add(mCanvas); 95 mFrame.pack();
113 mFrame.pack(); 96 mFrame.setLocationRelativeTo(null);
114 mFrame.setLocationRelativeTo(null); 97
115 98 // creates buffer strategy
116 // creates buffer strategy 99 mCanvas.createBufferStrategy(BUFFERS_NUMBER);
117 mCanvas.createBufferStrategy(BUFFERS_NUMBER); 100 mStrategy = mCanvas.getBufferStrategy();
118 mStrategy = mCanvas.getBufferStrategy(); 101
119 102 // loads images of tiles
120 // loads images of tiles 103 mTiles = new HashMap<Integer, BufferedImage>();
121 mTiles = new HashMap<Integer, BufferedImage>(); 104 try {
122 try { 105 mTiles.put(World.FREE, ImageIO.read(new File("img/tiles/free.png")));
123 mTiles.put(World.FREE, ImageIO.read(new File("img/tiles/free.png"))); 106 mTiles.put(World.WALL, ImageIO.read(new File("img/tiles/wall.png")));
124 mTiles.put(World.WALL, ImageIO.read(new File("img/tiles/wall.png"))); 107 mTiles.put(World.START, ImageIO.read(new File("img/tiles/start.png")));
125 mTiles.put(World.START, ImageIO.read(new File("img/tiles/start.png"))); 108 mTiles.put(World.EXIT, ImageIO.read(new File("img/tiles/exit.png")));
126 mTiles.put(World.EXIT, ImageIO.read(new File("img/tiles/exit.png"))); 109 mTiles.put(World.NOTHING, ImageIO.read(new File("img/tiles/nothing.png")));
127 mTiles.put(World.NOTHING, ImageIO.read(new File("img/tiles/nothing.png"))); 110 } catch (IOException e) {
128 } catch (IOException e) { 111 e.printStackTrace();
129 e.printStackTrace(); 112 }
130 } 113 }
131 } 114
132 115 @Override
133 @Override 116 public void run() {
134 public void run() { 117 mFrame.setVisible(true);
135 mFrame.setVisible(true); 118 mainLoop();
136 mainLoop(); 119 mFrame.dispose();
137 mFrame.dispose(); 120 }
138 } 121
139 122 /**
140 /** 123 * Sets the debug control.
141 * Sets the debug control. 124 *
142 * 125 * @param debug The new debug value
143 * @param debug 126 */
144 * The new debug value 127
145 */ 128 public void setDebug(boolean debug) {
146 129 mDebug = debug;
147 public void setDebug(boolean debug) { 130 mShowGrid = debug;
148 mDebug = debug; 131 createMenu();