-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGrid.java
More file actions
49 lines (46 loc) · 1.15 KB
/
SnakeGrid.java
File metadata and controls
49 lines (46 loc) · 1.15 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
/**
* @(#)SnakeGrid.java
*
*
* @authors Martin Verde
* @version 1.00 2014/5/7
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
public class SnakeGrid extends SnakeObject
{
private JFrame frame = new JFrame();
private static final int WIDTH = 320;
private static final int HEIGHT = 320;
private static final int SCALE = 2;
private JPanel[][] grid;
public SnakeGrid(int width, int length) //constructs a grid of JPanels with int parameters for dimensions of the grid
{
frame.setLayout(new GridLayout(width, length));
grid = new JPanel[width][length];
for(int y=0; y<length; y++)
{
for(int x=0; x<width; x++)
{
grid[x][y] = new JPanel();
frame.add(grid[x][y]);
grid[x][y].setBackground(Color.GRAY);
}
}
frame.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public JPanel[][] getGrid() //returns the 2D array of JPanels
{
return grid;
}
public void closeFrame() //exits the game
{
frame.setVisible(false);
}
}