Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: CHANDLER CYR
- //Date:
- //Purpose: MATCHING GAME
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.applet.Applet;
- public class FinalGame extends Applet implements ActionListener
- {
- JProgressBar p;
- int i = 25;
- Panel p_card; //to hold all of the screens
- Panel card1, card2, card3, card4, card5; //the two screens
- CardLayout cdLayout = new CardLayout ();
- //grid
- int row = 4;
- int col = 9;
- JButton a[] = new JButton [row * col];
- 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};
- int click1 = -1;
- int click2 = -1;
- JButton e[] = new JButton [row * col];
- public void init ()
- {
- newGame ();
- p_card = new Panel ();
- p_card.setLayout (cdLayout);
- screen1 ();
- screen2 ();
- screen3 ();
- screen4 ();
- resize (1280, 680);
- setLayout (new BorderLayout ());
- add ("Center", p_card);
- JMenuBar menuBar = new JMenuBar ();
- JMenu menu;
- JMenuItem menuItem;
- menu = new JMenu ("File");
- menuBar.add (menu);
- menuItem = new JMenuItem ("Close");
- menuItem.addActionListener (this);
- menuItem.setActionCommand ("Close");
- menu.add (menuItem);
- menu = new JMenu ("Navigate");
- menuBar.add (menu);
- menuItem = new JMenuItem ("Starting Screen");
- menuItem.addActionListener (this);
- menuItem.setActionCommand ("s1");
- menu.add (menuItem);
- menuItem = new JMenuItem ("Instructions");
- menuItem.addActionListener (this);
- menuItem.setActionCommand ("s2");
- menu.add (menuItem);
- menuItem = new JMenuItem ("Play Game");
- menuItem.addActionListener (this);
- menuItem.setActionCommand ("s3");
- menu.add (menuItem);
- menuItem = new JMenuItem ("Ending");
- menuItem.addActionListener (this);
- menuItem.setActionCommand ("s4");
- menu.add (menuItem);
- add ("North", menuBar);
- }
- public void newGame ()
- {
- for (int i = 0 ; i < 80 ; i++)
- {
- int r1 = (int) (Math.random () * b.length);
- int r2 = (int) (Math.random () * b.length);
- int temp = b [r1];
- b [r1] = b [r2];
- b [r2] = temp;
- }
- }
- public void screen1 ()
- {
- //screen 1 is set up.
- card1 = new Panel ();
- card1.setBackground (Color.white);
- JLabel title = new JLabel ("Santa's Matching Game");
- title.setForeground (Color.red);
- title.setFont (new Font ("ArialBlack", Font.PLAIN, 50));
- JButton next = new JButton ("Next");
- next.setActionCommand ("s2");
- next.addActionListener (this);
- JLabel Gif = new JLabel (createImageIcon ("santa-snowmen.gif"));
- card1.add (Gif);
- card1.add (title);
- card1.add (next);
- p_card.add ("1", card1);
- Panel a = new Panel ();
- a.add (title);
- Panel b = new Panel ();
- b.add (Gif);
- Panel c = new Panel ();
- c.add (next);
- Panel bord = new Panel (new BorderLayout ());
- bord.add ("North", a);
- bord.add ("Center", b);
- bord.add ("South", c);
- card1.add (bord);
- p_card.add ("1", card1);
- }
- public void reset ()
- {
- JButton reset = new JButton ("Reset");
- card3.add (reset);
- }
- public void screen2 ()
- {
- //screen 2 is set up.
- card2 = new Panel ();
- card2.setBackground (Color.white);
- JLabel title = new JLabel ("Instructions");
- title.setFont (new Font ("ArialBlack", Font.BOLD, 75));
- title.setForeground (Color.black);
- JLabel Wording1 = new JLabel ("\nHow To Play like a Pro!");
- Wording1.setFont (new Font ("ArialBlack", Font.PLAIN, 45));
- 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.");
- Wording2.setFont (new Font ("ArialBlack", Font.BOLD, 20));
- JButton next = new JButton ("Next");
- JLabel pic = new JLabel (createImageIcon ("Read Instructions.jpg"));
- next.setActionCommand ("s3");
- next.addActionListener (this);
- Panel a = new Panel (new GridLayout (2, 1));
- a.add (title);
- a.add (Wording1);
- Panel b = new Panel (new GridLayout (2, 1));
- b.add (pic);
- b.add (Wording2);
- Panel bord = new Panel (new BorderLayout ());
- bord.add ("North", a);
- bord.add ("Center", b);
- card2.add (title);
- card2.add (Wording1);
- card2.add (pic);
- card2.add (Wording2);
- // card2.add (bord);
- card2.add (next);
- p_card.add ("2", card2);
- }
- public void screen3 ()
- { //screen 3 is set up.
- card3 = new Panel ();
- card3.setBackground (Color.red);
- Color red = Color.decode ("#FF0000");
- JLabel title = new JLabel ("Santa's Matching Game");
- title.setFont (new Font ("ArialBlack", Font.BOLD, 50));
- JButton next = new JButton ("Next");
- next.setActionCommand ("s4");
- next.addActionListener (this);
- //Set up grid
- Panel p = new Panel (new GridLayout (row, col));
- int move = 0;
- for (int i = 0 ; i < row ; i++)
- {
- for (int j = 0 ; j < col ; j++)
- { //take out when you've got pictures
- //a [move] = new JButton ("");
- //add in when you have pictures
- a [move] = new JButton (createImageIcon ("back.jpg"));
- a [move].setPreferredSize (new Dimension (140, 140));
- a [move].addActionListener (this);
- a [move].setActionCommand ("" + move);
- p.add (a [move]);
- move++;
- }
- }
- card3.add (title);
- card3.add (p);
- card3.add (next);
- p_card.add ("3", card3);
- }
- public void screen4 ()
- { //screen 4 is set up.
- card4 = new Panel ();
- card4.setBackground (Color.yellow);
- JLabel title = new JLabel ("Winner!");
- JButton next = new JButton ("Next");
- JLabel Gif = new JLabel (createImageIcon ("giphy.gif"));
- title.setForeground (Color.red);
- title.setFont (new Font ("ArialBlack", Font.PLAIN, 50));
- next.setActionCommand ("s5");
- next.addActionListener (this);
- card4.add (Gif);
- card4.add (title);
- card4.add (next);
- p_card.add ("4", card4);
- Panel a = new Panel ();
- a.add (title);
- Panel b = new Panel ();
- b.add (Gif);
- Panel c = new Panel ();
- c.add (next);
- Panel bord = new Panel (new BorderLayout ());
- bord.add ("North", a);
- bord.add ("Center", b);
- bord.add ("South", c);
- card4.add (bord);
- p_card.add ("4", card4);
- }
- protected static ImageIcon createImageIcon (String path)
- { //change the red to your class name
- java.net.URL imgURL = FinalGame.class.getResource (path);
- if (imgURL != null)
- {
- return new ImageIcon (imgURL);
- }
- else
- {
- System.err.println ("Couldn't find file: " + path);
- return null;
- }
- }
- public void redraw ()
- {
- int move = 0;
- for (int i = 0 ; i < b.length ; i++)
- {
- a [move].setIcon (createImageIcon (b [i] + ".jpg"));
- move++;
- }
- }
- public void actionPerformed (ActionEvent e)
- { //moves between the screens
- if (e.getActionCommand ().equals ("s1"))
- cdLayout.show (p_card, "1");
- else if (e.getActionCommand ().equals ("s2"))
- cdLayout.show (p_card, "2");
- else if (e.getActionCommand ().equals ("s3"))
- cdLayout.show (p_card, "3");
- else if (e.getActionCommand ().equals ("s4"))
- cdLayout.show (p_card, "4");
- else if (e.getActionCommand ().equals ("s5"))
- cdLayout.show (p_card, "5");
- else if (e.getActionCommand ().equals ("Close"))
- System.exit (0);
- else
- { //code to handle the game
- int n = Integer.parseInt (e.getActionCommand ());
- int x = n / col;
- int y = n % col;
- showStatus ("(" + x + ", " + y + ")");
- if (click1 == -1) //click1
- {
- a [n].setIcon (createImageIcon (b [n] + ".jpg"));
- click1 = n;
- }
- else if (click2 == -1)
- {
- a [n].setIcon (createImageIcon (b [n] + ".jpg"));
- click2 = n;
- }
- else
- {
- if (b [click1] != b [click2] || click1 == click2) //not match
- {
- a [click1].setIcon (createImageIcon ("back.jpg"));
- a [click2].setIcon (createImageIcon ("back.jpg"));
- }
- else
- { //match
- a [click1].setEnabled (false);
- a [click2].setEnabled (false);
- }
- a [n].setIcon (createImageIcon (b [n] + ".jpg"));
- click1 = n;
- click2 = -1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement