-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
78 lines (64 loc) · 1.75 KB
/
GUI.java
File metadata and controls
78 lines (64 loc) · 1.75 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
/*
* GUI stands for graphical user interface. It controls all the user
* input/output.
* It should be separate from the "model" that stores/manipulates data.
*/
import java.awt.Color;
public abstract class GUI {
protected String title = "";
protected Color titleColor = StdDraw.BLACK;
//Available to subtypes
protected Box[] boxes;
public void setTitleAndColor(String title, Color titleColor) {
this.title = title;
this.titleColor = titleColor;
}
/*
* This something that the main game should call
* Returns 0 if no valid move has been made.
* Returns an integer representing the move if one has been made
* This should work for any type of GUI -> the code gets written here.
*/
public int getMove(){
if(StdDraw.mousePressed()) {
while(StdDraw.mousePressed()) {
}
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
for(int i=0; i < boxes.length; i++) {
if(boxes[i].contains(x,y)) {
return boxes[i].getBoxcode();
}
}
}
return 0;
}
/*
* display output to the screen
*/
abstract void draw();
//Available to subtypes
protected class Box{
private double x0,y0,x1,y1;
private int boxcode;
public Box(double x0, double y0, double x1, double y1, int boxcode) {
this.x0 = x0;
this.y0 = y0;
this.x1 = x1;
this.y1 = y1;
this.boxcode = boxcode;
}
/*
* Return true if this Box contains this point
*/
public boolean contains(double x, double y) {
return(x0 < x && x < x1 && y0 < y && y < y1);
}
public int getBoxcode() {
return boxcode;
}
public void drawBox() {
StdDraw.rectangle((x0+x1)/2, (y0+y1)/2, Math.abs(x1-x0)/2, Math.abs(y1-y0)/2);
}
}
}