-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoard.java
More file actions
149 lines (134 loc) · 4.29 KB
/
Board.java
File metadata and controls
149 lines (134 loc) · 4.29 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.border.Border;
/**
*
* @author James
*/
public class Board extends javax.swing.JPanel {
private int[][] grid;
private JLabel[][] labelGrid;
private Tetrimino piece;
/**
* Creates new form Board
*/
public Board(int[][] grid, Tetrimino piece) {
//initComponents();
this.grid = grid;
this.piece = piece;
setLayout(new GridLayout(grid.length-4, grid[0].length));
init();
}
public void init()
{
//build grid
labelGrid = new JLabel[grid.length-4][grid[0].length];
Border border = BorderFactory.createLineBorder(Color.BLACK);
for (int i = 0; i < labelGrid.length; i++)
{
for (int j = 0; j < labelGrid[0].length; j++)
{
labelGrid[i][j] = new JLabel();
labelGrid[i][j].setBorder(border);
labelGrid[i][j].setOpaque(true);
labelGrid[i][j].setBackground(Color.LIGHT_GRAY);
add(labelGrid[i][j]);
}
}
repaint();
validate();
}
public void update()
{
for (int i = 0; i < labelGrid.length; i++)
{
for (int j = 0; j < labelGrid[0].length; j++)
{
int med = grid[i+4][j];
if (med == -1)
{
med = this.piece.getType();
}
if (med == 0) labelGrid[i][j].setBackground(Color.LIGHT_GRAY);
else if (med == 1) labelGrid[i][j].setBackground(Color.RED);
else if (med == 2) labelGrid[i][j].setBackground(Color.YELLOW);
else if (med == 3) labelGrid[i][j].setBackground(Color.GREEN);
else if (med == 4) labelGrid[i][j].setBackground(Color.ORANGE);
else if (med == 5) labelGrid[i][j].setBackground(Color.CYAN);
else if (med == 6) labelGrid[i][j].setBackground(Color.BLUE);
else labelGrid[i][j].setBackground(Color.MAGENTA);
}
}
repaint();
validate();
}
//starts from bottom and heads up, looking for lines to clear; returns false when there's nothing
public int clearLine()
{
int count = 0;
for (int i = grid.length-1; i >= 1;)
{
//check if it's a row to remove
boolean z = true;
for (int j = 0; j < grid[i].length; j++)
{
if (grid[i][j] == 0)
{
z = false;
break;
}
}
//remove it, shift everything down one
if (z)
{
for (int k = i; k >= 1; k--)
{
for (int l = 0; l < grid[i].length; l++)
{
grid[k][l] = grid[k-1][l];
}
}
count++;
} else
{
i--;
}
}
return count;
}
public void clear()
{
for (int i = 0; i < grid.length; i++)
{
for (int j = 0; j < grid[0].length; j++)
{
grid[i][j] = 0;
}
}
update();
}
//because apparently java refuses to update the object
public void setPiece(Tetrimino piece)
{
this.piece = piece;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
setLayout(new java.awt.GridLayout());
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}