Guest User

Untitled

a guest
Jun 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. import static javax.swing.JOptionPane.*;
  5. import java.util.*;
  6. import java.io.*;
  7. import javax.swing.border.*;
  8.  
  9. public class Memory extends JFrame implements ActionListener {
  10.  
  11. //Klassvariabler
  12.  
  13. private int b;
  14. private int a;
  15. int spelare;
  16.  
  17. //Skapa fält
  18.  
  19. Kort[] allaKort;
  20. Kort[] aktivaKort = new Kort[a*b];
  21. Kort[] uppvandaKort;
  22.  
  23.  
  24.  
  25. //Skapa Paneler
  26.  
  27. private JPanel spelplan = new JPanel();
  28. private JPanel spelareplats = new JPanel();
  29.  
  30. //Menyraden variabler
  31.  
  32. private JMenuBar menyalternativ = new JMenuBar();
  33. private JMenu alt = new JMenu ("Alternativ");
  34. private JMenu inst = new JMenu ("Inställningar");
  35. private JMenuItem nyttsp = new JMenuItem ("Nytt spel");
  36. private JMenuItem avs = new JMenuItem ("Avsluta");
  37. private JMenu antsp = new JMenu ("Antalet spelare");
  38. private JMenuItem antsp1 = new JMenuItem ("1");
  39. private JMenuItem antsp2 = new JMenuItem ("2");
  40. private JMenuItem antsp3 = new JMenuItem ("3");
  41. private JMenuItem antsp4 = new JMenuItem ("4");
  42. private JMenu storl = new JMenu ("Spelplansstorlek");
  43. private JMenuItem storl1 = new JMenuItem ("3x4");
  44. private JMenuItem storl2 = new JMenuItem ("4x4");
  45. private JMenuItem storl3 = new JMenuItem ("4x6");
  46. private JMenuItem storl4 = new JMenuItem ("6x6");
  47.  
  48.  
  49.  
  50. public Memory(){
  51.  
  52. //Sätta startvariabler
  53.  
  54. spelare = Integer.parseInt(showInputDialog("Hur många spelare?"));
  55. String[] a0 = {"3*4", "4*4", "4*6", "6*6"};
  56. int svar = JOptionPane.showOptionDialog( null ,"Hur stor spelplan vill du ha?", "Startup",
  57. JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, a0, a0[1]);
  58.  
  59.  
  60.  
  61. if(svar == 0){
  62. a = 3;
  63. b = 4;
  64. }
  65. else if(svar == 1){
  66. a = 4;
  67. b = 4;
  68. }
  69. else if(svar == 2){
  70. a = 4;
  71. b = 6;
  72. }
  73. else if(svar == 3){
  74. a = 6;
  75. b = 6;
  76. }
  77.  
  78. //Skapa knappar/kort
  79.  
  80.  
  81. Kort[] allaKort;
  82. Kort[] aktivaKort = new Kort[a*b];
  83. Kort[] uppvandaKort;
  84.  
  85.  
  86.  
  87. //Lägger till menyer och undermenyer
  88.  
  89. setJMenuBar(menyalternativ);
  90. menyalternativ.add(alt); menyalternativ.add(inst);
  91. alt.add(nyttsp); alt.add(avs);
  92. inst.add(antsp);
  93. antsp.add(antsp1); antsp.add(antsp2); antsp.add(antsp3); antsp.add(antsp4); //Antalet spelare-meny
  94. inst.add(storl);
  95. storl.add(storl1); storl.add(storl2); storl.add(storl3); storl.add(storl4);
  96.  
  97. //Koppla actionlistener till menyknapparna
  98.  
  99. nyttsp.addActionListener(this);
  100. avs.addActionListener(this);
  101. antsp1.addActionListener(this);
  102. antsp2.addActionListener(this);
  103. antsp3.addActionListener(this);
  104. antsp4.addActionListener(this);
  105. storl1.addActionListener(this);
  106. storl2.addActionListener(this);
  107. storl3.addActionListener(this);
  108. storl4.addActionListener(this);
  109.  
  110. //Nyttspel, spelsetup
  111.  
  112. nyttSpel();
  113.  
  114. pack();
  115.  
  116. setVisible(true);
  117. setDefaultCloseOperation(EXIT_ON_CLOSE);
  118.  
  119. }
  120.  
  121. public void nyttSpel(){
  122.  
  123. //Importerar bilder
  124.  
  125. File bildmapp = new File("bildmapp");
  126. File[] bilder = bildmapp.listFiles();
  127. allaKort = new Kort[bilder.length];
  128. for(int j = 0 ; j < allaKort.length ; j++){
  129. allaKort[j] = new Kort(new ImageIcon(bilder[j].getPath()));
  130. }
  131.  
  132. Verktyg.slumpOrdning(allaKort);
  133.  
  134. for(int k = 0 ; k < ((a*b)/2) ; k++){
  135. aktivaKort[k] = allaKort[k].copy();
  136. aktivaKort[k+((a*b)/2)] = allaKort[k].copy();
  137.  
  138. }
  139.  
  140. Verktyg.slumpOrdning(aktivaKort);
  141.  
  142. //Lägger ut stommen för programmet
  143.  
  144. for(int s = 0; s < (a*b) ; s++){ //Lägger till korten till spelplanen
  145. spelplan.add(aktivaKort[s]);
  146. }
  147. spelplan.setLayout(new GridLayout(a,b)); //Sätter spelplanen till ett rutnät
  148.  
  149. setLayout(new BorderLayout()); //Sätter ut ramen för programmet och
  150. add(spelplan, BorderLayout.CENTER); //lägger till de 2 huvudkomponenterna
  151. add(spelareplats, BorderLayout.WEST);
  152.  
  153. setSize(150*a,150*b); //Anpassar spelplansstorleken
  154.  
  155. setVisible(true);
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. //Nadine, spelare
  163.  
  164. /* JLabel[] u = new JLabel[spelare];
  165. for (int i=0; i<spelare; i++) {
  166. u[i] = new JLabel("Spelare"+(i+1));
  167. u[i].setHorizontalAlignment(JLabel.CENTER);
  168. u[i].setPreferredSize(new Dimension(100,100));
  169. u[i].setBorder(new LineBorder(Color.RED));
  170. spelareplats.add(u[i]);
  171. }
  172. */
  173.  
  174.  
  175. //Startar om spelet och anpassar storlek efter inställningar
  176.  
  177.  
  178.  
  179. //lyssnametoder
  180.  
  181. public void actionPerformed(ActionEvent e){
  182. if (e.getSource() == nyttsp){
  183. nyttSpel();
  184.  
  185. }
  186. else if(e.getSource() == avs){
  187. System.exit(0);
  188. }
  189. else if(e.getSource() == antsp1){
  190. spelare = 1;
  191. nyttSpel();
  192. }
  193. else if(e.getSource() == antsp2){
  194. spelare = 2;
  195. nyttSpel();
  196. }
  197.  
  198. else if(e.getSource() == antsp3){
  199. spelare = 3;
  200. nyttSpel();
  201. }
  202. else if(e.getSource() == antsp4){
  203. spelare = 4;
  204. nyttSpel();
  205. }
  206. else if(e.getSource() == storl1){
  207. a= 3; b= 4;
  208. nyttSpel();
  209. }
  210. else if(e.getSource() == storl2){
  211. a= 4; b= 4;
  212. nyttSpel();
  213. }
  214. else if(e.getSource() == storl3){
  215. a= 4; b= 6;
  216. nyttSpel();
  217. }
  218. else if(e.getSource() == storl4){
  219. a= 6; b= 6;
  220. nyttSpel();
  221. }
  222. }
  223.  
  224. public static void main(String [] arg){
  225. new Memory();
  226. }
  227.  
  228. }
Add Comment
Please, Sign In to add comment