Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.event.MouseAdapter;
- import java.awt.image.BufferedImage;
- import java.net.URL;
- import javax.swing.JEditorPane;
- import javax.swing.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.border.TitledBorder;
- public class P4_Namiranian_Pedram_Minesweeper_GUIView {
- P4_Namiranian_Pedram_Minesweeper_Controller control;
- MyPanel board;
- JFrame window;
- time elapsed;
- MinesLeft mines;
- public P4_Namiranian_Pedram_Minesweeper_GUIView(P4_Namiranian_Pedram_Minesweeper_Controller c) {
- control = c;
- // WINDER
- window = new JFrame("P4_Namiranian_Pedram_Minesweeper");
- window.setBounds(100, 20, 445, 550);
- window.setResizable(false);
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setLayout(null);
- // MENU
- JMenuBar bar = new JMenuBar();
- bar.setVisible(true);
- JMenu game = new JMenu("Game");
- JMenu option = new JMenu("Option");
- JMenu help = new JMenu("Help");
- JMenuItem newGame = new JMenuItem("New Game");
- newGame.addActionListener(e -> {
- control.resetGame();
- });
- JMenuItem exit = new JMenuItem("Exit");
- exit.addActionListener(e -> {
- window.dispose();
- });
- JMenuItem setNumMines = new JMenuItem("Set Number Of Mines");
- setNumMines.addActionListener(e -> {
- int mines = Integer.parseInt(JOptionPane.showInputDialog("How many Mines?"));
- control.resetMines(mines);
- });
- JMenuItem howTo = new JMenuItem("How To Play");
- howTo.addActionListener(e -> {
- try {
- JEditorPane pane = new JEditorPane(new URL("file:how.html"));
- JScrollPane scroll = new JScrollPane(pane);
- JOptionPane.showMessageDialog(null, scroll, "How To Play", JOptionPane.PLAIN_MESSAGE, null);
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- });
- JMenuItem about = new JMenuItem("About");
- about.addActionListener(e -> {
- try {
- JEditorPane pane = new JEditorPane(new URL("file:about.html"));
- JScrollPane scroll = new JScrollPane(pane);
- JOptionPane.showMessageDialog(null, scroll, "About", JOptionPane.PLAIN_MESSAGE, null);
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- });
- game.add(newGame);
- game.add(exit);
- option.add(setNumMines);
- help.add(howTo);
- help.add(about);
- bar.add(game);
- bar.add(option);
- bar.add(help);
- // JPANEL
- board = new MyPanel();
- board.setBounds(20, 20, 400, 400);
- board.repaint();
- // TIME ELAPSED
- elapsed = new time();
- elapsed.setBorder(new TitledBorder("Time Elapsed"));
- elapsed.setBounds(100, 430, 100, 50);
- // NUMBER OF MINES
- mines = new MinesLeft();
- mines.setBorder(new TitledBorder("Mines"));
- mines.setBounds(230, 430, 100, 50);
- // LAST THINGS
- window.setJMenuBar(bar);
- window.getContentPane().add(board);
- window.getContentPane().add(elapsed);
- window.getContentPane().add(mines);
- window.setVisible(true);
- }
- void setMouseListener(MouseAdapter m) {
- board.addMouseListener(m);
- }
- public class MyPanel extends JPanel {
- private static final long serialVersionUID = 1L;
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- for (int i = 0; i < control.numSquares; i++) {
- for (int j = 0; j < control.numSquares; j++) {
- BufferedImage pic = control.getImage(j, i);
- if (pic == control.mine) {
- g.drawImage(pic, j * control.line, i * control.line, control.line, control.line, null);
- } else {
- g.drawImage(pic, j * control.line, i * control.line, control.line, control.line, null);
- }
- }
- }
- }
- }
- public class time extends JPanel {
- private static final long serialVersionUID = 1L;
- public void paintComponent(Graphics g) {
- g.setColor(window.getBackground());
- g.fillRect(0, 0, 100, 100);
- int seconds = control.sec;
- g.setFont(new Font("Serif", Font.BOLD, 20));
- g.setColor(Color.BLACK);
- if (seconds <= 999) {
- g.drawString(Integer.toString(seconds), 30, 35);
- } else {
- ;
- }
- }
- }
- public class MinesLeft extends JPanel {
- private static final long serialVersionUID = 1L;
- public void paintComponent(Graphics g) {
- g.setColor(window.getBackground());
- g.fillRect(0, 0, 100, 100);
- g.setFont(new Font("Serif", Font.BOLD, 20));
- g.setColor(Color.BLACK);
- g.drawString(Integer.toString(control.getMines()), 30, 35);
- }
- }
- }
Add Comment
Please, Sign In to add comment