Guest User

Puzzle

a guest
Dec 11th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 KB | None | 0 0
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.EventQueue;
  5. import java.awt.Graphics2D;
  6. import java.awt.GridLayout;
  7. import java.awt.Image;
  8. import java.awt.Point;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.image.BufferedImage;
  13. import java.awt.image.CropImageFilter;
  14. import java.awt.image.FilteredImageSource;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.imageio.ImageIO;
  23. import javax.swing.AbstractAction;
  24. import javax.swing.BorderFactory;
  25. import javax.swing.ImageIcon;
  26. import javax.swing.JButton;
  27. import javax.swing.JComponent;
  28. import javax.swing.JFrame;
  29. import javax.swing.JOptionPane;
  30. import javax.swing.JPanel;
  31.  
  32. class MyButton extends JButton {
  33.  
  34. private boolean isLastButton;
  35.  
  36. public MyButton() {
  37.  
  38. super();
  39.  
  40. initUI();
  41. }
  42.  
  43. public MyButton(Image image) {
  44.  
  45. super(new ImageIcon(image));
  46.  
  47. initUI();
  48. }
  49.  
  50. private void initUI() {
  51.  
  52. isLastButton = false;
  53. BorderFactory.createLineBorder(Color.gray);
  54.  
  55. addMouseListener(new MouseAdapter() { // When we hover a mouse pointer over the button, the border changes to blue color.
  56. @Override
  57. public void mouseEntered(MouseEvent e) {
  58. setBorder(BorderFactory.createLineBorder(Color.blue));
  59. }
  60.  
  61. @Override
  62. public void mouseExited(MouseEvent e) {
  63. setBorder(BorderFactory.createLineBorder(Color.gray));
  64. }
  65. });
  66. }
  67.  
  68. public void setLastButton() {
  69.  
  70. isLastButton = true;
  71. }
  72.  
  73. public boolean isLastButton() { // button that does not have an image. Other buttons swap space with this one.
  74.  
  75. return isLastButton;
  76. }
  77. }
  78.  
  79. public class PuzzleMain extends JFrame {
  80.  
  81. private JPanel panel;
  82. private BufferedImage source;
  83. private BufferedImage resized;
  84. private Image image;
  85. private MyButton lastButton;
  86. private int width, height;
  87.  
  88. private List<MyButton> buttons;
  89. private List<Point> solution;
  90.  
  91. private final int NUMBER_OF_BUTTONS = 12;
  92. private final int DESIRED_WIDTH = 724; // scale the image to have the desired width
  93.  
  94. public PuzzleMain() {
  95.  
  96. initUI();
  97. }
  98.  
  99. private void initUI() {
  100.  
  101. solution = new ArrayList<>(); // array list stores the correct order of buttons which forms the image.
  102.  
  103. solution.add(new Point(0, 0)); // Each button is identified by one Point.
  104. solution.add(new Point(0, 1));
  105. solution.add(new Point(0, 2));
  106. solution.add(new Point(1, 0));
  107. solution.add(new Point(1, 1));
  108. solution.add(new Point(1, 2));
  109. solution.add(new Point(2, 0));
  110. solution.add(new Point(2, 1));
  111. solution.add(new Point(2, 2));
  112. solution.add(new Point(3, 0));
  113. solution.add(new Point(3, 1));
  114. solution.add(new Point(3, 2));
  115.  
  116. buttons = new ArrayList<>();
  117.  
  118. panel = new JPanel();
  119. panel.setBorder(BorderFactory.createLineBorder(Color.gray));
  120. panel.setLayout(new GridLayout(4, 3, 0, 0));
  121.  
  122. try {
  123. source = loadImage();
  124. int h = getNewHeight(source.getWidth(), source.getHeight());
  125. resized = resizeImage(source, DESIRED_WIDTH, h,
  126. BufferedImage.TYPE_INT_ARGB);
  127.  
  128. } catch (IOException ex) {
  129. Logger.getLogger(PuzzleMain.class.getName()).log(
  130. Level.SEVERE, null, ex);
  131. }
  132.  
  133. width = resized.getWidth(null);
  134. height = resized.getHeight(null);
  135.  
  136. getContentPane().add(panel, BorderLayout.CENTER);
  137.  
  138. for (int i = 0; i < 4; i++) {
  139.  
  140. for (int j = 0; j < 3; j++) {
  141.  
  142. image = createImage(new FilteredImageSource(resized.getSource(),
  143. new CropImageFilter(j * width / 3, i * height / 4, // cut a rectangular shape from the already resized image source.
  144. (width / 3), height / 4)));
  145.  
  146. MyButton button = new MyButton(image);
  147. button.putClientProperty("position", new Point(i, j));
  148.  
  149. if (i == 3 && j == 2) {
  150. lastButton = new MyButton();
  151. lastButton.setBorderPainted(false);
  152. lastButton.setContentAreaFilled(false);
  153. lastButton.setLastButton();
  154. lastButton.putClientProperty("position", new Point(i, j));
  155. } else {
  156. buttons.add(button);
  157. }
  158. }
  159. }
  160.  
  161. Collections.shuffle(buttons); // randomly reorder the elements of the buttons list
  162. buttons.add(lastButton); // the button with no image, is inserted at the end of the list,always goes at the end.
  163.  
  164. for (int i = 0; i < NUMBER_OF_BUTTONS; i++) {
  165. // All components from the buttons list are placed on the panel. We create gray border around the buttons and add a click action listener.
  166. MyButton btn = buttons.get(i);
  167. panel.add(btn);
  168. btn.setBorder(BorderFactory.createLineBorder(Color.gray));
  169. btn.addActionListener(new ClickAction());
  170. }
  171.  
  172. pack();
  173. setTitle("Puzzle");
  174. setResizable(false);
  175. setLocationRelativeTo(null);
  176. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  177. }
  178.  
  179. private int getNewHeight(int w, int h) {
  180. // method calculates the height of the image based on the desired width,We scale the image using these values.
  181. double ratio = DESIRED_WIDTH / (double) w;
  182. int newHeight = (int) (h * ratio);
  183. return newHeight;
  184. }
  185.  
  186. private BufferedImage loadImage() throws IOException {
  187.  
  188. BufferedImage bimg = ImageIO.read(new File("src/Pics/Horse.jpg"));
  189.  
  190. return bimg;
  191. }
  192.  
  193. private BufferedImage resizeImage(BufferedImage originalImage, int width,
  194. int height, int type) throws IOException {
  195.  
  196. BufferedImage resizedImage = new BufferedImage(width, height, type);
  197. Graphics2D g = resizedImage.createGraphics();
  198. g.drawImage(originalImage, 0, 0, width, height, null);
  199. g.dispose();
  200.  
  201. return resizedImage;
  202. }
  203.  
  204. private class ClickAction extends AbstractAction {
  205.  
  206. @Override
  207. public void actionPerformed(ActionEvent e) {
  208.  
  209. checkButton(e);
  210. checkSolution();
  211. }
  212.  
  213. private void checkButton(ActionEvent e) {
  214.  
  215. int lidx = 0;
  216.  
  217. for (MyButton button : buttons) {
  218. if (button.isLastButton()) {
  219. lidx = buttons.indexOf(button);
  220. }
  221. }
  222.  
  223. JButton button = (JButton) e.getSource();
  224. int bidx = buttons.indexOf(button);
  225.  
  226. if ((bidx - 1 == lidx) || (bidx + 1 == lidx)
  227. || (bidx - 3 == lidx) || (bidx + 3 == lidx)) {
  228. Collections.swap(buttons, bidx, lidx);
  229. updateButtons();
  230. }
  231. }
  232.  
  233. private void updateButtons() { // method maps the list to the panel's grid
  234.  
  235. panel.removeAll(); // all components are removed
  236.  
  237. for (JComponent btn : buttons) { //loop to go through the buttons list to add the reordered buttons back to the panel's layout manager.
  238.  
  239. panel.add(btn);
  240. }
  241.  
  242. panel.validate(); // method implements the new layout.
  243. }
  244. }
  245.  
  246. private void checkSolution() {
  247. // comparing the list of points of the correctly ordered buttons with the current list containing the order of buttons from the window
  248. List<Point> current = new ArrayList<>();
  249.  
  250. for (JComponent btn : buttons) {
  251. current.add((Point) btn.getClientProperty("position"));
  252. }
  253.  
  254. if (compareList(solution, current)) {
  255. JOptionPane.showMessageDialog(panel, "Finished",
  256. "Congratulation", JOptionPane.INFORMATION_MESSAGE);
  257. }
  258. }
  259.  
  260. public static boolean compareList(List<Point> ls1, List<Point> ls2) {
  261.  
  262. return ls1.toString().contentEquals(ls2.toString());
  263. }
  264.  
  265. public static void main(String[] args) {
  266.  
  267. EventQueue.invokeLater(new Runnable() {
  268.  
  269. @Override
  270. public void run() {
  271.  
  272. PuzzleMain puzzle = new PuzzleMain();
  273. puzzle.setVisible(true);
  274. }
  275. });
  276. }
  277. }
Add Comment
Please, Sign In to add comment