import
java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class CardEx extends JFrame{ Integer cnt =0; Image Card[] = new Image[5]; int wh=70,ht=100; int vX[]=new int[Card.length]; int vY[]=new int[Card.length]; boolean back[] = new boolean[Card.length]; boolean ctch[] = new boolean[Card.length]; int helpX,helpY,clicks; Panel p= new Panel(); Button s=new Button("save"), l=new Button("load"); TextField tf = new TextField("moves: "+ cnt); Ce ce= new Ce(); CardEx(int x, int y, int ln, int ht){ setLayout(new BorderLayout()); setBounds(x, y, ln, ht); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p.add(s); p.add(l); s.addActionListener(new Save()); l.addActionListener(new Load()); ce.setBackground(Color.green); add("Center",ce); add("South",p); for(int i=0; i<Card.length;i++){ Card[i]=Toolkit.getDefaultToolkit().getImage("C"+i+".jpg"); vX[i]=20+55*i; vY[i]=30; back[i]=ctch[i]=false; } ce.addMouseListener(new MouseHandler()); ce.addMouseMotionListener(new MouseMotionHandler()); validate(); } public static void main(String arg[]){ new CardEx(50,50,500,500); } class Ce extends Canvas{ public void paint(Graphics g){ for(int i=0;i<Card.length;i++){ g.drawImage(Card[i], vX[i], vY[i], wh,ht,this); g.drawRect(vX[i], vY[i], wh,ht); } } } class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent e){ helpX=e.getX(); helpY=e.getY(); for(int i=0;i<Card.length;i++){ if(chckIn(i,helpX,helpY)){ ctch[i]=true; if(e.getClickCount()==2){ if(back[i]) Card[i]=Toolkit.getDefaultToolkit().getImage("C"+i+".jpg"); else Card[i]=Toolkit.getDefaultToolkit().getImage("Cback.jpg"); back[i]=!back[i]; ce.repaint(vX[i],vY[i],wh,ht); } } } clicks=e.getClickCount(); ce.repaint(); } public void mouseReleased(MouseEvent e){ for(int i=0;i<Card.length;i++){ if(ctch[i]) tf.setText(": "+ ++cnt); ctch[i]=false; } ce.repaint(); } } class MouseMotionHandler extends MouseMotionAdapter { public void mouseDragged(MouseEvent e){ int x = e.getX(); int y = e.getY(); for(int i=0;i<Card.length;i++) if(ctch[i]) { vX[i]+=(x-helpX); vY[i]+=(y-helpY); ce.repaint(vX[i],vY[i],wh,ht); helpX=x; helpY=y; } } } boolean chckIn(int ncard, int x, int y){ if( (x>vX[ncard])&& (x< vX[ncard]+wh) && (y> vY[ncard])&& (y< vY[ncard]+ht) ) return true; return false; } class Save implements ActionListener { public void actionPerformed(ActionEvent e ){ ObjectOutputStream oos = null; try{ oos = new ObjectOutputStream ( new FileOutputStream ("save.ser")); oos.writeObject(vX); oos.writeObject(vY); oos.writeObject(back); } catch (IOException ex){ System.out.println(ex); } try{ if(oos!=null)oos.close(); } catch (IOException ex){} } } class Load implements ActionListener { public void actionPerformed(ActionEvent e ){ ObjectInputStream ios = null; try{ ios = new ObjectInputStream (new FileInputStream ("save.ser")); vX=(int[])ios.readObject(); vY=(int[])ios.readObject(); back=(boolean[])ios.readObject(); for(int i=0;i<Card.length;i++) if(back[i]) Card[i]=Toolkit.getDefaultToolkit().getImage("Cback.jpg"); else Card[i]=Toolkit.getDefaultToolkit().getImage("C"+i+".jpg"); ce.repaint(); } catch (Exception ex){ } try{ if(ios!=null)ios.close(); } catch (IOException ex){} } } } |