-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDGrid.java
More file actions
130 lines (108 loc) · 3.63 KB
/
DGrid.java
File metadata and controls
130 lines (108 loc) · 3.63 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
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class DrawGrid {
private JFrame frame;
public DrawGrid() {
frame = new JFrame("DrawGrid");
frame.setSize(310, 400);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(frame.getSize());
frame.add(new MultiDraw(frame.getSize()));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] argv) {
new DrawGrid();
}
public static class MultiDraw extends JPanel implements MouseListener {
int startX = 10;
int startY = 10;
int cellWidth = 40; //Coin
int turn = 2; //players
int rows = 6;//rows
int cols = 7;//columns
Color[][] grid = new Color[rows][cols];
public MultiDraw(Dimension dimension) {
setSize(dimension);
setPreferredSize(dimension);
addMouseListener(this);
//1. initialize array here
int x = 0;
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
grid[row][col]= new Color(255,255,255);
}
}
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Dimension d = getSize();
g2.setColor(new Color(0, 200, 200));
g2.fillRect(0,0,d.width,d.height);
g2.setColor(new Color(0, 0, 200));
// g2.fillRect(60,60,d.width,d.height);
startX = 0;
startY = 0;
//2) draw grid here
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
g2.setColor(grid[row][col]);
g2.fillOval(startX, startY, cellWidth, cellWidth);
startX+=cellWidth;
}
startX=0;
startY+=cellWidth;
}
g2.setColor(new Color(255, 255, 255));
if(turn%2==0){
g2.drawString("Red's Turn", 20, 350);
}else{
g2.drawString("Yellow's Turn",20,350);
}
}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int Xspot= x/cellWidth;
int Yspot = y/cellWidth;
Yspot= Test(Xspot);
if(Yspot<0){
System.out.println("Not a Valid Entry");
}
else{
if (turn%2==0){
grid[Yspot][Xspot]= new Color(255,0,0);
}
else{
grid[Yspot][Xspot]= new Color(255,255,0);
}
turn++;
}
repaint();
}
public int Test(int Xspot){
int yspot = rows-1;
while(!(grid[yspot][Xspot].equals(new Color(255,255,255))|| yspot<0)){
yspot--;
}
return yspot;
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
}
}