Example of Pic Puzzle Game

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

WikiSir

Example of Pic Puzzle Game import java.awt.event.*; import java.awt.*; import javax.swing.*; class picpuzzle2 extends JFrame implements ActionListener{ JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,sample,starB; Icon star; Icon ic0=new ImageIcon(“pic/starB0.jpg”); Icon ic10=new ImageIcon(“pic/starB10.jpg”); Icon ic20=new ImageIcon(“pic/starB20.jpg”); Icon samicon1=new ImageIcon(“pic/main.jpg”); Icon samicon2=new ImageIcon(“pic/main2.jpg”); Icon samicon3=new ImageIcon(“pic/main3.jpg”); Icon ic1=new ImageIcon(“pic/1.jpg”); Icon ic2=new ImageIcon(“pic/5.jpg”); Icon ic3=new ImageIcon(“pic/2.jpg”); Icon ic4=new ImageIcon(“pic/7.jpg”); Icon ic5=new ImageIcon(“pic/4.jpg”); Icon ic6=new ImageIcon(“pic/6.jpg”); Icon ic7=new ImageIcon(“pic/8.jpg”); Icon ic8=new ImageIcon(“pic/9.jpg”); Icon ic9=new ImageIcon(“pic/3.jpg”); Icon ic11=new ImageIcon(“pic/12.jpg”); Icon ic12=new ImageIcon(“pic/13.jpg”); Icon ic13=new ImageIcon(“pic/16.jpg”); Icon ic14=new ImageIcon(“pic/11.jpg”); Icon ic15=new ImageIcon(“pic/14.jpg”); Icon ic16=new ImageIcon(“pic/19.jpg”); Icon ic17=new ImageIcon(“pic/17.jpg”); Icon ic18=new ImageIcon(“pic/15.jpg”); Icon ic19=new ImageIcon(“pic/18.jpg”); Icon ic21=new ImageIcon(“pic/24.jpg”); Icon ic22=new ImageIcon(“pic/25.jpg”); Icon ic23=new ImageIcon(“pic/21.jpg”); Icon ic24=new ImageIcon(“pic/27.jpg”); Icon ic25=new ImageIcon(“pic/23.jpg”); Icon ic26=new ImageIcon(“pic/29.jpg”); Icon ic27=new ImageIcon(“pic/28.jpg”); Icon ic28=new ImageIcon(“pic/22.jpg”); Icon ic29=new ImageIcon(“pic/26.jpg”); picpuzzle2(){ super(“pic puzzle”); b1=new JButton(ic1); b1.setBounds(10,80,100,100); b2=new JButton(ic2); b2.setBounds(110,80,100,100); b3=new JButton(ic3); b3.setBounds(210,80,100,100); b4=new JButton(ic4); b4.setBounds(10,180,100,100); b5=new JButton(ic5); […]

Tags:

....
...