Advertisement
LegendSujay2019

Puzzle game With Java (2D games)

Nov 8th, 2020
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 KB | None | 0 0
  1. //The Puzzle game//
  2. //Puzzle.java//
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.GridLayout;
  6. import java.awt.Image;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.image.CropImageFilter;
  10. import java.awt.image.FilteredImageSource;
  11.  
  12. import javax.swing.Box;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JPanel;
  18.  
  19.  
  20. public class Puzzle extends JFrame implements ActionListener {
  21.  
  22.     private JPanel centerPanel;
  23.     private JButton button;
  24.     private JLabel label;
  25.     private Image source;
  26.     private Image image;
  27.     int[][] pos;
  28.     int width, height;
  29.  
  30.     public Puzzle() {
  31.  
  32.         pos = new int[][] {
  33.                             {0, 1, 2},
  34.                             {3, 4, 5},
  35.                             {6, 7, 8},
  36.                             {9, 10, 11}
  37.                         };
  38.  
  39.  
  40.         centerPanel = new JPanel();
  41.         centerPanel.setLayout(new GridLayout(4, 4, 0, 0));
  42.  
  43.         ImageIcon sid = new ImageIcon(Puzzle.class.getResource("icesid.jpg"));
  44.         source = sid.getImage();
  45.  
  46.         width = sid.getIconWidth();
  47.         height = sid.getIconHeight();
  48.  
  49.  
  50.         add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH);    
  51.         add(centerPanel, BorderLayout.CENTER);
  52.  
  53.  
  54.         for ( int i = 0; i < 4; i++) {
  55.             for ( int j = 0; j < 3; j++) {
  56.                 if ( j == 2 && i == 3) {
  57.                     label = new JLabel("");
  58.                     centerPanel.add(label);
  59.                 } else {
  60.                     button = new JButton();
  61.                     button.addActionListener(this);
  62.                     centerPanel.add(button);
  63.                     image = createImage(new FilteredImageSource(source.getSource(),
  64.                         new CropImageFilter(j*width/3, i*height/4,
  65.                             (width/3)+1, height/4)));
  66.                     button.setIcon(new ImageIcon(image));
  67.                 }
  68.             }
  69.         }
  70.  
  71.         setSize(325, 275);
  72.         setTitle("Puzzle");
  73.         setResizable(false);
  74.         setLocationRelativeTo(null);
  75.         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  76.         setVisible(true);
  77.     }
  78.  
  79.  
  80.     public static void main(String[] args) {
  81.  
  82.         new Puzzle();
  83.  
  84.     }
  85.  
  86.     public void actionPerformed(ActionEvent e) {
  87.         JButton button = (JButton) e.getSource();
  88.         Dimension size = button.getSize();
  89.  
  90.         int labelX = label.getX();
  91.         int labelY = label.getY();
  92.         int buttonX = button.getX();
  93.         int buttonY = button.getY();
  94.         int buttonPosX = buttonX / size.width;
  95.         int buttonPosY = buttonY / size.height;
  96.         int buttonIndex = pos[buttonPosY][buttonPosX];
  97.  
  98.  
  99.  
  100.         if (labelX == buttonX && (labelY - buttonY) == size.height ) {
  101.  
  102.              int labelIndex = buttonIndex + 3;
  103.  
  104.              centerPanel.remove(buttonIndex);
  105.              centerPanel.add(label, buttonIndex);
  106.              centerPanel.add(button,labelIndex);
  107.              centerPanel.validate();
  108.         }
  109.  
  110.         if (labelX == buttonX && (labelY - buttonY) == -size.height ) {
  111.  
  112.              int labelIndex = buttonIndex - 3;
  113.              centerPanel.remove(labelIndex);
  114.              centerPanel.add(button,labelIndex);
  115.              centerPanel.add(label, buttonIndex);
  116.              centerPanel.validate();
  117.         }
  118.  
  119.         if (labelY == buttonY && (labelX - buttonX) == size.width ) {
  120.  
  121.              int labelIndex = buttonIndex + 1;
  122.  
  123.              centerPanel.remove(buttonIndex);
  124.              centerPanel.add(label, buttonIndex);
  125.              centerPanel.add(button,labelIndex);
  126.              centerPanel.validate();
  127.         }
  128.  
  129.         if (labelY == buttonY && (labelX - buttonX) == -size.width ) {
  130.  
  131.              int labelIndex = buttonIndex - 1;
  132.  
  133.              centerPanel.remove(buttonIndex);
  134.              centerPanel.add(label, labelIndex);
  135.              centerPanel.add(button,labelIndex);
  136.              centerPanel.validate();
  137.         }
  138.     }
  139. }
  140. //The goal of this little game is to form the original picture. We move the buttons by clicking on them. Only buttons adjacent to the label can be moved.//
  141. pos = new int[][] {
  142.     {0, 1, 2},
  143.     {3, 4, 5},
  144.     {6, 7, 8},
  145.     {9, 10, 11}
  146. };
  147. //These are the positions of the image parts.//
  148. ImageIcon sid = new ImageIcon(Puzzle.class.getResource("icesid.jpg"));
  149. source = sid.getImage();
  150. //We use the ImageIcon class to load the image.//
  151. for ( int i = 0; i < 4; i++) {
  152.     for ( int j = 0; j < 3; j++) {
  153.         if ( j == 2 && i == 3) {
  154.             label = new JLabel("");
  155.             centerPanel.add(label);
  156.         } else {
  157.             button = new JButton();
  158.             button.addActionListener(this);
  159.             centerPanel.add(button);
  160.             image = createImage(new FilteredImageSource(source.getSource(),
  161.                 new CropImageFilter(j*width/3, i*height/4,
  162.                     (width/3)+1, height/4)));
  163.             button.setIcon(new ImageIcon(image));
  164.         }
  165.     }
  166. }
  167. //The code creates 11 buttons and one label. We crop the image into pieces and place them on the buttons.//
  168. int labelX = label.getX();
  169. int labelY = label.getY();
  170. int buttonX = button.getX();
  171. int buttonY = button.getY();
  172. //We get the x, y coordinates of the button that we hit and an empty label. The x, y coordinates are important in the logic of the program.//
  173. int buttonPosX = buttonX / size.width;
  174. int buttonPosY = buttonY / size.height;
  175. int buttonIndex = pos[buttonPosY][buttonPosX];
  176. //Here we get the index of the button in the two dimensional array of the button positions.//
  177. if (labelX == buttonX && (labelY - buttonY) == size.height ) {
  178.  
  179.     int labelIndex = buttonIndex + 3;
  180.  
  181.     centerPanel.remove(buttonIndex);
  182.     centerPanel.add(label, buttonIndex);
  183.     centerPanel.add(button,labelIndex);
  184.     centerPanel.validate();
  185. }
  186. //In this case, we check if we clicked on the button, that is right above the empty label. If it is above the label, they share the x coordinate. If the button is right above the label, the equation (labelY - buttonY) == size.height is true.//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement