-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
73 lines (61 loc) · 1.47 KB
/
Grid.java
File metadata and controls
73 lines (61 loc) · 1.47 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
package animation;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
/**
* A black and white tetris grid. Each block is 30 x 30 and is a 10 by 22 grid.
*
* @author johnsonhsiung
*
*/
public class Grid extends JPanel
{
/**
* Paints the 10 by 22 grid.
*
* @param g The graphics component used to paint.
*/
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
super.setOpaque(true);
super.setBackground(Color.BLACK);
g2.setColor(Color.WHITE);
for (int w = 0; w < COLUMNS; w++)
{
for (int h = 0; h < ROWS; h++)
{
g2.drawRect(getPixelUnit(w), getPixelUnit(h), BLOCK_LENGTH, BLOCK_LENGTH);
}
}
}
/**
* Static method to get the block x or y index grid given pixels.
*
* @param pixel The pixel to convert
* @return The grid unit of the pixel
*/
public static int getGridUnitRounded(int pixel)
{
return (pixel + BLOCK_LENGTH - 1) / BLOCK_LENGTH;
}
/**
* Static method to get the pixel unit given grid unit
*
* @param gridUnit The grid unit to convert
* @return the pixels
*/
public static int getPixelUnit(int gridUnit)
{
return gridUnit * BLOCK_LENGTH;
}
private static final long serialVersionUID = 1L;
public static final int WIDTH = 300;
public static final int HEIGHT = 660;
public static final int ROWS = 22;
public static final int COLUMNS = 10;
public static final int BLOCK_LENGTH = WIDTH / COLUMNS;
}