Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. import java.io.*;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6.  
  7. public class Memory extends JFrame{
  8.  
  9. //test av buttons
  10.  
  11. public static void main(String [] args) {
  12. new Memory();
  13.  
  14. }
  15.  
  16.  
  17.  
  18. private JPanel p;
  19. private JButton b1;
  20. private JLabel lab;
  21.  
  22.  
  23.  
  24. /*public void gui () {
  25. f = new JFrame ("Creativity tuts");
  26. f.
  27. f.setSize(600,400);
  28. f.
  29.  
  30. p = new JPanel();
  31. p.setBackground(Color.GRAY);
  32.  
  33. b1 = new JButton("Test");
  34. lab = new JLabel ("This is a test label");
  35.  
  36. p.add(b1);
  37. p.add(lab);
  38.  
  39. f.add(p);
  40. }
  41. */
  42.  
  43. File folder= new File("CardImages");
  44. File[] pictures = folder.listFiles();
  45. Card [] cards= new Card [pictures.length];
  46.  
  47. public Memory() {
  48. String [] possibilities = {"2","4","6"};
  49. String s = (String)JOptionPane.showInputDialog(
  50. this,
  51. "Välj antal kort",
  52. "Memory",
  53. JOptionPane.PLAIN_MESSAGE,
  54. new ImageIcon("cat1.jpg"),
  55. possibilities,
  56. possibilities[0]);
  57. int n = Integer.parseInt(s);
  58. System.out.println(n);
  59. //skapa cards
  60.  
  61. //läs in alla filer i mappen
  62.  
  63. //för varje fil ...
  64. // Skapa ett kort utifrån filen
  65. // Lägg kortet i cards
  66.  
  67. //
  68. for(int i=0; i<pictures.length; i++) {
  69. ImageIcon im= new ImageIcon(this.pictures[i].getPath());
  70. Card card= new Card(im, Card.Status.VISIBLE);
  71. cards [i]= card;
  72.  
  73.  
  74. }
  75.  
  76. //JFrame del
  77.  
  78. setSize(800,400);
  79. setVisible(true);
  80. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  81. setLayout(new BorderLayout());
  82.  
  83. /*JLabel ett = new JLabel ("Ett");
  84. JLabel två = new JLabel ("Två");
  85. JLabel tre = new JLabel ("Tre");
  86. JLabel fyra = new JLabel ("Fyra");
  87. JLabel fem = new JLabel ("Fem");
  88. JLabel sex = new JLabel ("Sex");
  89. JLabel sju = new JLabel ("Sju");
  90. JLabel åtta = new JLabel ("Åtta");
  91. JLabel nio = new JLabel ("Nio");
  92.  
  93. JLabel spelare1 = new JLabel ("Spelare 1");
  94. JLabel spelare2 = new JLabel ("Spelare 2");*/
  95.  
  96. JButton nyttspel = new JButton ("nytt spel");
  97. JButton avsluta = new JButton ("avsluta");
  98. JButton Spelare1 = new JButton ("Spelare 1");
  99. JButton Spelare2 = new JButton ("Spelare 2");
  100.  
  101. JPanel spelare = new JPanel (new GridLayout (2,1));
  102. JPanel spelbord = new JPanel(new GridLayout(3,6));
  103. JPanel options = new JPanel (new GridLayout(1,2));
  104.  
  105. add(spelare, BorderLayout.LINE_START);
  106. spelare.add(Spelare1);
  107. spelare.add(Spelare2);
  108.  
  109.  
  110.  
  111. //Card [] a = new newGame(int n);
  112. //cards[0].addActionlistener(new Action());
  113.  
  114.  
  115. add(spelbord, BorderLayout.CENTER);
  116. /* spelbord.add(cards[0]);
  117. spelbord.add(cards[1]);
  118. spelbord.add(cards[2]);
  119. spelbord.add(cards[3]);
  120. spelbord.add(cards[4]);
  121. spelbord.add(cards[5]);
  122. spelbord.add(cards[6]);
  123. spelbord.add(cards[7]);
  124. spelbord.add(cards[8]);
  125. spelbord.add(cards[9]);
  126. spelbord.add(cards[10]);
  127. spelbord.add(cards[11]);
  128. spelbord.add(cards[12]);
  129. spelbord.add(cards[13]);
  130. spelbord.add(cards[14]);
  131. spelbord.add(cards[15]);
  132. spelbord.add(cards[16]);
  133. spelbord.add(cards[17]);*/
  134. NewGame(n);
  135.  
  136.  
  137.  
  138.  
  139. add(options, BorderLayout.PAGE_END);
  140. options.add(nyttspel);
  141. options.add(avsluta);
  142. }
  143.  
  144.  
  145.  
  146.  
  147. public void NewGame(int n) {
  148. Card [] gameCards= new Card [n];
  149. Card [] currentGame= cards.clone();
  150.  
  151. for(int i=0;i<(n/2);i++) {
  152. Card pickCard= currentGame[i];
  153. gameCards[i]=pickCard;
  154. }
  155.  
  156. for(int i=0; i<n/2;i++) {
  157. Card pickCard= currentGame[i];
  158. gameCards[(n/2)+i]=pickCard;}
  159.  
  160.  
  161. Tools.randomOrder(gameCards);
  162.  
  163. for(int i=0; i<n;i++) {
  164. spelbord.add(gameCards[i]);
  165. }
  166.  
  167.  
  168. }
  169.  
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement