Skip to content
2 changes: 1 addition & 1 deletion Lab5/src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class Main {
public static void main(String[] args) {
JFrame myFrame = new JFrame("Color Grid");
JFrame myFrame = new JFrame("MineSweeper");
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
myFrame.setLocation(400, 150);
myFrame.setSize(400, 400);
Expand Down
149 changes: 111 additions & 38 deletions Lab5/src/MyMouseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import javax.swing.JFrame;

public class MyMouseAdapter extends MouseAdapter {
private Random generator = new Random();
public class MyMouseAdapter extends MouseAdapter
{


public void mousePressed(MouseEvent e) {
switch (e.getButton()) {
case 1: //Left mouse button
Expand All @@ -19,6 +21,7 @@ public void mousePressed(MouseEvent e) {
return;
}
}

JFrame myFrame = (JFrame) c;
MyPanel myPanel = (MyPanel) myFrame.getContentPane().getComponent(0);
Insets myInsets = myFrame.getInsets();
Expand All @@ -33,16 +36,16 @@ public void mousePressed(MouseEvent e) {
myPanel.mouseDownGridY = myPanel.getGridY(x, y);
myPanel.repaint();
break;
case 3: //Right mouse button
//Do nothing
case 3:
break;
default: //Some other button (2 = Middle mouse button, etc.)
//Do nothing
break;
}
}
public void mouseReleased(MouseEvent e) {
switch (e.getButton()) {
switch (e.getButton())
{
case 1: //Left mouse button
Component c = e.getComponent();
while (!(c instanceof JFrame)) {
Expand All @@ -63,6 +66,8 @@ public void mouseReleased(MouseEvent e) {
myPanel.y = y;
int gridX = myPanel.getGridX(x, y);
int gridY = myPanel.getGridY(x, y);


if ((myPanel.mouseDownGridX == -1) || (myPanel.mouseDownGridY == -1)) {
//Had pressed outside
//Do nothing
Expand All @@ -76,42 +81,110 @@ public void mouseReleased(MouseEvent e) {
//Do nothing
} else {
//Released the mouse button on the same cell where it was pressed
if ((gridX == 0) || (gridY == 0)) {
//On the left column and on the top row... do nothing


if(myPanel.flagged[myPanel.mouseDownGridX][myPanel.mouseDownGridY]==false)
{
if(myPanel.mines[myPanel.mouseDownGridX][myPanel.mouseDownGridY]== 1)//if it hits a bomb reveal everything
{
for (int i = 0; i < myPanel.getTotalCol(); i++)
{
for (int j = 0; j < myPanel.getTotalRows(); j++)
{

if(myPanel.mines[i][j]== 1)//paints bomb cells black
myPanel.colorArray[i][j] = Color.BLACK;
myPanel.revealed[i][j]=true;

}
}




}else
{
if(myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]==true){
}else
{
myPanel.clearZeros(gridX, gridY);
}
}
}

}
}
myPanel.repaint();
break;
}
case 3: //when right click flag (paint red)
Component cr = e.getComponent();
while (!(cr instanceof JFrame)) {
cr = cr.getParent();
if (cr == null) {
return;
}
}

JFrame myRightClickFrame = (JFrame) cr;
MyPanel myRightClickPanel = (MyPanel) myRightClickFrame.getContentPane().getComponent(0);
Insets myRightClickInsets = myRightClickFrame.getInsets();
int xr1 = myRightClickInsets.left;
int yr1 = myRightClickInsets.top;
e.translatePoint(-xr1, -yr1);
int xr2 = e.getX();
int yr2 = e.getY();
myRightClickPanel.mouseDownGridX = myRightClickPanel.getGridX(xr2, yr2);
myRightClickPanel.mouseDownGridY = myRightClickPanel.getGridY(xr2, yr2);
myRightClickPanel.x = xr2;
myRightClickPanel.y = yr2;


int rightClickGridX = myRightClickPanel.getGridX(xr2, yr2);
int rightClickGridY = myRightClickPanel.getGridY(xr2, yr2);


if ((myRightClickPanel.mouseDownGridX == -1) || (myRightClickPanel.mouseDownGridY == -1)) {
//Had pressed outside
//Do nothing
} else {
if ((rightClickGridX == -1) || (rightClickGridY == -1)) {
//Is releasing outside
//Do nothing
} else {
if ((myRightClickPanel.mouseDownGridX != rightClickGridX) || (myRightClickPanel.mouseDownGridY != rightClickGridY)) {
//Released the mouse button on a different cell where it was pressed
//Do nothing
} else {
//On the grid other than on the left column and on the top row:
Color newColor = null;
switch (generator.nextInt(5)) {
case 0:
newColor = Color.YELLOW;
break;
case 1:
newColor = Color.MAGENTA;
break;
case 2:
newColor = Color.BLACK;
break;
case 3:
newColor = new Color(0x964B00); //Brown (from http://simple.wikipedia.org/wiki/List_of_colors)
break;
case 4:
newColor = new Color(0xB57EDC); //Lavender (from http://simple.wikipedia.org/wiki/List_of_colors)
break;
//Released the mouse button on the same cell where it was pressed



if(myRightClickPanel.revealed[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY]!= true)//does not let to marked revealed spaces
{

if(myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY]== false)//paints the cell if it doesn't have a bomb
{
myRightClickPanel.colorArray[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = Color.RED;
myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = true;


}else
{
myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = false;
myRightClickPanel.colorArray[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = Color.WHITE;
}
}

}
myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = newColor;
myPanel.repaint();
}
myRightClickPanel.repaint();
break;
}
}
break;
default: //Some other button (2 = Middle mouse button, etc.)
//Do nothing
break;
}
myPanel.repaint();
break;
case 3: //Right mouse button
//Do nothing
break;
default: //Some other button (2 = Middle mouse button, etc.)
//Do nothing
break;
}
}
}
}
Loading