-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java
More file actions
61 lines (56 loc) · 2.5 KB
/
Copy pathSquare.java
File metadata and controls
61 lines (56 loc) · 2.5 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
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
public class Square extends Shape {
// class for creating the square shape, this class extends the abstract class Shape
public Square(int length, int i, int j) {
super(length, Type.SQUARE, i, j);
isConnected = new boolean[this.type.getValue()];
for (int ii = 0; ii < this.type.getValue(); ii++)
neighbors.add(null);
}
/*
drawing method:
checks the routes/edges connecting the different squares and
paint them with the corresponding visted color
*/
@Override
public void draw(Graphics g) {
if (g == null) return;
Graphics2D g2 = (Graphics2D) g.create();
try {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(this.explored.getColor());
double side = this.length * 3 / 2.0;
int x = (int) ((this.j + 1) * side);
int y = (int) ((this.i + 1) * side);
g2.fillRect(x, y, (int) side, (int) side);
g2.drawRect(x, y, (int) side, (int) side);
g2.setStroke(new BasicStroke(2));
for (int edge = 0; edge < 4; edge++) {
if (!isConnected[edge]) continue;
switch (edge) {
case 0 -> g2.drawLine(x, (int) (y + side), x, y); // Left
case 1 -> g2.drawLine(x, y, (int) (x + side), y); // Top
case 2 -> g2.drawLine((int) (x + side), y, (int) (x + side), (int) (y + side)); // Right
case 3 -> g2.drawLine((int) (x + side), (int) (y + side), x, (int) (y + side)); // Bottom
}
}
g2.setColor(Color.BLACK);
for (int edge = 0; edge < 4; edge++) {
if (isConnected[edge]) continue;
switch (edge) {
case 0 -> g2.drawLine(x, (int) (y + side), x, y); // Left
case 1 -> g2.drawLine(x, y, (int) (x + side), y); // Top
case 2 -> g2.drawLine((int) (x + side), y, (int) (x + side), (int) (y + side)); // Right
case 3 -> g2.drawLine((int) (x + side), (int) (y + side), x, (int) (y + side)); // Bottom
}
}
} finally {
g2.dispose();
}
}
}