Here is the source code I got from the lab for MINESWEEPER, I need to understand
ID: 3582142 • Letter: H
Question
Here is the source code I got from the lab for MINESWEEPER,
I need to understand this since I haven't learn, Can some one just give me some comment on them to explain how does it work?
private static final long serialVersionUID = 1L;
private final static Color clicked = new Color(10,200,200);
private final static Color bomb= Color.red;
private final static Color safe = Color.blue;
private int rows;
private int cols;
private Color[][] color;
private Color lineColor;
private boolean[][] bombs;
private boolean lose;
private int safeSpace;
private int flaggedSpace;
setPreferredSize( new Dimension(square*columns,
square*rows) );
addMouseListener(this);
defineMines();
}
private int findR(int getY) {
return (int)(((double)getY)/getHeight()*rows);
}
private int findCol(int getX) {
return (int)(((double)getX)/getWidth()*cols);
}
row = findR( evt.getY() );
col = findCol( evt.getX() );
Show the paintComponent:
protected void paintComponent(Graphics g) {
g.setColor(clicked);
g.fillRect(0,0,getWidth(),getHeight());
int row, col;
double cellWidth = (double)getWidth() /cols;
double cellHeight = (double)getHeight() / rows;
for (row = 0; row < rows; row++) {
for (col = 0; col < cols; col++) {
if (color[row][col] != null) {
int x1 = (int)(col*cellWidth);
int y1 = (int)(row*cellHeight);
int x2 = (int)((col+1)*cellWidth);
int y2 = (int)((row+1)*cellHeight);
g.setColor(color[row][col]);
g.fillRect( x1, y1, (x2-x1), (y2-y1) );
if(color[row][col]==safe){
g.setColor(Color.BLACK);
g.drawString(""+countMines(row, col),x2-40,y2-40);
}
else {
g.setColor(Color.RED);
g.drawString(" ",x2-30,y2-30);
}
if(lose==true){
if (bombs[row][col] == true){
g.drawString("B",x2-30,y2-30);
}
}
}
}
}
if (lineColor != null) {
g.setColor(lineColor);
for (row = 1; row < rows; row++) {
int y = (int)(row*cellHeight);
g.drawLine(0,y,getWidth(),y);
}
for (col = 1; col < rows; col++) {
int x = (int)(col*cellWidth);
g.drawLine(x,0,x,getHeight());
}
}
}
Explanation / Answer
PROGRAM CODE WITH COMMENTS:
//serialversion id to have a unique id for each particular object being created.
//this will be helpful while writing that object into a file
private static final long serialVersionUID = 1L;
// declaring a color variable and storing a color using RGB format.
//R-10, G-200, B-200
private final static Color clicked = new Color(10,200,200);
// directly assigning the value of color red here
private final static Color bomb= Color.red;
// directly assigning the value of color blue here
private final static Color safe = Color.blue;
// normal declaration of integer variables to use for row and column in an array
private int rows;
private int cols;
// array of colors
private Color[][] color;
//variable to maintain color for a line
private Color lineColor;
//boolean to store whether bomb is at a cell or not
private boolean[][] bombs;
//boolean to indicate whether the player lost the game
private boolean lose;
private int safeSpace;
private int flaggedSpace;
//setting the dimension with rows and column values
setPreferredSize( new Dimension(square*columns,
square*rows) );
//adding mouse listener to this object so that mouse click will be recognized
addMouseListener(this);
defineMines();
}
//using a formula to find a row
private int findR(int getY) {
return (int)(((double)getY)/getHeight()*rows);
}
//using a formula to find a row
private int findCol(int getX) {
return (int)(((double)getX)/getWidth()*cols);
}
row = findR( evt.getY() );
col = findCol( evt.getX() );
//this is the paint component function whic will paint the panel
Show the paintComponent:
protected void paintComponent(Graphics g) {
//graphics is the class that will do coloring and drawing for the panel
//setting the color to graphics with the color that is already defined in the variable clicked.
//now whatever you do with graphics will appear in the color defined in clicked
g.setColor(clicked);
//fillRect will draw a rectangle that is filled with color(clicked).
//the first two parameters are corresponding to coordinates - x, y
// the next two parameters are width and height of he rectangle
//the height and width will be from the container in which this method is defined.
//after this code the whole panel will be filled with color and this will be the border for the whole game board
g.fillRect(0,0,getWidth(),getHeight());
int row, col;
// calculating the width and height for each cell based on the number of rows and columns
double cellWidth = (double)getWidth() /cols;
double cellHeight = (double)getHeight() / rows;
//the following code will create cells, fill each cell with color and then draw a string within it
// it will draw the string with value returned fromt he function if the color is equal to safe otherwise it just draws a space
// if lose becomes true at any point , then this will draw 'B'
// after this the game board will be ready
for (row = 0; row < rows; row++) {
for (col = 0; col < cols; col++) {
if (color[row][col] != null) { //checkign that the array holds value in the particular location
int x1 = (int)(col*cellWidth);
int y1 = (int)(row*cellHeight);
int x2 = (int)((col+1)*cellWidth);
int y2 = (int)((row+1)*cellHeight);
g.setColor(color[row][col]);
g.fillRect( x1, y1, (x2-x1), (y2-y1) );
if(color[row][col]==safe){
g.setColor(Color.BLACK);
g.drawString(""+countMines(row, col),x2-40,y2-40);
}
else {
g.setColor(Color.RED);
g.drawString(" ",x2-30,y2-30);
}
if(lose==true){
if (bombs[row][col] == true){
g.drawString("B",x2-30,y2-30);
}
}
}
}
}
//drawing the border lines for each cell
if (lineColor != null) {
g.setColor(lineColor);
for (row = 1; row < rows; row++) {
int y = (int)(row*cellHeight);
g.drawLine(0,y,getWidth(),y);
}
for (col = 1; col < rows; col++) {
int x = (int)(col*cellWidth);
g.drawLine(x,0,x,getHeight());
}
}
}