Advertisement
Stann

Untitled

May 21st, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.22 KB | None | 0 0
  1. package javaGame;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.GridLayout;
  6.  
  7. import javax.swing.JFrame;
  8.  
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import java.util.Random;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.Icon;
  16. import javax.swing.JLabel;
  17. import javax.swing.JPanel;
  18. import javax.swing.Timer;
  19.  
  20. public class CardButton extends JFrame {
  21.     Clicked handler = new Clicked();
  22.     JFrame game = new JFrame("Card Match Game");
  23.     JPanel playfield = new JPanel();
  24.     JPanel playerPanel = new JPanel();
  25.     JLabel displayText[] = new JLabel[2];
  26.     JButton restartGame = new JButton("Play Again");
  27.     JButton hold;
  28.     JButton held;
  29.     int player = 0;
  30.     int counter = 0;
  31.     Icon pic1;
  32.     Icon pic2;
  33.     int score = 0;
  34.  
  35.     public JButton buttons[] = new JButton[16];
  36.     public ImageIcon imagesicons[] = new ImageIcon[16];
  37.  
  38.     public CardButton() {
  39.  
  40.         playfield.setLayout(new GridLayout(4, 4, 4, 4));
  41.         playerPanel.setLayout(new GridLayout(1, 4));
  42.  
  43.         // generate the buttons
  44.         for (int i = 0; i < 16; i++) {
  45.             buttons[i] = new JButton();
  46.             buttons[i].setBackground(Color.GREEN);
  47.             buttons[i].addActionListener(handler);// add button to action
  48.                                                     // listener
  49.             playfield.add(buttons[i]);// add button to grid
  50.         }
  51.  
  52.         // generate text labels
  53.         for (int i = 0; i < 2; i++) {
  54.             displayText[i] = new JLabel();
  55.             playerPanel.add(displayText[i]);
  56.         }
  57.         displayText[0].setText("Player score:");
  58.         displayText[1].setText("0");
  59.         game.setSize(600, 620);
  60.         game.setResizable(true);
  61.         game.setVisible(true);
  62.         game.getContentPane().add(playerPanel, BorderLayout.SOUTH);
  63.         game.getContentPane().add(playfield, BorderLayout.CENTER);
  64.  
  65.         // this.getContentPane().add(playerPanel, BorderLayout.SOUTH);// need to
  66.         // fix
  67.         // this.getContentPane().add(go,BorderLayout.CENTER);
  68.  
  69.         // generate array with random images
  70.         int counter1 = 0;
  71.         int counter2 = 0;
  72.         int counter3 = 0;
  73.         int counter4 = 0;
  74.         int counter5 = 0;
  75.         int counter6 = 0;
  76.         int counter7 = 0;
  77.         int counter8 = 0;
  78.         for (int i = 0; i < 16; i++) {
  79.             Random rnd = new Random();
  80.             int num = 1 + rnd.nextInt(8);
  81.  
  82.             switch (num) {
  83.             case 1:
  84.                 if (counter1 < 2) {
  85.                     imagesicons[i] = new ImageIcon(getClass().getResource(
  86.                             (num) + ".png"));
  87.                     counter1++;
  88.                 }
  89.  
  90.                 else {
  91.                     i--;
  92.                     continue;
  93.                 }
  94.  
  95.                 break;
  96.             case 2:
  97.                 if (counter2 < 2) {
  98.                     imagesicons[i] = new ImageIcon(getClass().getResource(
  99.                             (num) + ".png"));
  100.                     counter2++;
  101.                 }
  102.  
  103.                 else {
  104.                     i--;
  105.                     continue;
  106.                 }
  107.  
  108.                 break;
  109.             case 3:
  110.                 if (counter3 < 2) {
  111.                     imagesicons[i] = new ImageIcon(getClass().getResource(
  112.                             (num) + ".png"));
  113.                     counter3++;
  114.                 }
  115.  
  116.                 else {
  117.                     i--;
  118.                     continue;
  119.                 }
  120.  
  121.                 break;
  122.             case 4:
  123.                 if (counter4 < 2) {
  124.                     imagesicons[i] = new ImageIcon(getClass().getResource(
  125.                             (num) + ".png"));
  126.                     counter4++;
  127.                 }
  128.  
  129.                 else {
  130.                     i--;
  131.                     continue;
  132.                 }
  133.  
  134.                 break;
  135.             case 5:
  136.                 if (counter5 < 2) {
  137.                     imagesicons[i] = new ImageIcon(getClass().getResource(
  138.                             (num) + ".png"));
  139.                     counter5++;
  140.                 }
  141.  
  142.                 else {
  143.                     i--;
  144.                     continue;
  145.                 }
  146.  
  147.                 break;
  148.             case 6:
  149.                 if (counter6 < 2) {
  150.                     imagesicons[i] = new ImageIcon(getClass().getResource(
  151.                             (num) + ".png"));
  152.                     counter6++;
  153.                 }
  154.  
  155.                 else {
  156.                     i--;
  157.                     continue;
  158.                 }
  159.  
  160.                 break;
  161.             case 7:
  162.                 if (counter7 < 2) {
  163.                     imagesicons[i] = new ImageIcon(getClass().getResource(
  164.                             (num) + ".png"));
  165.                     counter7++;
  166.                 }
  167.  
  168.                 else {
  169.                     i--;
  170.                     continue;
  171.                 }
  172.  
  173.                 break;
  174.             case 8:
  175.                 if (counter8 < 2) {
  176.                     imagesicons[i] = new ImageIcon(getClass().getResource(
  177.                             (num) + ".png"));
  178.                     counter8++;
  179.                 }
  180.  
  181.                 else {
  182.                     i--;
  183.                     continue;
  184.                 }
  185.  
  186.                 break;
  187.  
  188.             }
  189.  
  190.         }
  191.  
  192.         restartGame.addActionListener(new ActionListener() {
  193.  
  194.             public void actionPerformed(ActionEvent a) {
  195.                 score = 0;
  196.                 displayText[1].setText(score + "");
  197.                 // set all icons on buttons to null
  198.                 for (int i = 0; i < 16; i++) {
  199.  
  200.                     buttons[i].setIcon(null);
  201.                     imagesicons[i] = null;
  202.  
  203.                 }
  204.                 int counter1 = 0;
  205.                 int counter2 = 0;
  206.                 int counter3 = 0;
  207.                 int counter4 = 0;
  208.                 int counter5 = 0;
  209.                 int counter6 = 0;
  210.                 int counter7 = 0;
  211.                 int counter8 = 0;
  212.                 // generate again the array with random images
  213.                 for (int i = 0; i < 16; i++) {
  214.                     Random rnd = new Random();
  215.                     int num = 1 + rnd.nextInt(8);
  216.  
  217.                     switch (num) {
  218.                     case 1:
  219.                         if (counter1 < 2) {
  220.                             imagesicons[i] = new ImageIcon(getClass()
  221.                                     .getResource((num) + ".png"));
  222.                             counter1++;
  223.                         }
  224.  
  225.                         else {
  226.                             i--;
  227.                             continue;
  228.                         }
  229.  
  230.                         break;
  231.                     case 2:
  232.                         if (counter2 < 2) {
  233.                             imagesicons[i] = new ImageIcon(getClass()
  234.                                     .getResource((num) + ".png"));
  235.                             counter2++;
  236.                         }
  237.  
  238.                         else {
  239.                             i--;
  240.                             continue;
  241.                         }
  242.  
  243.                         break;
  244.                     case 3:
  245.                         if (counter3 < 2) {
  246.                             imagesicons[i] = new ImageIcon(getClass()
  247.                                     .getResource((num) + ".png"));
  248.                             counter3++;
  249.                         }
  250.  
  251.                         else {
  252.                             i--;
  253.                             continue;
  254.                         }
  255.  
  256.                         break;
  257.                     case 4:
  258.                         if (counter4 < 2) {
  259.                             imagesicons[i] = new ImageIcon(getClass()
  260.                                     .getResource((num) + ".png"));
  261.                             counter4++;
  262.                         }
  263.  
  264.                         else {
  265.                             i--;
  266.                             continue;
  267.                         }
  268.  
  269.                         break;
  270.                     case 5:
  271.                         if (counter5 < 2) {
  272.                             imagesicons[i] = new ImageIcon(getClass()
  273.                                     .getResource((num) + ".png"));
  274.                             counter5++;
  275.                         }
  276.  
  277.                         else {
  278.                             i--;
  279.                             continue;
  280.                         }
  281.  
  282.                         break;
  283.                     case 6:
  284.                         if (counter6 < 2) {
  285.                             imagesicons[i] = new ImageIcon(getClass()
  286.                                     .getResource((num) + ".png"));
  287.                             counter6++;
  288.                         }
  289.  
  290.                         else {
  291.                             i--;
  292.                             continue;
  293.                         }
  294.  
  295.                         break;
  296.                     case 7:
  297.                         if (counter7 < 2) {
  298.                             imagesicons[i] = new ImageIcon(getClass()
  299.                                     .getResource((num) + ".png"));
  300.                             counter7++;
  301.                         }
  302.  
  303.                         else {
  304.                             i--;
  305.                             continue;
  306.                         }
  307.  
  308.                         break;
  309.                     case 8:
  310.                         if (counter8 < 2) {
  311.                             imagesicons[i] = new ImageIcon(getClass()
  312.                                     .getResource((num) + ".png"));
  313.                             counter8++;
  314.                         }
  315.  
  316.                         else {
  317.                             i--;
  318.                             continue;
  319.                         }
  320.  
  321.                         break;
  322.  
  323.                     }
  324.  
  325.                 }
  326.  
  327.             }
  328.         });
  329.         restartGame.setBackground(Color.orange);
  330.  
  331.         playerPanel.add(restartGame);
  332.  
  333.     }
  334.  
  335.     // handling events
  336.     private class Clicked implements ActionListener {
  337.  
  338.         public void actionPerformed(ActionEvent e) {
  339.             int i;
  340.             hold = (JButton) e.getSource();
  341.             if (hold.getIcon() == null) {
  342.                 for (i = 0; buttons[i] != hold; i++) {
  343.  
  344.                 }
  345.                 if (counter == 0) {
  346.  
  347.                     hold.setIcon(imagesicons[i]);
  348.                     pic2 = imagesicons[i];
  349.                     counter++;
  350.                     held = hold;
  351.                 } else {
  352.  
  353.                     hold.setIcon(imagesicons[i]);
  354.                     pic1 = imagesicons[i];
  355.                     countScore(pic1, hold);
  356.                     counter = 0;
  357.  
  358.                 }
  359.  
  360.             }
  361.  
  362.         }
  363.  
  364.         // count score
  365.         private void countScore(Icon pic1, JButton hold) {
  366.  
  367.             if (pic1.toString().equals(pic2.toString())
  368.                     || pic1.toString().equals("8.png")
  369.                     || pic2.toString().equals("8.png")) {
  370.                 score += 10;
  371.                 displayText[1].setText(score + "");
  372.  
  373.             } else {
  374.                 // hide the two icons
  375.                 Timer t = new Timer(200, new ActionListener() {
  376.  
  377.                     public void actionPerformed(ActionEvent e) {
  378.                         hold.setIcon(null);
  379.  
  380.                         held.setIcon(null);
  381.  
  382.                     }
  383.                 });
  384.                 t.setRepeats(false);
  385.                 t.start();
  386.  
  387.             }
  388.         }
  389.     }
  390.  
  391.     public static void main(String[] args) throws Exception {
  392.         CardButton play = new CardButton();
  393.     }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement