Advertisement
Mixies

Find the Burger game [Java]

May 19th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. package FTTG;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. import java.util.Random;
  7.  
  8. //@Cakemoth [20171705]
  9. // Class [4]
  10. // EXMP - [2] :BURGER:
  11.  
  12. public class FTTC extends JFrame{
  13.  
  14. //Class Declarations
  15.  
  16. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  17. JLabel label0 = new JLabel();
  18.  
  19. JLabel label1 = new JLabel();
  20.  
  21. JLabel label2 = new JLabel();
  22.  
  23. static JLabel[] choiceLabel = new JLabel[3];
  24.  
  25. static ImageIcon burger = new ImageIcon("burger.gif");
  26.  
  27. static int burgerLocation;
  28.  
  29. static Random myRandom = new Random();
  30. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  31. JFrame FindBurger = new JFrame();
  32. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  33. JButton newButton = new JButton();
  34. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  35.  
  36. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  37. static boolean isHot = false;
  38. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  39. public static void main(String args[])
  40. {
  41. new FTTC();
  42.  
  43. }
  44. //- - - - -Hamburger Finder- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  45. public FTTC()
  46. {
  47. setTitle("Trouver l'hamburger");
  48. setVisible(true);
  49. setResizable(false);
  50. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. addWindowListener(new WindowAdapter()
  52. {
  53. public void windowClosing(WindowEvent evt)
  54. {
  55. exitForm(evt);
  56. }
  57. });
  58. getContentPane().setLayout(new GridBagLayout());
  59.  
  60. //Application Controls
  61. getContentPane().setLayout(new GridBagLayout());
  62. GridBagConstraints gridCons;
  63. gridCons = new GridBagConstraints();
  64. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  65. GridBagConstraints gridConstraints;
  66. choiceLabel[0] = label0;
  67. choiceLabel[1] = label1;
  68. choiceLabel[2] = label2;
  69. for (int i = 0; i < 3; i++)
  70. {
  71. gridConstraints = new GridBagConstraints();
  72. choiceLabel[i].setPreferredSize(new
  73. Dimension(burger.getIconWidth(), burger.getIconHeight()));
  74. choiceLabel[i].setOpaque(true);
  75. choiceLabel[i].setBackground(Color.RED);
  76. gridConstraints.gridx = i;
  77. gridConstraints.gridy = 0;
  78. gridConstraints.insets = new Insets(10, 10, 10, 10);
  79. getContentPane().add(choiceLabel[i], gridConstraints);
  80. choiceLabel[i].addMouseListener(new MouseAdapter()
  81. {
  82. public void mouseClicked(MouseEvent e)
  83. {
  84. labelMouseClicked(e);
  85. }
  86. });
  87. }
  88.  
  89.  
  90. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  91. newButton.setText("Play Again");
  92. gridConstraints = new GridBagConstraints();
  93. gridConstraints.gridx = 1;
  94. gridConstraints.gridy = 1;
  95. gridConstraints.insets = new Insets(10, 10, 10, 10);
  96. getContentPane().add(newButton, gridConstraints);
  97. newButton.addActionListener(new ActionListener()
  98. {
  99. public void actionPerformed(ActionEvent e)
  100. {
  101. newButtonActionPerformed(e);
  102. }
  103. });
  104. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  105. newButton.doClick();
  106. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  107. pack();
  108.  
  109. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  110. setBounds((int) (0.5 *(screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight());
  111. }
  112. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  113. private void exitForm(WindowEvent evt)
  114. {
  115. System.exit(0);
  116. }
  117.  
  118. private void labelMouseClicked(MouseEvent e)
  119. {
  120. Component clickedComponent = e.getComponent();
  121. int choice;
  122. for (choice = 0; choice < 3; choice++)
  123. {
  124. if (clickedComponent == choiceLabel[choice])
  125. {
  126. break;
  127. }
  128. }
  129. choiceLabel[choice].setBackground(Color.WHITE);
  130. if (choice == burgerLocation)
  131. {
  132. choiceLabel[choice].setIcon(burger);
  133. newButton.setEnabled(true);
  134. }
  135. }
  136.  
  137. private void newButtonActionPerformed(ActionEvent e)
  138. {
  139. // clear boxes and hide burger
  140. for (int i = 0; i < 3; i++)
  141. {
  142. choiceLabel[i].setIcon(null);
  143. choiceLabel[i].setBackground(Color.RED);
  144. }
  145. burgerLocation = myRandom.nextInt(3);
  146. newButton.setEnabled(false);
  147.  
  148. }
  149. //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement