-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridBox.java
More file actions
125 lines (113 loc) · 3.35 KB
/
GridBox.java
File metadata and controls
125 lines (113 loc) · 3.35 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package Nonogram;
import basicgraphics.*;
import basicgraphics.images.Picture;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
public class GridBox extends Sprite {
public final static String FILLED = "filled";
public final static String CROSSED = "crossed";
public final static String EMPTY = "empty";
private final int colNum;
private final int rowNum;
private String status = EMPTY;
private ArrayList<String> history = new ArrayList<String>();
private int historyIndex = 0;
public GridBox(SpriteComponent sc, int r, int c) {
super(sc);
this.colNum = c;
this.rowNum = r;
setPicture(makeBoxPicture(EMPTY));
setDrawingPriority(10000000);
int x = Grid.GRID_OFFSET + colNum * Grid.boxSize + 1;
int y = Grid.GRID_OFFSET + rowNum * Grid.boxSize + 1;
setX(x);
setY(y);
history.add(EMPTY);
}
/**
* Changes the box to filled if empty, or back to empty if it was already filled
*/
public void changeFilled() {
String newStatus = status.equals(FILLED) ? EMPTY : FILLED;
status = newStatus;
history.add(newStatus);
setPicture(makeBoxPicture(newStatus));
historyIndex++;
}
/**
* Changes the box to crossed if empty, or back to empty if it was already crossed
*/
public void changeCrossed() {
String newStatus = status.equals(CROSSED) ? EMPTY : CROSSED;
status = newStatus;
history.add(newStatus);
setPicture(makeBoxPicture(newStatus));
historyIndex++;
}
/**
* Gets the correct picture based on the status of the box
* @param boxType if it is filled, crossed, or empty
* @return
*/
private Picture makeBoxPicture(String boxType) {
Picture pic;
if (boxType.equals(FILLED)) {
pic = Grid.filled;
} else if (boxType.equals(CROSSED)) {
pic = Grid.crossed;
} else {
pic = Grid.empty;
}
return pic;
}
/**
* Reverts the box status to the previous status when there is an undo
*/
public void setToPreviousHistory() {
historyIndex--;
String lastStatus = history.get(historyIndex);
switch (lastStatus) {
case EMPTY:
if (status.equals(CROSSED)) {
changeCrossed();
} else if (status.equals(FILLED)) {
changeFilled();
}
break;
case CROSSED:
changeCrossed();
break;
case FILLED:
changeFilled();
break;
default:
break;
}
}
/**
* Get the column number of this box
* @return
*/
public int getCol() {
return colNum;
}
/** Get the row number of this box
*
* @return
*/
public int getRow() {
return rowNum;
}
/** Get the current status of this box
*
* @return
*/
public String getStatus() {
return status;
}
}