Created
April 16, 2019 13:44
-
-
Save uvlad7/c527990f142d140c03a40ca9411f3b8f to your computer and use it in GitHub Desktop.
УП 6.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
public class Main { | |
public static void main(String[] args) { | |
EventQueue.invokeLater(() -> new Puzzle()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.imageio.ImageIO; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.CropImageFilter; | |
import java.awt.image.FilteredImageSource; | |
import java.awt.image.ImageProducer; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MyUtils { | |
public static boolean compareList(List ls1, List ls2) { | |
return ls1.toString().contentEquals(ls2.toString()); | |
} | |
public static int getNewHeight(int desiredWidth, int w, int h) { | |
double ratio = desiredWidth / (double) w; | |
int newHeight = (int) (h * ratio); | |
return newHeight; | |
} | |
public static ImageProducer getImage(Image img, int x, int y, int w, int h) { | |
return new FilteredImageSource(img.getSource(), new CropImageFilter(x, y, w, h)); | |
} | |
public static BufferedImage loadImage(File sourse) throws IOException { | |
return ImageIO.read(sourse); | |
} | |
public static BufferedImage createImage(int w, int h) { | |
BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); | |
bimg.getGraphics().setColor(new Color(0xA8A8A8)); | |
return bimg; | |
} | |
public static BufferedImage resizeImage(BufferedImage originalImage, int width, | |
int height, int type) { | |
var resizedImage = new BufferedImage(width, height, type); | |
var g = resizedImage.createGraphics(); | |
g.drawImage(originalImage, 0, 0, width, height, null); | |
g.dispose(); | |
return resizedImage; | |
} | |
public static boolean checkSolution(List<MyButton> buttons, List<Point> solution) { | |
var current = new ArrayList<Point>(); | |
for (JComponent button : buttons) { | |
current.add((Point) button.getClientProperty("position")); | |
} | |
if (MyUtils.compareList(solution, current)) { | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class Puzzle extends JFrame implements KeyListener { | |
private int verticalPieces; | |
private int horizontalPieces; | |
private int desiredWidth; | |
private int desiredHeight; | |
private JPanel panel; | |
private JPanel examplePanel; | |
private BufferedImage source; | |
private BufferedImage resized; | |
private Image image; | |
private int width, height; | |
private List<MyButton> buttons; | |
private MyButton example; | |
private List<Point> solution; | |
public Puzzle() { | |
verticalPieces = 4; | |
horizontalPieces = 3; | |
desiredWidth = 600; | |
initUI(new File("src\\puzzle.png")); | |
} | |
private void initUI(File picture) { | |
try { | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { | |
} | |
solution = new ArrayList<>(); | |
for (int i = 0; i < verticalPieces; i++) { | |
for (int j = 0; j < horizontalPieces; j++) { | |
solution.add(new Point(i, j)); | |
} | |
} | |
buttons = new ArrayList<>(); | |
panel = new JPanel(); | |
panel.setBorder(BorderFactory.createLineBorder(Color.gray)); | |
panel.setLayout(new GridLayout(verticalPieces, horizontalPieces, 0, 0)); | |
try { | |
source = MyUtils.loadImage(picture); | |
desiredHeight = MyUtils.getNewHeight(desiredWidth, source.getWidth(), source.getHeight()); | |
resized = MyUtils.resizeImage(source, desiredWidth, desiredHeight, | |
BufferedImage.TYPE_INT_ARGB); | |
width = resized.getWidth(); | |
height = resized.getHeight(); | |
} catch (IOException e) { | |
JOptionPane.showMessageDialog(this, "Не удалось загрузить изображение", "Ошибка", | |
JOptionPane.ERROR_MESSAGE); | |
desiredHeight = desiredWidth * 2 / 3; | |
source = MyUtils.createImage(desiredWidth, desiredHeight); | |
resized = source; | |
width = desiredWidth; | |
height = desiredHeight; | |
} | |
for (int i = 0; i < verticalPieces; i++) { | |
for (int j = 0; j < horizontalPieces; j++) { | |
image = createImage(MyUtils.getImage(resized, j * width / horizontalPieces, i * height / verticalPieces, | |
width / horizontalPieces, height / verticalPieces)); | |
var button = new MyButton(image); | |
button.putClientProperty("position", new Point(i, j)); | |
buttons.add(button); | |
} | |
} | |
Collections.shuffle(buttons); | |
for (MyButton button : buttons) { | |
panel.add(button); | |
button.setBorder(BorderFactory.createLineBorder(Color.gray)); | |
button.addKeyListener(this); | |
} | |
add(panel, BorderLayout.WEST); | |
examplePanel = new JPanel(new BorderLayout()); | |
examplePanel.setBorder(BorderFactory.createLineBorder(Color.black)); | |
example = new MyButton(resized); | |
example.setFocusable(false); | |
example.setBackground(Color.black); | |
example.setBorder(BorderFactory.createLineBorder(Color.black)); | |
examplePanel.add(example, BorderLayout.CENTER); | |
examplePanel.setPreferredSize(new Dimension(width, height)); | |
add(examplePanel, BorderLayout.EAST); | |
pack(); | |
setTitle("Puzzle"); | |
setResizable(false); | |
setIconImage(new ImageIcon("src\\icon.png").getImage()); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
} | |
@Override | |
public void keyTyped(KeyEvent e) { | |
} | |
@Override | |
public void keyPressed(KeyEvent e) { | |
if ((e.getKeyCode() == KeyEvent.VK_LEFT) || (e.getKeyCode() == KeyEvent.VK_RIGHT) | |
|| (e.getKeyCode() == KeyEvent.VK_DOWN) || (e.getKeyCode() == KeyEvent.VK_UP)) { | |
var button = (MyButton) e.getSource(); | |
int bidx = buttons.indexOf(button); | |
int lidx = 0; | |
boolean canSwap = true; | |
switch (e.getKeyCode()) { | |
case KeyEvent.VK_LEFT: { | |
if (bidx % horizontalPieces != 0) { | |
lidx = bidx - 1; | |
} else { | |
canSwap = false; | |
} | |
break; | |
} | |
case KeyEvent.VK_RIGHT: { | |
if (bidx % horizontalPieces != horizontalPieces - 1) { | |
lidx = bidx + 1; | |
} else { | |
canSwap = false; | |
} | |
break; | |
} | |
case KeyEvent.VK_UP: { | |
if (bidx >= horizontalPieces) { | |
lidx = bidx - horizontalPieces; | |
} else { | |
canSwap = false; | |
} | |
break; | |
} | |
case KeyEvent.VK_DOWN: { | |
if (bidx < horizontalPieces * (verticalPieces - 1)) { | |
lidx = bidx + horizontalPieces; | |
} else { | |
canSwap = false; | |
} | |
break; | |
} | |
} | |
if (canSwap) { | |
Collections.swap(buttons, bidx, lidx); | |
Rectangle old = buttons.get(bidx).getBounds(); | |
buttons.get(bidx).setBounds(buttons.get(lidx).getBounds()); | |
buttons.get(lidx).setBounds(old); | |
if (MyUtils.checkSolution(buttons, solution)) { | |
JOptionPane.showMessageDialog(this, "Взлитаем", | |
"Готово", JOptionPane.INFORMATION_MESSAGE); | |
} | |
} | |
} | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment