Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Memory extends JFrame{
- //test av buttons
- public static void main(String [] args) {
- new Memory();
- }
- private JPanel p;
- private JButton b1;
- private JLabel lab;
- /*public void gui () {
- f = new JFrame ("Creativity tuts");
- f.
- f.setSize(600,400);
- f.
- p = new JPanel();
- p.setBackground(Color.GRAY);
- b1 = new JButton("Test");
- lab = new JLabel ("This is a test label");
- p.add(b1);
- p.add(lab);
- f.add(p);
- }
- */
- File folder= new File("CardImages");
- File[] pictures = folder.listFiles();
- Card [] cards= new Card [pictures.length];
- public Memory() {
- String [] possibilities = {"2","4","6"};
- String s = (String)JOptionPane.showInputDialog(
- this,
- "Välj antal kort",
- "Memory",
- JOptionPane.PLAIN_MESSAGE,
- new ImageIcon("cat1.jpg"),
- possibilities,
- possibilities[0]);
- int n = Integer.parseInt(s);
- System.out.println(n);
- //skapa cards
- //läs in alla filer i mappen
- //för varje fil ...
- // Skapa ett kort utifrån filen
- // Lägg kortet i cards
- //
- for(int i=0; i<pictures.length; i++) {
- ImageIcon im= new ImageIcon(this.pictures[i].getPath());
- Card card= new Card(im, Card.Status.VISIBLE);
- cards [i]= card;
- }
- //JFrame del
- setSize(800,400);
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new BorderLayout());
- /*JLabel ett = new JLabel ("Ett");
- JLabel två = new JLabel ("Två");
- JLabel tre = new JLabel ("Tre");
- JLabel fyra = new JLabel ("Fyra");
- JLabel fem = new JLabel ("Fem");
- JLabel sex = new JLabel ("Sex");
- JLabel sju = new JLabel ("Sju");
- JLabel åtta = new JLabel ("Åtta");
- JLabel nio = new JLabel ("Nio");
- JLabel spelare1 = new JLabel ("Spelare 1");
- JLabel spelare2 = new JLabel ("Spelare 2");*/
- JButton nyttspel = new JButton ("nytt spel");
- JButton avsluta = new JButton ("avsluta");
- JButton Spelare1 = new JButton ("Spelare 1");
- JButton Spelare2 = new JButton ("Spelare 2");
- JPanel spelare = new JPanel (new GridLayout (2,1));
- JPanel spelbord = new JPanel(new GridLayout(3,6));
- JPanel options = new JPanel (new GridLayout(1,2));
- add(spelare, BorderLayout.LINE_START);
- spelare.add(Spelare1);
- spelare.add(Spelare2);
- //Card [] a = new newGame(int n);
- //cards[0].addActionlistener(new Action());
- add(spelbord, BorderLayout.CENTER);
- /* spelbord.add(cards[0]);
- spelbord.add(cards[1]);
- spelbord.add(cards[2]);
- spelbord.add(cards[3]);
- spelbord.add(cards[4]);
- spelbord.add(cards[5]);
- spelbord.add(cards[6]);
- spelbord.add(cards[7]);
- spelbord.add(cards[8]);
- spelbord.add(cards[9]);
- spelbord.add(cards[10]);
- spelbord.add(cards[11]);
- spelbord.add(cards[12]);
- spelbord.add(cards[13]);
- spelbord.add(cards[14]);
- spelbord.add(cards[15]);
- spelbord.add(cards[16]);
- spelbord.add(cards[17]);*/
- NewGame(n);
- add(options, BorderLayout.PAGE_END);
- options.add(nyttspel);
- options.add(avsluta);
- }
- public void NewGame(int n) {
- Card [] gameCards= new Card [n];
- Card [] currentGame= cards.clone();
- for(int i=0;i<(n/2);i++) {
- Card pickCard= currentGame[i];
- gameCards[i]=pickCard;
- }
- for(int i=0; i<n/2;i++) {
- Card pickCard= currentGame[i];
- gameCards[(n/2)+i]=pickCard;}
- Tools.randomOrder(gameCards);
- for(int i=0; i<n;i++) {
- spelbord.add(gameCards[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement