diff options
Diffstat (limited to 'src/ch/epfl/xblast/client/XBlastComponent.java')
-rw-r--r-- | src/ch/epfl/xblast/client/XBlastComponent.java | 103 |
1 files changed, 101 insertions, 2 deletions
diff --git a/src/ch/epfl/xblast/client/XBlastComponent.java b/src/ch/epfl/xblast/client/XBlastComponent.java index 0ef8c5c..f90d6ee 100644 --- a/src/ch/epfl/xblast/client/XBlastComponent.java +++ b/src/ch/epfl/xblast/client/XBlastComponent.java | |||
@@ -20,36 +20,69 @@ import java.util.stream.IntStream; | |||
20 | */ | 20 | */ |
21 | public final class XBlastComponent extends JComponent { | 21 | public final class XBlastComponent extends JComponent { |
22 | 22 | ||
23 | /** | ||
24 | * Representation of the client's window as a grid. | ||
25 | */ | ||
23 | private static class Grid { | 26 | private static class Grid { |
24 | 27 | ||
28 | /** | ||
29 | * Parameters of a grid. | ||
30 | */ | ||
25 | private static final Dimension CELL_DIMENSION = new Dimension(64, 48); | 31 | private static final Dimension CELL_DIMENSION = new Dimension(64, 48); |
26 | private static final Dimension SCORE_IMG_DIMENSION = new Dimension(48, 48); | 32 | private static final Dimension SCORE_IMG_DIMENSION = new Dimension(48, 48); |
27 | private static final Dimension TIME_LED_DIMENSION = new Dimension(16, 16); | 33 | private static final Dimension TIME_LED_DIMENSION = new Dimension(16, 16); |
28 | private static final Dimension FRAME_DIMENSION = totalDimension(); | 34 | private static final Dimension FRAME_DIMENSION = totalDimension(); |
29 | 35 | ||
36 | /** | ||
37 | * Positions of the elements on the grid. | ||
38 | */ | ||
30 | private static final List<Point> CELL_POSITIONS = buildCellGrid(CELL_DIMENSION); | 39 | private static final List<Point> CELL_POSITIONS = buildCellGrid(CELL_DIMENSION); |
31 | private static final List<Point> SCORE_BG_POSITIONS = buildScoreGrid(); | 40 | private static final List<Point> SCORE_BG_POSITIONS = buildScoreGrid(); |
32 | private static final List<Point> TIME_LINE_POSITIONS = buildTimeLineGrid(); | 41 | private static final List<Point> TIME_LINE_POSITIONS = buildTimeLineGrid(); |
33 | private static final Map<PlayerID, Point> SCORE_TXT_POSITIONS = buildScoreMap(659); | 42 | private static final Map<PlayerID, Point> SCORE_TXT_POSITIONS = buildScoreMap(659); |
34 | 43 | ||
44 | /** | ||
45 | * Returns the dimension of the grid. | ||
46 | * | ||
47 | * @return the dimension of the grid | ||
48 | */ | ||
35 | private static Dimension totalDimension() { | 49 | private static Dimension totalDimension() { |
36 | return new Dimension( | 50 | return new Dimension( |
37 | CELL_DIMENSION.width * Cell.COLUMNS, | 51 | CELL_DIMENSION.width * Cell.COLUMNS, |
38 | CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height + TIME_LED_DIMENSION.height); | 52 | CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height + TIME_LED_DIMENSION.height); |
39 | } | 53 | } |
40 | 54 | ||
55 | /** | ||
56 | * Returns a list of positions of every cell on the grid. | ||
57 | * | ||
58 | * @param elementDim dimension of a cell | ||
59 | * @return a list of positions of every cell on the grid | ||
60 | */ | ||
41 | private static List<Point> buildCellGrid(Dimension elementDim) { | 61 | private static List<Point> buildCellGrid(Dimension elementDim) { |
42 | return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream() | 62 | return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream() |
43 | .map(c -> new Point(c.x() * elementDim.width, c.y() * elementDim.height)) | 63 | .map(c -> new Point(c.x() * elementDim.width, c.y() * elementDim.height)) |
44 | .collect(Collectors.toList())); | 64 | .collect(Collectors.toList())); |
45 | } | 65 | } |
46 | 66 | ||
67 | /** | ||
68 | * Builds a list containing the positions of the elements of a line. | ||
69 | * | ||
70 | * @param elementDim dimension of the every element of the line | ||
71 | * @param len length of the line | ||
72 | * @param shift shift | ||
73 | * @return list containing the positions of the elements of a line. | ||
74 | */ | ||
47 | private static List<Point> buildLine(Dimension elementDim, int len, Point shift) { | 75 | private static List<Point> buildLine(Dimension elementDim, int len, Point shift) { |
48 | return Collections.unmodifiableList(IntStream.range(0, len) | 76 | return Collections.unmodifiableList(IntStream.range(0, len) |
49 | .mapToObj(x -> new Point(x * elementDim.width + shift.x, shift.y)) | 77 | .mapToObj(x -> new Point(x * elementDim.width + shift.x, shift.y)) |
50 | .collect(Collectors.toList())); | 78 | .collect(Collectors.toList())); |
51 | } | 79 | } |
52 | 80 | ||
81 | /** | ||
82 | * Builds the list of the positions of the scores' line elements. | ||
83 | * | ||
84 | * @return the list of the positions of the scores' line elements. | ||
85 | */ | ||
53 | private static List<Point> buildScoreGrid() { | 86 | private static List<Point> buildScoreGrid() { |
54 | return buildLine( | 87 | return buildLine( |
55 | SCORE_IMG_DIMENSION, | 88 | SCORE_IMG_DIMENSION, |
@@ -57,6 +90,11 @@ public final class XBlastComponent extends JComponent { | |||
57 | new Point(0, CELL_DIMENSION.height * Cell.ROWS)); | 90 | new Point(0, CELL_DIMENSION.height * Cell.ROWS)); |
58 | } | 91 | } |
59 | 92 | ||
93 | /** | ||
94 | * Builds the list of the positions of the time "line" elements. | ||
95 | * | ||
96 | * @return the list of the positions of the time "line" elements. | ||
97 | */ | ||
60 | private static List<Point> buildTimeLineGrid() { | 98 | private static List<Point> buildTimeLineGrid() { |
61 | return buildLine( | 99 | return buildLine( |
62 | TIME_LED_DIMENSION, | 100 | TIME_LED_DIMENSION, |
@@ -64,6 +102,12 @@ public final class XBlastComponent extends JComponent { | |||
64 | new Point(0, CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height)); | 102 | new Point(0, CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height)); |
65 | } | 103 | } |
66 | 104 | ||
105 | /** | ||
106 | * Builds the map linking the players' ID to the location of their scores on the grid. | ||
107 | * | ||
108 | * @param vShift vertical position of the scores on the grid | ||
109 | * @return map linking the players' ID to the location of their scores on the grid | ||
110 | */ | ||
67 | private static Map<PlayerID, Point> buildScoreMap(int vShift) { | 111 | private static Map<PlayerID, Point> buildScoreMap(int vShift) { |
68 | Map<PlayerID, Point> m = new EnumMap<>(PlayerID.class); | 112 | Map<PlayerID, Point> m = new EnumMap<>(PlayerID.class); |
69 | m.put(PlayerID.PLAYER_1, new Point(96, vShift)); | 113 | m.put(PlayerID.PLAYER_1, new Point(96, vShift)); |
@@ -73,6 +117,12 @@ public final class XBlastComponent extends JComponent { | |||
73 | return m; | 117 | return m; |
74 | } | 118 | } |
75 | 119 | ||
120 | /** | ||
121 | * Computes the position of a player on the grid. | ||
122 | * | ||
123 | * @param p given player | ||
124 | * @return the player's position on the grid | ||
125 | */ | ||
76 | private static Point positionForPlayer(GameState.Player p) { | 126 | private static Point positionForPlayer(GameState.Player p) { |
77 | return new Point( | 127 | return new Point( |
78 | 4 * p.position().x() - 24, | 128 | 4 * p.position().x() - 24, |
@@ -81,27 +131,59 @@ public final class XBlastComponent extends JComponent { | |||
81 | 131 | ||
82 | } | 132 | } |
83 | 133 | ||
134 | /** | ||
135 | * Client's painter. | ||
136 | */ | ||
84 | private static class Painter { | 137 | private static class Painter { |
85 | 138 | ||
139 | /** | ||
140 | * Painter's parameters. | ||
141 | */ | ||
86 | private static final ImageObserver IMG_OBSERVER = null; | 142 | private static final ImageObserver IMG_OBSERVER = null; |
87 | private static final Font TXT_FONT = new Font("Arial", Font.BOLD, 25); | 143 | private static final Font TXT_FONT = new Font("Arial", Font.BOLD, 25); |
88 | private static final Color TXT_COLOR = Color.WHITE; | 144 | private static final Color TXT_COLOR = Color.WHITE; |
89 | 145 | ||
146 | /** | ||
147 | * Draws a string on the graphic context at the given point. | ||
148 | * | ||
149 | * @param g the graphic context | ||
150 | * @param pos position on the graphic context | ||
151 | * @param str string to be draw | ||
152 | */ | ||
90 | private static void drawString(Graphics2D g, Point pos, String str) { | 153 | private static void drawString(Graphics2D g, Point pos, String str) { |
91 | g.setColor(TXT_COLOR); | 154 | g.setColor(TXT_COLOR); |
92 | g.setFont(TXT_FONT); | 155 | g.setFont(TXT_FONT); |
93 | g.drawString(str, pos.x, pos.y); | 156 | g.drawString(str, pos.x, pos.y); |
94 | } | 157 | } |
95 | 158 | ||
159 | /** | ||
160 | * Draws an image on the graphic at the given point. | ||
161 | * | ||
162 | * @param g the graphic context | ||
163 | * @param pos position on the graphic context. | ||
164 | * @param img image to be draw | ||
165 | */ | ||
96 | private static void drawImage(Graphics2D g, Point pos, Image img) { | 166 | private static void drawImage(Graphics2D g, Point pos, Image img) { |
97 | g.drawImage(img, pos.x, pos.y, IMG_OBSERVER); | 167 | g.drawImage(img, pos.x, pos.y, IMG_OBSERVER); |
98 | } | 168 | } |
99 | 169 | ||
170 | /** | ||
171 | * Draws images given their location on the graphic context. | ||
172 | * | ||
173 | * @param g the graphic context | ||
174 | * @param m map linking images to points on the graphic context | ||
175 | */ | ||
100 | private static void drawImageMap(Graphics2D g, Map<Point, Image> m) { | 176 | private static void drawImageMap(Graphics2D g, Map<Point, Image> m) { |
101 | for (Map.Entry<Point, Image> e : m.entrySet()) | 177 | for (Map.Entry<Point, Image> e : m.entrySet()) |
102 | drawImage(g, e.getKey(), e.getValue()); | 178 | drawImage(g, e.getKey(), e.getValue()); |
103 | } | 179 | } |
104 | 180 | ||
181 | /** | ||
182 | * Draws strings given their location on the graphic context. | ||
183 | * | ||
184 | * @param g the graphic context | ||
185 | * @param m map linking strings to their locations | ||
186 | */ | ||
105 | private static <T> void drawStringMap(Graphics2D g, Map<Point, T> m) { | 187 | private static <T> void drawStringMap(Graphics2D g, Map<Point, T> m) { |
106 | for (Map.Entry<Point, T> e : m.entrySet()) | 188 | for (Map.Entry<Point, T> e : m.entrySet()) |
107 | drawString(g, e.getKey(), e.getValue().toString()); | 189 | drawString(g, e.getKey(), e.getValue().toString()); |
@@ -120,7 +202,17 @@ public final class XBlastComponent extends JComponent { | |||
120 | 202 | ||
121 | } | 203 | } |
122 | 204 | ||
205 | /** | ||
206 | * Sorts the players in order to have the current player at first. | ||
207 | * | ||