Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 KB | None | 0 0
  1. //Name: CHANDLER CYR
  2. //Date:
  3. //Purpose: MATCHING GAME
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import javax.swing.*;
  8. import java.applet.Applet;
  9.  
  10. public class FinalGame extends Applet implements ActionListener
  11. {
  12. JProgressBar p;
  13. int i = 25;
  14. Panel p_card; //to hold all of the screens
  15. Panel card1, card2, card3, card4, card5; //the two screens
  16. CardLayout cdLayout = new CardLayout ();
  17.  
  18. //grid
  19. int row = 4;
  20. int col = 9;
  21. JButton a[] = new JButton [row * col];
  22. int b[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
  23. int click1 = -1;
  24. int click2 = -1;
  25. int matches = 0;
  26. JButton e[] = new JButton [row * col];
  27.  
  28. public void init ()
  29. {
  30. newGame ();
  31. p_card = new Panel ();
  32. p_card.setLayout (cdLayout);
  33. screen1 ();
  34. screen2 ();
  35. screen3 ();
  36. screen4 ();
  37. resize (1280, 680);
  38.  
  39. setLayout (new BorderLayout ());
  40. add ("Center", p_card);
  41.  
  42. JMenuBar menuBar = new JMenuBar ();
  43. JMenu menu;
  44. JMenuItem menuItem;
  45.  
  46. menu = new JMenu ("File");
  47. menuBar.add (menu);
  48. menuItem = new JMenuItem ("Close");
  49. menuItem.addActionListener (this);
  50. menuItem.setActionCommand ("Close");
  51. menu.add (menuItem);
  52.  
  53. menu = new JMenu ("Navigate");
  54. menuBar.add (menu);
  55. menuItem = new JMenuItem ("Starting Screen");
  56. menuItem.addActionListener (this);
  57. menuItem.setActionCommand ("s1");
  58. menu.add (menuItem);
  59. menuItem = new JMenuItem ("Instructions");
  60. menuItem.addActionListener (this);
  61. menuItem.setActionCommand ("s2");
  62. menu.add (menuItem);
  63. menuItem = new JMenuItem ("Play Game");
  64. menuItem.addActionListener (this);
  65. menuItem.setActionCommand ("s3");
  66. menu.add (menuItem);
  67. menuItem = new JMenuItem ("Ending");
  68. menuItem.addActionListener (this);
  69. menuItem.setActionCommand ("s4");
  70. menu.add (menuItem);
  71. add ("North", menuBar);
  72. }
  73.  
  74.  
  75.  
  76.  
  77. public void newGame ()
  78. {
  79. for (int i = 0 ; i < 80 ; i++)
  80. {
  81. int r1 = (int) (Math.random () * b.length);
  82. int r2 = (int) (Math.random () * b.length);
  83. int temp = b [r1];
  84. b [r1] = b [r2];
  85. b [r2] = temp;
  86. }
  87. }
  88.  
  89.  
  90.  
  91. public void screen1 ()
  92. {
  93. //screen 1 is set up.
  94. card1 = new Panel ();
  95. card1.setBackground (Color.white);
  96. JLabel title = new JLabel ("Santa's Matching Game");
  97. title.setForeground (Color.red);
  98. title.setFont (new Font ("ArialBlack", Font.PLAIN, 50));
  99. JButton next = new JButton ("Next");
  100. next.setActionCommand ("s2");
  101. next.addActionListener (this);
  102. JLabel Gif = new JLabel (createImageIcon ("santa-snowmen.gif"));
  103. card1.add (Gif);
  104. card1.add (title);
  105. card1.add (next);
  106. p_card.add ("1", card1);
  107.  
  108.  
  109. Panel a = new Panel ();
  110. a.add (title);
  111.  
  112. Panel b = new Panel ();
  113. b.add (Gif);
  114.  
  115. Panel c = new Panel ();
  116. c.add (next);
  117.  
  118.  
  119. Panel bord = new Panel (new BorderLayout ());
  120. bord.add ("North", a);
  121. bord.add ("Center", b);
  122. bord.add ("South", c);
  123. card1.add (bord);
  124.  
  125. p_card.add ("1", card1);
  126.  
  127. }
  128.  
  129.  
  130. public void reset ()
  131. {
  132. JButton reset = new JButton ("Reset");
  133. card3.add (reset);
  134. }
  135.  
  136.  
  137. public void screen2 ()
  138. {
  139. //screen 2 is set up.
  140. card2 = new Panel ();
  141. card2.setBackground (Color.white);
  142. JLabel title = new JLabel ("Instructions");
  143. title.setFont (new Font ("ArialBlack", Font.BOLD, 75));
  144. title.setForeground (Color.black);
  145. JLabel Wording1 = new JLabel ("\nHow To Play like a Pro!");
  146. Wording1.setFont (new Font ("ArialBlack", Font.PLAIN, 45));
  147. JTextArea Wording2 = new JTextArea ("\n\n Click on a card and remember what the card has on it, once remembered. Click on \n another card and check if that matches. If the card does not match, remember \n that card and try with another card until you remember where 2 matching \n cards are at and pair them.");
  148. Wording2.setFont (new Font ("ArialBlack", Font.BOLD, 20));
  149. JButton next = new JButton ("Next");
  150. JLabel pic = new JLabel (createImageIcon ("Read Instructions.jpg"));
  151. next.setActionCommand ("s3");
  152. next.addActionListener (this);
  153.  
  154.  
  155.  
  156. Panel a = new Panel (new GridLayout (2, 1));
  157. a.add (title);
  158. a.add (Wording1);
  159.  
  160. Panel b = new Panel (new GridLayout (2, 1));
  161. b.add (pic);
  162. b.add (Wording2);
  163.  
  164.  
  165. Panel bord = new Panel (new BorderLayout ());
  166. bord.add ("North", a);
  167. bord.add ("Center", b);
  168. card2.add (title);
  169. card2.add (Wording1);
  170. card2.add (pic);
  171. card2.add (Wording2);
  172. // card2.add (bord);
  173.  
  174. card2.add (next);
  175. p_card.add ("2", card2);
  176.  
  177. }
  178.  
  179.  
  180. public void screen3 ()
  181. { //screen 3 is set up.
  182. card3 = new Panel ();
  183. card3.setBackground (Color.red);
  184. Color red = Color.decode ("#FF0000");
  185. JLabel title = new JLabel ("Santa's Matching Game");
  186. title.setFont (new Font ("ArialBlack", Font.BOLD, 50));
  187. JButton next = new JButton ("Next");
  188. next.setActionCommand ("s4");
  189. next.addActionListener (this);
  190.  
  191. //Set up grid
  192. Panel p = new Panel (new GridLayout (row, col));
  193. int move = 0;
  194. for (int i = 0 ; i < row ; i++)
  195. {
  196. for (int j = 0 ; j < col ; j++)
  197. { //take out when you've got pictures
  198. //a [move] = new JButton ("");
  199. //add in when you have pictures
  200. a [move] = new JButton (createImageIcon ("back.jpg"));
  201. a [move].setPreferredSize (new Dimension (140, 140));
  202. a [move].addActionListener (this);
  203. a [move].setActionCommand ("" + move);
  204. p.add (a [move]);
  205. move++;
  206. }
  207. }
  208. card3.add (title);
  209. card3.add (p);
  210. card3.add (next);
  211. p_card.add ("3", card3);
  212. }
  213.  
  214.  
  215.  
  216.  
  217. public void screen4 ()
  218. { //screen 4 is set up.
  219. card4 = new Panel ();
  220. card4.setBackground (Color.yellow);
  221. JLabel title = new JLabel ("Winner!");
  222. JButton next = new JButton ("Next");
  223. JLabel Gif = new JLabel (createImageIcon ("giphy.gif"));
  224. title.setForeground (Color.red);
  225. title.setFont (new Font ("ArialBlack", Font.PLAIN, 50));
  226. next.setActionCommand ("s5");
  227. next.addActionListener (this);
  228. card4.add (Gif);
  229. card4.add (title);
  230. card4.add (next);
  231. p_card.add ("4", card4);
  232.  
  233. Panel a = new Panel ();
  234. a.add (title);
  235.  
  236. Panel b = new Panel ();
  237. b.add (Gif);
  238.  
  239. Panel c = new Panel ();
  240. c.add (next);
  241.  
  242.  
  243. Panel bord = new Panel (new BorderLayout ());
  244. bord.add ("North", a);
  245. bord.add ("Center", b);
  246. bord.add ("South", c);
  247. card4.add (bord);
  248.  
  249. p_card.add ("4", card4);
  250. }
  251.  
  252.  
  253.  
  254.  
  255. protected static ImageIcon createImageIcon (String path)
  256. { //change the red to your class name
  257. java.net.URL imgURL = FinalGame.class.getResource (path);
  258. if (imgURL != null)
  259. {
  260. return new ImageIcon (imgURL);
  261. }
  262. else
  263. {
  264. System.err.println ("Couldn't find file: " + path);
  265. return null;
  266. }
  267. }
  268.  
  269.  
  270. public void redraw ()
  271. {
  272. int move = 0;
  273. for (int i = 0 ; i < b.length ; i++)
  274. {
  275.  
  276. a [move].setIcon (createImageIcon (b [i] + ".jpg"));
  277. move++;
  278.  
  279. }
  280. }
  281.  
  282.  
  283. public void actionPerformed (ActionEvent e)
  284. { //moves between the screens
  285. if (e.getActionCommand ().equals ("s1"))
  286. cdLayout.show (p_card, "1");
  287. else if (e.getActionCommand ().equals ("s2"))
  288. cdLayout.show (p_card, "2");
  289. else if (e.getActionCommand ().equals ("s3"))
  290. cdLayout.show (p_card, "3");
  291. else if (e.getActionCommand ().equals ("s4"))
  292. cdLayout.show (p_card, "4");
  293. else if (e.getActionCommand ().equals ("s5"))
  294. cdLayout.show (p_card, "5");
  295. else if (e.getActionCommand ().equals ("Close"))
  296. System.exit (0);
  297. else
  298.  
  299. { //code to handle the game
  300. int n = Integer.parseInt (e.getActionCommand ());
  301. int x = n / col;
  302. int y = n % col;
  303. showStatus ("(" + x + ", " + y + ")");
  304. if (click1 == -1) //click1
  305. {
  306. a [n].setIcon (createImageIcon (b [n] + ".jpg"));
  307. click1 = n;
  308. }
  309. else if (click2 == -1)
  310. {
  311. a [n].setIcon (createImageIcon (b [n] + ".jpg"));
  312. click2 = n;
  313. }
  314. else
  315. {
  316. if (b [click1] != b [click2] || click1 == click2) //not match
  317. {
  318. a [click1].setIcon (createImageIcon ("back.jpg"));
  319. a [click2].setIcon (createImageIcon ("back.jpg"));
  320. }
  321. else
  322. { //match
  323. a [click1].setEnabled (false);
  324. a [click2].setEnabled (false);
  325. matches++;
  326. if (matches == 18)
  327. JOptionPane.showMessageDialog (null, "Test 1", "Test 2", JOptionPane.ERROR_MESSAGE);
  328. }
  329. a [n].setIcon (createImageIcon (b [n] + ".jpg"));
  330. click1 = n;
  331. click2 = -1;
  332. }
  333. }
  334. }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement