-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePanel.java
More file actions
96 lines (85 loc) · 3.67 KB
/
GamePanel.java
File metadata and controls
96 lines (85 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class GamePanel extends JPanel {
private HashMap<Poly, Polygon> polyToGon;
private Random rand;
//Intializes game panel with the polygon and random object from MisalignGraphics
public GamePanel(HashMap<Poly, Polygon> polyToGon, Random rand) {
this.polyToGon = polyToGon;
this.rand = rand;
this.setPreferredSize(new Dimension(500, 500));
this.setFont(new Font("Impact", Font.PLAIN, 64));
}
//Draws the game board
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g; //Graphics2D is a better version of the Graphics object
if (!MisalignGraphics.playingLossAnimation && !MisalignGraphics.gamePaused)
MisalignGraphics.checkGameEnd();
if (!MisalignGraphics.gamePaused) {
MisalignGraphics.setXM(this.getWidth());
MisalignGraphics.setYM(this.getHeight());
Misalignsweeper.generateAWTPolygons(MisalignGraphics.getXM(), MisalignGraphics.getYM());
for (Poly poly : polyToGon.keySet()) {
displayPolyColor(poly, g2);
drawImageInMine(poly, g2);
drawPolyOutline(polyToGon.get(poly), g2);
displayResult(g2);
}
}
}
//Fills poly with correct color based on display state and settings
public void displayPolyColor(Poly poly, Graphics2D g2) {
if (poly.isPressed()) {
switch (poly.getDisplayState()) {
case -2:
g2.setColor(MisalignGraphics.getSettings().getColor(1)); //color of clicked mine
break;
case -1:
g2.setColor(MisalignGraphics.getSettings().getColor(2)); //color of other mines
break;
default:
g2.setColor(MisalignGraphics.getColor(poly.getDisplayState()));
}
} else if (poly.isFlagged())
g2.setColor(MisalignGraphics.getSettings().getColor(3));
else
g2.setColor(MisalignGraphics.getSettings().getColor(4)); //unrevealed tile color
g2.fillPolygon(polyToGon.get(poly));
}
//Draws flag, mine, or number within a poly
public void drawImageInMine(Poly poly, Graphics2D g2) {
if (poly.isPressed() && poly.getDisplayState() > 0) {
g2.setColor(MisalignGraphics.getSettings().getColor(20));
poly.drawNum(g2);
} else if (poly.isFlagged()) {
poly.drawImageInPoly(g2, MisalignGraphics.FLAG_IMAGE);
} else if (poly.getDisplayState() < 0 && MisalignGraphics.playingLossAnimation) {
poly.drawImageInPoly(g2, MisalignGraphics.MINE_IMAGE);
}
}
//Draw the outline of a polygon
public void drawPolyOutline(Polygon gon, Graphics2D g2) {
g2.setColor(MisalignGraphics.getSettings().getColor(0));
if (!MisalignGraphics.getSettings().noLinesModeChecked()) {
g2.drawPolygon(gon);
}
}
//Displays win or loss text (centered horizontally, just above the middle vertically)
public void displayResult(Graphics2D g2) {
g2.setFont(g2.getFont().deriveFont(70f));
FontMetrics fm = g2.getFontMetrics();
boolean won = MisalignGraphics.gameWon();
if (MisalignGraphics.playingLossAnimation || won) {
String endText = won ? "YOU WIN" : "YOU LOSE";
int tx = (this.getWidth() - fm.stringWidth(endText)) / 2;
int ty = (this.getHeight() - fm.getHeight()) / 2;
g2.setColor(MisalignGraphics.getSettings().getColor(8));
g2.drawString(endText, tx - 2, ty + 2); // draw shadow
g2.setColor(MisalignGraphics.getSettings().getColor(won ? 6 : 5));
g2.drawString(endText, tx, ty);
}
}
}