-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPit.java
More file actions
158 lines (136 loc) · 3.86 KB
/
Copy pathPit.java
File metadata and controls
158 lines (136 loc) · 3.86 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.*;
import javax.swing.*;
/**
* The class that draws a pit and contains information about a pit.
* @author: Hung Phan
* @author: Lien Cao
* @author: Wendy Ta
* @version: 1.0 11/28/25
*/
public class Pit extends JComponent{
private String name;
private int numStones;
private Ellipse2D pit;
private double x, y, w, h;
private ArrayList<Stone> allStones;
/**
* Constructs the pit
* @param name the name of the pit
* @param x the left of the bounding rectangle
* @param y the top of the bounding rectangle
* @param w the width of the pit
* @param h the height of the pit
* @param numStones the number of stones in a pit
*/
public Pit(String name, double x, double y, double w, double h, int numStones){
this.name = name;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.numStones = numStones;
this.allStones = new ArrayList<>();
this.pit = new Ellipse2D.Double(x, y, w, h);
}
/**
* Gets the name of the pit
* @return name of the pit
*/
public String getName() {
return this.name;
}
/**
* Gets the pit
* @return a pit
*/
public Ellipse2D getPit(){
return pit;
}
/**
* Add stone to the pit
*/
public void addStone(){
numStones++;
repaint();
}
/**
* Remove stones from the pit
*/
public void removeStone() {
if (numStones > 0) {
numStones--;
}
repaint();
}
/**
* Sets the number of stones in the pit
* @param n the number of stones
*/
public void setNumStones(int n) {
this.numStones = n;
repaint();
}
/**
* Gets the number of stones
* @return the number of stones in the pit
*/
public int getNumStones() {
return numStones;
}
/**
* Gets all stones in the pits
* @return list of stones according to the pits
*/
public ArrayList<Stone> getAllStones() {
return this.allStones;
}
/**
* Draws the pit
* @param g2 the graphics context
*/
public void drawPit(Graphics2D g2) { //draw the pit then draw the stones in each pit
// Draw pit outline
g2.setColor(Color.BLACK);
g2.draw(pit);
// Draw stones inside the pit
if (numStones != 0) { //don't draw if no stones to draw, prevent divideByZeroException
drawStones(g2, pit, numStones);
}
}
/**
* Draws the stones in the pit
* @param g2 the graphics context
* @param pit the pit that stones are being draw
* @param num the number of stones
*/
public void drawStones(Graphics2D g2, Ellipse2D pit, int num) {
double pitX = pit.getX();
double pitY = pit.getY();
//double pitWidth = pit.getHeight();
double pitWidth = pit.getWidth();
double pitHeigth = pit.getHeight();
double stoneSize = 12;
g2.setColor(Color.black);
allStones.clear();
int cols = (int) Math.ceil(Math.sqrt(num));
int rows = (int) Math.ceil((double) num / cols);
double gapX = pitWidth / (cols + 1);
//double gapY = pitWidth / (rows + 1);
double gapY = pitHeigth / (rows + 1);
int count = 0; // counting the num of stones
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= cols; c++) {
if (count++ >= num) {
return; // stop when done
}
double x = pitX + c * gapX - stoneSize / 2;
double y = pitY + r * gapY - stoneSize / 2;
Stone stone = new Stone(x, y);
allStones.add(stone);
stone.drawStone(g2);
}
}
}
}