nubenugget

Untitled

Mar 20th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.event.MouseAdapter;
  5. import java.awt.image.BufferedImage;
  6. import java.net.URL;
  7.  
  8. import javax.swing.JEditorPane;
  9. import javax.swing.JFrame;
  10. import javax.swing.JMenu;
  11. import javax.swing.JMenuBar;
  12. import javax.swing.JMenuItem;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.border.TitledBorder;
  17.  
  18. public class P4_Namiranian_Pedram_Minesweeper_GUIView {
  19. P4_Namiranian_Pedram_Minesweeper_Controller control;
  20. MyPanel board;
  21. JFrame window;
  22. time elapsed;
  23. MinesLeft mines;
  24.  
  25. public P4_Namiranian_Pedram_Minesweeper_GUIView(P4_Namiranian_Pedram_Minesweeper_Controller c) {
  26. control = c;
  27.  
  28. // WINDER
  29. window = new JFrame("P4_Namiranian_Pedram_Minesweeper");
  30. window.setBounds(100, 20, 445, 550);
  31. window.setResizable(false);
  32. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. window.setLayout(null);
  34.  
  35. // MENU
  36. JMenuBar bar = new JMenuBar();
  37. bar.setVisible(true);
  38. JMenu game = new JMenu("Game");
  39. JMenu option = new JMenu("Option");
  40. JMenu help = new JMenu("Help");
  41. JMenuItem newGame = new JMenuItem("New Game");
  42. newGame.addActionListener(e -> {
  43. control.resetGame();
  44. });
  45. JMenuItem exit = new JMenuItem("Exit");
  46. exit.addActionListener(e -> {
  47. window.dispose();
  48. });
  49. JMenuItem setNumMines = new JMenuItem("Set Number Of Mines");
  50. setNumMines.addActionListener(e -> {
  51. int mines = Integer.parseInt(JOptionPane.showInputDialog("How many Mines?"));
  52. control.resetMines(mines);
  53. });
  54. JMenuItem howTo = new JMenuItem("How To Play");
  55. howTo.addActionListener(e -> {
  56. try {
  57. JEditorPane pane = new JEditorPane(new URL("file:how.html"));
  58. JScrollPane scroll = new JScrollPane(pane);
  59. JOptionPane.showMessageDialog(null, scroll, "How To Play", JOptionPane.PLAIN_MESSAGE, null);
  60. } catch (Exception e1) {
  61. e1.printStackTrace();
  62. }
  63. });
  64. JMenuItem about = new JMenuItem("About");
  65. about.addActionListener(e -> {
  66. try {
  67. JEditorPane pane = new JEditorPane(new URL("file:about.html"));
  68. JScrollPane scroll = new JScrollPane(pane);
  69. JOptionPane.showMessageDialog(null, scroll, "About", JOptionPane.PLAIN_MESSAGE, null);
  70. } catch (Exception e1) {
  71. e1.printStackTrace();
  72. }
  73. });
  74. game.add(newGame);
  75. game.add(exit);
  76. option.add(setNumMines);
  77. help.add(howTo);
  78. help.add(about);
  79. bar.add(game);
  80. bar.add(option);
  81. bar.add(help);
  82.  
  83. // JPANEL
  84. board = new MyPanel();
  85. board.setBounds(20, 20, 400, 400);
  86. board.repaint();
  87.  
  88. // TIME ELAPSED
  89. elapsed = new time();
  90. elapsed.setBorder(new TitledBorder("Time Elapsed"));
  91. elapsed.setBounds(100, 430, 100, 50);
  92.  
  93. // NUMBER OF MINES
  94. mines = new MinesLeft();
  95. mines.setBorder(new TitledBorder("Mines"));
  96. mines.setBounds(230, 430, 100, 50);
  97.  
  98. // LAST THINGS
  99. window.setJMenuBar(bar);
  100. window.getContentPane().add(board);
  101. window.getContentPane().add(elapsed);
  102. window.getContentPane().add(mines);
  103. window.setVisible(true);
  104. }
  105.  
  106. void setMouseListener(MouseAdapter m) {
  107. board.addMouseListener(m);
  108. }
  109.  
  110. public class MyPanel extends JPanel {
  111.  
  112. private static final long serialVersionUID = 1L;
  113.  
  114. public void paintComponent(Graphics g) {
  115. super.paintComponent(g);
  116. for (int i = 0; i < control.numSquares; i++) {
  117. for (int j = 0; j < control.numSquares; j++) {
  118. BufferedImage pic = control.getImage(j, i);
  119. if (pic == control.mine) {
  120. g.drawImage(pic, j * control.line, i * control.line, control.line, control.line, null);
  121. } else {
  122. g.drawImage(pic, j * control.line, i * control.line, control.line, control.line, null);
  123. }
  124. }
  125. }
  126. }
  127.  
  128. }
  129.  
  130. public class time extends JPanel {
  131. private static final long serialVersionUID = 1L;
  132.  
  133. public void paintComponent(Graphics g) {
  134. g.setColor(window.getBackground());
  135. g.fillRect(0, 0, 100, 100);
  136. int seconds = control.sec;
  137. g.setFont(new Font("Serif", Font.BOLD, 20));
  138. g.setColor(Color.BLACK);
  139. if (seconds <= 999) {
  140. g.drawString(Integer.toString(seconds), 30, 35);
  141. } else {
  142. ;
  143. }
  144. }
  145. }
  146.  
  147. public class MinesLeft extends JPanel {
  148. private static final long serialVersionUID = 1L;
  149.  
  150. public void paintComponent(Graphics g) {
  151. g.setColor(window.getBackground());
  152. g.fillRect(0, 0, 100, 100);
  153. g.setFont(new Font("Serif", Font.BOLD, 20));
  154. g.setColor(Color.BLACK);
  155. g.drawString(Integer.toString(control.getMines()), 30, 35);
  156. }
  157. }
  158. }
Add Comment
Please, Sign In to add comment