Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- import static javax.swing.JOptionPane.*;
- import java.util.*;
- import java.io.*;
- import javax.swing.border.*;
- public class Memory extends JFrame implements ActionListener {
- //Klassvariabler
- private int b;
- private int a;
- int spelare;
- //Skapa fält
- Kort[] allaKort;
- Kort[] aktivaKort = new Kort[a*b];
- Kort[] uppvandaKort;
- //Skapa Paneler
- private JPanel spelplan = new JPanel();
- private JPanel spelareplats = new JPanel();
- //Menyraden variabler
- private JMenuBar menyalternativ = new JMenuBar();
- private JMenu alt = new JMenu ("Alternativ");
- private JMenu inst = new JMenu ("Inställningar");
- private JMenuItem nyttsp = new JMenuItem ("Nytt spel");
- private JMenuItem avs = new JMenuItem ("Avsluta");
- private JMenu antsp = new JMenu ("Antalet spelare");
- private JMenuItem antsp1 = new JMenuItem ("1");
- private JMenuItem antsp2 = new JMenuItem ("2");
- private JMenuItem antsp3 = new JMenuItem ("3");
- private JMenuItem antsp4 = new JMenuItem ("4");
- private JMenu storl = new JMenu ("Spelplansstorlek");
- private JMenuItem storl1 = new JMenuItem ("3x4");
- private JMenuItem storl2 = new JMenuItem ("4x4");
- private JMenuItem storl3 = new JMenuItem ("4x6");
- private JMenuItem storl4 = new JMenuItem ("6x6");
- public Memory(){
- //Sätta startvariabler
- spelare = Integer.parseInt(showInputDialog("Hur många spelare?"));
- String[] a0 = {"3*4", "4*4", "4*6", "6*6"};
- int svar = JOptionPane.showOptionDialog( null ,"Hur stor spelplan vill du ha?", "Startup",
- JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, a0, a0[1]);
- if(svar == 0){
- a = 3;
- b = 4;
- }
- else if(svar == 1){
- a = 4;
- b = 4;
- }
- else if(svar == 2){
- a = 4;
- b = 6;
- }
- else if(svar == 3){
- a = 6;
- b = 6;
- }
- //Skapa knappar/kort
- Kort[] allaKort;
- Kort[] aktivaKort = new Kort[a*b];
- Kort[] uppvandaKort;
- //Lägger till menyer och undermenyer
- setJMenuBar(menyalternativ);
- menyalternativ.add(alt); menyalternativ.add(inst);
- alt.add(nyttsp); alt.add(avs);
- inst.add(antsp);
- antsp.add(antsp1); antsp.add(antsp2); antsp.add(antsp3); antsp.add(antsp4); //Antalet spelare-meny
- inst.add(storl);
- storl.add(storl1); storl.add(storl2); storl.add(storl3); storl.add(storl4);
- //Koppla actionlistener till menyknapparna
- nyttsp.addActionListener(this);
- avs.addActionListener(this);
- antsp1.addActionListener(this);
- antsp2.addActionListener(this);
- antsp3.addActionListener(this);
- antsp4.addActionListener(this);
- storl1.addActionListener(this);
- storl2.addActionListener(this);
- storl3.addActionListener(this);
- storl4.addActionListener(this);
- //Nyttspel, spelsetup
- nyttSpel();
- pack();
- setVisible(true);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public void nyttSpel(){
- //Importerar bilder
- File bildmapp = new File("bildmapp");
- File[] bilder = bildmapp.listFiles();
- allaKort = new Kort[bilder.length];
- for(int j = 0 ; j < allaKort.length ; j++){
- allaKort[j] = new Kort(new ImageIcon(bilder[j].getPath()));
- }
- Verktyg.slumpOrdning(allaKort);
- for(int k = 0 ; k < ((a*b)/2) ; k++){
- aktivaKort[k] = allaKort[k].copy();
- aktivaKort[k+((a*b)/2)] = allaKort[k].copy();
- }
- Verktyg.slumpOrdning(aktivaKort);
- //Lägger ut stommen för programmet
- for(int s = 0; s < (a*b) ; s++){ //Lägger till korten till spelplanen
- spelplan.add(aktivaKort[s]);
- }
- spelplan.setLayout(new GridLayout(a,b)); //Sätter spelplanen till ett rutnät
- setLayout(new BorderLayout()); //Sätter ut ramen för programmet och
- add(spelplan, BorderLayout.CENTER); //lägger till de 2 huvudkomponenterna
- add(spelareplats, BorderLayout.WEST);
- setSize(150*a,150*b); //Anpassar spelplansstorleken
- setVisible(true);
- }
- //Nadine, spelare
- /* JLabel[] u = new JLabel[spelare];
- for (int i=0; i<spelare; i++) {
- u[i] = new JLabel("Spelare"+(i+1));
- u[i].setHorizontalAlignment(JLabel.CENTER);
- u[i].setPreferredSize(new Dimension(100,100));
- u[i].setBorder(new LineBorder(Color.RED));
- spelareplats.add(u[i]);
- }
- */
- //Startar om spelet och anpassar storlek efter inställningar
- //lyssnametoder
- public void actionPerformed(ActionEvent e){
- if (e.getSource() == nyttsp){
- nyttSpel();
- }
- else if(e.getSource() == avs){
- System.exit(0);
- }
- else if(e.getSource() == antsp1){
- spelare = 1;
- nyttSpel();
- }
- else if(e.getSource() == antsp2){
- spelare = 2;
- nyttSpel();
- }
- else if(e.getSource() == antsp3){
- spelare = 3;
- nyttSpel();
- }
- else if(e.getSource() == antsp4){
- spelare = 4;
- nyttSpel();
- }
- else if(e.getSource() == storl1){
- a= 3; b= 4;
- nyttSpel();
- }
- else if(e.getSource() == storl2){
- a= 4; b= 4;
- nyttSpel();
- }
- else if(e.getSource() == storl3){
- a= 4; b= 6;
- nyttSpel();
- }
- else if(e.getSource() == storl4){
- a= 6; b= 6;
- nyttSpel();
- }
- }
- public static void main(String [] arg){
- new Memory();
- }
- }
Add Comment
Please, Sign In to add comment