Friday, 14 July 2017

Pic Puzzle Game in java ~ java help

Example of Pic Puzzle Game

Picture Puzzle game in swing
  1. import java.awt.event.*;  
  2. import java.awt.*;  
  3. import javax.swing.*;  
  4. class picpuzzle2 extends JFrame implements ActionListener{  
  5. JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,sample,starB;  
  6. Icon star;  
  7. Icon ic0=new ImageIcon("pic/starB0.jpg");  
  8. Icon ic10=new ImageIcon("pic/starB10.jpg");  
  9. Icon ic20=new ImageIcon("pic/starB20.jpg");  
  10. Icon samicon1=new ImageIcon("pic/main.jpg");  
  11. Icon samicon2=new ImageIcon("pic/main2.jpg");  
  12. Icon samicon3=new ImageIcon("pic/main3.jpg");  
  13. Icon ic1=new ImageIcon("pic/1.jpg");  
  14. Icon ic2=new ImageIcon("pic/5.jpg");  
  15. Icon ic3=new ImageIcon("pic/2.jpg");  
  16. Icon ic4=new ImageIcon("pic/7.jpg");  
  17. Icon ic5=new ImageIcon("pic/4.jpg");  
  18. Icon ic6=new ImageIcon("pic/6.jpg");  
  19. Icon ic7=new ImageIcon("pic/8.jpg");  
  20. Icon ic8=new ImageIcon("pic/9.jpg");  
  21. Icon ic9=new ImageIcon("pic/3.jpg");  
  22.   
  23. Icon ic11=new ImageIcon("pic/12.jpg");  
  24. Icon ic12=new ImageIcon("pic/13.jpg");  
  25. Icon ic13=new ImageIcon("pic/16.jpg");  
  26. Icon ic14=new ImageIcon("pic/11.jpg");  
  27. Icon ic15=new ImageIcon("pic/14.jpg");  
  28. Icon ic16=new ImageIcon("pic/19.jpg");  
  29. Icon ic17=new ImageIcon("pic/17.jpg");  
  30. Icon ic18=new ImageIcon("pic/15.jpg");  
  31. Icon ic19=new ImageIcon("pic/18.jpg");  
  32.   
  33. Icon ic21=new ImageIcon("pic/24.jpg");  
  34. Icon ic22=new ImageIcon("pic/25.jpg");  
  35. Icon ic23=new ImageIcon("pic/21.jpg");  
  36. Icon ic24=new ImageIcon("pic/27.jpg");  
  37. Icon ic25=new ImageIcon("pic/23.jpg");  
  38. Icon ic26=new ImageIcon("pic/29.jpg");  
  39. Icon ic27=new ImageIcon("pic/28.jpg");  
  40. Icon ic28=new ImageIcon("pic/22.jpg");  
  41. Icon ic29=new ImageIcon("pic/26.jpg");  
  42.   
  43. picpuzzle2(){  
  44.   
  45. super("pic puzzle");  
  46.   
  47. b1=new JButton(ic1);  
  48. b1.setBounds(10,80,100,100);  
  49. b2=new JButton(ic2);  
  50. b2.setBounds(110,80,100,100);  
  51. b3=new JButton(ic3);  
  52. b3.setBounds(210,80,100,100);  
  53. b4=new JButton(ic4);  
  54. b4.setBounds(10,180,100,100);  
  55. b5=new JButton(ic5);  
  56. b5.setBounds(110,180,100,100);  
  57. b6=new JButton(ic6);  
  58. b6.setBounds(210,180,100,100);  
  59. b7=new JButton(ic7);  
  60. b7.setBounds(10,280,100,100);  
  61. b8=new JButton(ic8);  
  62. b8.setBounds(110,280,100,100);  
  63. b9=new JButton(ic9);  
  64. b9.setBounds(210,280,100,100);  
  65. sample=new JButton(samicon1);  
  66. sample.setBounds(380,100,200,200);  
  67.   
  68. JLabel l1=new JLabel("Sample:");  
  69. l1.setBounds(330,200,70,20);  
  70. JLabel l2=new JLabel("NOTE:  
  71. icon has power to swap with neighbour icon=");  
  72. l2.setBounds(5,15,500,20);  
  73. JLabel l3=new JLabel("click sample picture to next puzzle");  
  74. l3.setBounds(380,320,200,20);  
  75. l3.setForeground(Color.red);  
  76.   
  77. starB=new JButton(ic0);  
  78. starB.setBounds(330,5,50,50);  
  79. star=b9.getIcon();  
  80.   
  81. add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);  
  82. add(b9);add(sample);add(l1);add(l2);add(starB);add(l3);  
  83. b1.addActionListener(this); b2.addActionListener(this);  
  84.  b3.addActionListener(this); b4.addActionListener(this);   
  85. b5.addActionListener(this); b6.addActionListener(this);  
  86.  b7.addActionListener(this); b8.addActionListener(this);  
  87.  b9.addActionListener(this);   
  88.   
  89. sample.addActionListener(this);  
  90. setLayout(null);  
  91. setSize(600,500);  
  92. setVisible(true);  
  93. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  94. }  
  95.   
  96. public void actionPerformed(ActionEvent e){  
  97. if(e.getSource()==b1){  
  98.     Icon s1=b1.getIcon();  
  99.       if(b2.getIcon()==star){  
  100.         b2.setIcon(s1);  
  101.         b1.setIcon(star);  
  102.       } else if(b4.getIcon()==star){  
  103.                     b4.setIcon(s1);  
  104.                     b1.setIcon(star);  
  105.                    }  
  106.   }//end of if  
  107.   
  108. if(e.getSource()==b2){  
  109.     Icon s1=b2.getIcon();  
  110.       if(b1.getIcon()==star){  
  111.         b1.setIcon(s1);  
  112.         b2.setIcon(star);  
  113.       } else if(b5.getIcon()==star){  
  114.                     b5.setIcon(s1);  
  115.                     b2.setIcon(star);  
  116.                    }  
  117.          else if(b3.getIcon()==star){  
  118.                     b3.setIcon(s1);  
  119.                     b2.setIcon(star);  
  120.                    }  
  121.   }//end of if  
  122.   
  123. if(e.getSource()==b3){  
  124.     Icon s1=b3.getIcon();  
  125.       if(b2.getIcon()==star){  
  126.         b2.setIcon(s1);  
  127.         b3.setIcon(star);  
  128.       } else if(b6.getIcon()==star){  
  129.                     b6.setIcon(s1);  
  130.                     b3.setIcon(star);  
  131.                    }  
  132.   }//end of if  
  133.   
  134. if(e.getSource()==b4){  
  135.     Icon s1=b4.getIcon();  
  136.       if(b1.getIcon()==star){  
  137.         b1.setIcon(s1);  
  138.         b4.setIcon(star);  
  139.       } else if(b5.getIcon()==star){  
  140.                     b5.setIcon(s1);  
  141.                     b4.setIcon(star);  
  142.                    }  
  143.          else if(b7.getIcon()==star){  
  144.                     b7.setIcon(s1);  
  145.                     b4.setIcon(star);  
  146.                    }  
  147.   }//end of if  
  148.   
  149. if(e.getSource()==b5){  
  150.     Icon s1=b5.getIcon();  
  151.       if(b2.getIcon()==star){  
  152.         b2.setIcon(s1);  
  153.         b5.setIcon(star);  
  154.       } else if(b4.getIcon()==star){  
  155.                     b4.setIcon(s1);  
  156.                     b5.setIcon(star);  
  157.                    }  
  158.          else if(b6.getIcon()==star){  
  159.                     b6.setIcon(s1);  
  160.                     b5.setIcon(star);  
  161.                    }  
  162.           else if(b8.getIcon()==star){  
  163.                     b8.setIcon(s1);  
  164.                     b5.setIcon(star);  
  165.                    }  
  166.   }//end of if  
  167.   
  168. if(e.getSource()==b6){  
  169.     Icon s1=b6.getIcon();  
  170.       if(b3.getIcon()==star){  
  171.         b3.setIcon(s1);  
  172.         b6.setIcon(star);  
  173.       } else if(b5.getIcon()==star){  
  174.                     b5.setIcon(s1);  
  175.                     b6.setIcon(star);  
  176.                    }  
  177.          else if(b9.getIcon()==star){  
  178.                     b9.setIcon(s1);  
  179.                     b6.setIcon(star);  
  180.                    }  
  181. }//end of if  
  182.   
  183. if(e.getSource()==b7){  
  184.     Icon s1=b7.getIcon();  
  185.       if(b4.getIcon()==star){  
  186.         b4.setIcon(s1);  
  187.         b7.setIcon(star);  
  188.       } else if(b8.getIcon()==star){  
  189.                     b8.setIcon(s1);  
  190.                     b7.setIcon(star);  
  191.                    }  
  192.  }//end of if  
  193.   
  194.    if(e.getSource()==b8){  
  195.     Icon s1=b8.getIcon();  
  196.       if(b7.getIcon()==star){  
  197.         b7.setIcon(s1);  
  198.         b8.setIcon(star);  
  199.       } else if(b5.getIcon()==star){  
  200.                     b5.setIcon(s1);  
  201.                     b8.setIcon(star);  
  202.                    }  
  203.          else if(b9.getIcon()==star){  
  204.                     b9.setIcon(s1);  
  205.                     b8.setIcon(star);  
  206.                    }  
  207.   
  208.   }//end of if  
  209.   
  210.  if(e.getSource()==b9){  
  211.     Icon s1=b9.getIcon();  
  212.       if(b8.getIcon()==star){  
  213.         b8.setIcon(s1);  
  214.         b9.setIcon(star);  
  215.       } else if(b6.getIcon()==star){  
  216.                     b6.setIcon(s1);  
  217.                     b9.setIcon(star);  
  218.                    }  
  219.   }//end of if  
  220.   
  221. if(e.getSource()==sample){  
  222. Icon s1=sample.getIcon();  
  223.  if(s1==samicon3){  
  224. sample.setIcon(samicon1);  
  225. b1.setIcon(ic1);  
  226. b2.setIcon(ic2);  
  227. b3.setIcon(ic3);  
  228. b4.setIcon(ic4);  
  229. b5.setIcon(ic5);  
  230. b6.setIcon(ic6);  
  231. b7.setIcon(ic7);  
  232. b8.setIcon(ic8);  
  233. b9.setIcon(ic9);  
  234. star=b9.getIcon();  
  235. starB.setIcon(ic0);  
  236. }//eof if  
  237. else if(s1==samicon1){  
  238. sample.setIcon(samicon2);  
  239. b1.setIcon(ic11);  
  240. b2.setIcon(ic12);  
  241. b3.setIcon(ic13);  
  242. b4.setIcon(ic14);  
  243. b5.setIcon(ic15);  
  244. b6.setIcon(ic16);  
  245. b7.setIcon(ic17);  
  246. b8.setIcon(ic18);  
  247. b9.setIcon(ic19);  
  248. star=b6.getIcon();  
  249. starB.setIcon(ic10);  
  250. }//eof else  
  251. else{  
  252. sample.setIcon(samicon3);  
  253. b1.setIcon(ic21);  
  254. b2.setIcon(ic22);  
  255. b3.setIcon(ic23);  
  256. b4.setIcon(ic24);  
  257. b5.setIcon(ic25);  
  258. b6.setIcon(ic26);  
  259. b7.setIcon(ic27);  
  260. b8.setIcon(ic28);  
  261. b9.setIcon(ic29);  
  262. star=b6.getIcon();  
  263. starB.setIcon(ic20);  
  264. }//eof else  
  265.   
  266. }  
  267. }//end of actionPerformed  
  268.   
  269. public static void main(String args[]){  
  270. new picpuzzle2();  
  271. }//end of main  
  272. }//end of class  

No comments:

Post a Comment