-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathResizableGrid.java
More file actions
54 lines (42 loc) · 1.58 KB
/
ResizableGrid.java
File metadata and controls
54 lines (42 loc) · 1.58 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
/*
* File: ResizableGrid.java
* ----------------------------
* მთელ ფორმაზე ბადის დახატვა, ისე რომ ფორმის ზომის ცვლილებაზე ბადის უჯრის ზომაც
* პროპორციულად იზრდებოდეს.
*
* დეტალური ამოხსნა იხილეთ ResizableGrid.md ფაილში.
*/
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import acm.graphics.GRect;
import acm.program.GraphicsProgram;
public class GraphicsProgramSample extends GraphicsProgram implements ComponentListener {
// Size of the board
private final int BOARD_WIDTH = 10;
private final int BOARD_HEIGHT = 10;
public void run() {
addComponentListener(this);
}
public void componentResized(ComponentEvent e) {
drawBoard(getWidth(), getHeight());
}
// This method draw board with (width, height) size
private void drawBoard(int width, int height) {
removeAll();
int cellWidth = width / BOARD_WIDTH; // Width of each cell
int cellHeight = height / BOARD_HEIGHT; // Height of each cell
for (int k = 0; k < BOARD_WIDTH; k++) {
for (int i = 0; i < BOARD_HEIGHT; i++) {
// Coordinates of the (k,i) cell will be (k * cellWidth, i * cellHeight)
GRect cell = new GRect(k * cellWidth, i * cellHeight, cellWidth, cellHeight);
add(cell);
}
}
}
public void componentHidden(ComponentEvent e) {
}
public void componentMoved(ComponentEvent e) {
}
public void componentShown(ComponentEvent e) {
}
}