0% found this document useful (0 votes)
416 views

Puzzle Java

This document contains the code for a puzzle game application written in Java. It defines a Puzzle class that extends JFrame and implements ActionListener. The Puzzle class initializes the user interface by adding labeled buttons to a grid layout, loads an image to crop and display on the buttons. It also contains the action listener code to swap clicked buttons with the empty label space to solve the puzzle.

Uploaded by

Ankit Arjaria
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
416 views

Puzzle Java

This document contains the code for a puzzle game application written in Java. It defines a Puzzle class that extends JFrame and implements ActionListener. The Puzzle class initializes the user interface by adding labeled buttons to a grid layout, loads an image to crop and display on the buttons. It also contains the action listener code to swap clicked buttons with the empty label space to solve the puzzle.

Uploaded by

Ankit Arjaria
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import import import import import import import import import import import import import import import

java.awt.BorderLayout; java.awt.Dimension; java.awt.GridLayout; java.awt.Image; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.awt.image.CropImageFilter; java.awt.image.FilteredImageSource; javax.swing.Box; javax.swing.ImageIcon; javax.swing.JButton; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JPanel; javax.swing.SwingUtilities;

public class Puzzle extends JFrame implements ActionListener { private JPanel centerPanel; private JButton button; private JLabel label; private Image source; private Image image; int[][] pos; int width, height; public Puzzle() { initUI(); } public final void initUI() { pos = new int[][]{ {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11} }; centerPanel = new JPanel(); centerPanel.setLayout(new GridLayout(4, 4, 0, 0)); ImageIcon sid = new ImageIcon(Puzzle.class.getResource("abc.jpg")); source = sid.getImage(); width = sid.getIconWidth(); height = sid.getIconHeight(); add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH); add(centerPanel, BorderLayout.CENTER); for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { if (j == 2 && i == 3) {

label = new JLabel(""); centerPanel.add(label); } else { button = new JButton(); button.addActionListener(this); centerPanel.add(button); image = createImage(new FilteredImageSource(source.getSource (), new CropImageFilter(j * width / 3, i * height / 4, (width / 3) + 1, height / 4))); button.setIcon(new ImageIcon(image)); } } } setSize(325, 275); setTitle("Puzzle"); // setResizable(false); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); Dimension size = button.getSize(); int int int int int int int labelX = label.getX(); labelY = label.getY(); buttonX = button.getX(); buttonY = button.getY(); buttonPosX = buttonX / size.width; buttonPosY = buttonY / size.height; buttonIndex = pos[buttonPosY][buttonPosX];

if (labelX == buttonX && (labelY - buttonY) == size.height) { int labelIndex = buttonIndex + 3; centerPanel.remove(buttonIndex); centerPanel.add(label, buttonIndex); centerPanel.add(button, labelIndex); centerPanel.validate(); } if (labelX == buttonX && (labelY - buttonY) == -size.height) { int labelIndex = buttonIndex - 3; centerPanel.remove(labelIndex); centerPanel.add(button, labelIndex); centerPanel.add(label, buttonIndex); centerPanel.validate(); } if (labelY == buttonY && (labelX - buttonX) == size.width) { int labelIndex = buttonIndex + 1;

centerPanel.remove(buttonIndex); centerPanel.add(label, buttonIndex); centerPanel.add(button, labelIndex); centerPanel.validate(); } if (labelY == buttonY && (labelX - buttonX) == -size.width) { int labelIndex = buttonIndex - 1; centerPanel.remove(buttonIndex); centerPanel.add(label, labelIndex); centerPanel.add(button, labelIndex); centerPanel.validate(); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Puzzle puzzle = new Puzzle(); puzzle.setVisible(true); } }); } }

You might also like