All pastes #481259 Raw Edit

Johan

public java v1 · immutable
·published 2007-05-10 18:57 UTC
rendered paste body
import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import static javax.swing.JOptionPane.*;import javax.swing.border.*;public class Memory extends JFrame implements ActionListener{	File bildmapp = new File("bildmapp");	public File[] bilder = bildmapp.listFiles();	public Kort[] k = new Kort[bilder.length];	public Kort select=null;	private JLabel[] p = new JLabel[3];   Kort tempies=null;	int[] points = new int[3];         int player_turn=1;		JFrame spelplan = new JFrame("Memory Spel");	JMenuBar bar = new JMenuBar();   JMenu menu = new JMenu("Spel");   JMenu menu2 = new JMenu("Inställningar");   JMenuItem newGame = new JMenuItem("Nytt Spel");   JMenuItem exit = new JMenuItem("Avsluta");	JMenuItem players = new JMenuItem("Antal Spelare");   JMenuItem about = new JMenuItem("Om");		int rad = 0;	int kol = 0;	int antal_spelare = 0;	int kort_kvar = 0;	boolean lika = false;		private Timer tim = new Timer(1500, this);	boolean tim_on = false;		Memory()    {   	for(int i=0; i<bilder.length; i++)      {      	ImageIcon icon = new ImageIcon(bilder[i].getPath());        	Kort kort = new Kort(icon, Kort.Status.DOLT);         k[i] = kort;     	}      nyttSpel();   }	 	public void nyttSpel()   {		String input = showInputDialog("Antal spelare? (1-3)");		if(input == null)			System.exit(0);		antal_spelare = Integer.parseInt(input);					input = showInputDialog("Hur många rader önskas?");		if(input == null)			System.exit(0);		rad = Integer.parseInt(input);					input = showInputDialog("Hur många kolumner önskas?");		if(input == null)			System.exit(0);		kol = Integer.parseInt(input);					int n = rad*kol;      Kort[] l = new Kort[n];		if(n/2>bilder.length || n%2!=0)		{			showMessageDialog(null, "Fel val av rader och kolumner, spelet avslutas");			System.exit(0);		}				        Util.slumpOrdning(k);      int i=0;     	for(; i<(n/2); i++)      {      	l[i]=k[i];     	}      for(int m=0; i!=m*2; m++)      	l[i++]=l[m].copy();      Util.slumpOrdning(l);		for(int m=0; m<l.length; m++)      	spelplan.add(l[m]);		kort_kvar=l.length;      spelplan();				update_player_turn_sign();		spelet();	}		public void spelet()	{		//tim.addActionListener(clock);		/*while(kort_kvar>0)		{			if(lika)				add_point();			else if(!lika)				next_player();		}*/		if(kort_kvar==0)		{			int svar = showConfirmDialog(null, "Vill du spela igen?");			if(svar==0)				reset();			else if(svar==1)				System.exit(0);			}	}		                public Kort get_temp()        {            return tempies;        }                public Kort set_temp(Kort tempy)        {            tempies=tempy;				return null;        }                public void blahoj(Kort detta)        {            if (get_temp()==null)            {                set_temp(detta);            }            else            {                if (detta.equals(get_temp()))                {					 		//lika=true;                    add_point();                    System.out.print("++");                    set_temp(null);                    detta.setStatus(Kort.Status.SAKNAS);                    detta.kortfärg();                    get_temp().setStatus(Kort.Status.SAKNAS);                    get_temp().kortfärg();                }                else                {                    detta.setStatus(Kort.Status.DOLT);                    detta.kortfärg();                    get_temp().setStatus(Kort.Status.DOLT);                    get_temp().kortfärg();                    set_temp(null);						  //lika=false;                    next_player();                }            }      } -------------------import java.awt.*;import java.awt.event.*;import javax.swing.*;//import java.util.*;public class Kort extends JButton implements ActionListener{	public enum Status {DOLT, SYNLIGT, SAKNAS}	Icon bild;	Status kort_status = Status.SAKNAS;	JButton box = new JButton();        Kort select=null;        private Timer tim = new Timer(100, this);		   public Kort(Icon bilden)    {      bild = bilden;                box.setSize(50,50);      		add(box);		box.addActionListener(this);                kortfärg();   }	   public Kort(Icon bilden, Status value)   {   	bild = bilden;		kort_status = value;                box.setSize(50,50);                add(box);		box.addActionListener(this);		kortfärg();	   }	       public void setStatus(Status nytt_tillstånd)   {   	kort_status = nytt_tillstånd;                box.setSize(50,50);                add(box);                		box.addActionListener(this);		kortfärg();   }	   public Status getStatus()   {   	return kort_status;   }       public Kort copy()   {     	return new Kort(bild, kort_status);   }      public boolean equals(Kort second_card)   {     	return bild == second_card.bild && kort_status == second_card.kort_status;   }		public void kortfärg()	{		//b.setPreferredSize(new Dimension(100, 100));               		if(kort_status == Status.DOLT)		  box.setBackground(Color.blue);		else if(kort_status == Status.SYNLIGT)		{		  	//b.setBackground(Color.blue);			box.setIcon(bild);		}		else if(kort_status == Status.SAKNAS)			box.setBackground(Color.white);	}		public void actionPerformed(ActionEvent e)   {		if(e.getSource() == box)                {                     box.setIcon(bild);                     tim.start();                   //  Memory.blahoj(this);                                     }                                   }           }