Skip to content

Instantly share code, notes, and snippets.

@memish
Created October 23, 2018 00:28
Show Gist options
  • Select an option

  • Save memish/a917f7cb23c6a305f19baa3e59b787e3 to your computer and use it in GitHub Desktop.

Select an option

Save memish/a917f7cb23c6a305f19baa3e59b787e3 to your computer and use it in GitHub Desktop.
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;
import java.awt.Font;
import java.awt.FontMetrics;
public class MineSweeper {
private JFrame frame;
public MineSweeper() {
frame = new JFrame("Mine Sweeper");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(frame.getSize());
frame.add(new MineDraw(frame.getSize()));
frame.pack();
frame.setVisible(true);
}
public static void main(String... argv) {
new MineSweeper();
}
public static class MineDraw extends JPanel implements MouseListener {
int startX = 10;
int startY = 10;
int side = 40;
int turn = 0;
int yPlace = 0;
int[][] grid = new int[8][8];
public MineDraw(Dimension dimension) {
setSize(dimension);
setPreferredSize(dimension);
addMouseListener(this);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Dimension d = getSize();
startX = 0;
startY = 0;
//1) draw grid of squares
//2) draw numbers in the squares
/*
* g2.setColor(Color.red);
g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
g2.drawString(""+grid[row][col] , x,y);
3) assign -1 to mines. Randomly assign this to ten spots
*/
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
//grid[row][col]
g2.setColor(Color.black);
g2.drawRect(startX,startY,side,side);
startX += side;
}
startX = 0;
startY += side;
}
}
public void randomAssign(){
}
public void assignNumbers(){
}
public void mousePressed(MouseEvent e) {
int x = e.getX()/side;//column
int y = e.getY()/side;//row
// System.out.println(x + " " + y);
if(x<grid.length && y <grid[0].length){
randomAssign();
}
//
repaint();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment