1, Introduction
There are 16 puzzles and a reference picture. After entering the player's name, the player enters the game, then needs to press the reference picture on the upper left, drag the puzzle on the lower left into the black and white box on the right, and then click OK. The game will return to a correct or wrong conclusion. If the player wants to play the game again, click RESTART, and then drag anywhere in the game, The puzzle will be restored.
- effect
The image material is Vincent's House in Arles (The Yellow House)(1888).
2, Finished product operation steps
(1) Operation steps
- Enter the player's name and click to confirm the start of the game. If the player clicks cancel, the game will exit;
- Players drag each puzzle module to the black-and-white area on the left according to the reference drawing;
- Players click OK to view the results;
- Players click the RESTART button and drag in the game area to reset and play again;
- Players can exit the game by clicking exit from the menu bar, but the game results will not be saved.
- Players can click about from the game to view the game information.
(2) Login
If the player chooses to cancel, he will exit the game. If the player does not input or the input content is empty, after clicking OK, the player name will be player by default.
(3) Jigsaw puzzle error
After pressing the OK key, an error will be prompted and the player name and WEONG will be displayed.
(4) The puzzle is correct
- Within 190 seconds, the player name, PERFERCT and time are displayed.
- Over 190 seconds, the player name, GOOD and time are displayed.
(5) Reset
After clicking the reset button, drag anywhere in the game. In the figure, drag near (615241) to achieve reset.
(6) Function introduction of menu bar
Click about to display game related information. If you click exit, you will exit the game.
3, Source code
import javax.swing.*; import java.awt.*; import java.awt.event.*; /* Jigsaw puzzle */ class Jigsaw extends JPanel implements ActionListener { /* Modules and initial coordinates */ String[] s = {"imgs\\5.jpg","imgs\\9.jpg","imgs\\1.jpg","imgs\\3.jpg","imgs\\2.jpg","imgs\\8.jpg","imgs\\4.jpg","imgs\\16.jpg", "imgs\\11.jpg","imgs\\15.jpg","imgs\\13.jpg","imgs\\6.jpg","imgs\\10.jpg","imgs\\12.jpg","imgs\\14.jpg","imgs\\7.jpg"}; private int[] x = {40,40,40,40,40,40,40,40,140,140,140,140,140,140,140,140}; private int[] y = {200,270,340,410,480,550,620,690,200,270,340,410,480,550,620,690}; /* Move the coordinates x,y to deal with the layer conflict mechanism */ private int px, py, z[] = new int[100], k = 1; /* Player name */ private String name; /* Start and end time */ private long startTime, endTime; /* Point class */ private Point point = new Point(); /* title */ JLabel title = new JLabel("Jigsaw puzzle·Famous paintings"); JLabel reference = new JLabel("Reference figure"); /* Rule description text box */ JTextField rule1 = new JTextField("Rules: 1.Please place the left block in the black and white box above;"); JTextField rule2 = new JTextField("2.Click when finished OK;"); JTextField rule3 = new JTextField("3.To reset, click RESTART,And drag it in other places of the program"); /* Confirm and reset keys */ JButton OK = new JButton("OK"); JButton restart = new JButton("RESTART"); /* Player information */ public void Login() { name = JOptionPane.showInputDialog(null,"Enter Your Name","player"); } /* Select */ public boolean isDragged(int px, int py, int x, int y) { if ((px <= (x + 50) && px >= (x - 10)) && (py <= (y + 50) && py >= (y - 10))) { return true; } return false; } public Jigsaw() { Login(); startTime = System.currentTimeMillis(); add(title); add(reference); add(rule1); add(rule2); add(rule3); add(OK); add(restart); setLayout(null); //Layout with absolute position title.setBounds(700,100,400,50); title.setFont(new Font("Regular script",Font.BOLD,40)); reference.setBounds(145,20,40,40); rule1.setBounds(700,600,400,30); rule2.setBounds(700,630,400,30); rule3.setBounds(700,660,400,30); OK.setBounds(800,550,100,30); restart.setBounds(940,550,100,30); rule1.setEditable(false); rule2.setEditable(false); rule3.setEditable(false); OK.addActionListener(this); restart.addActionListener(this); /* Mouse monitor */ addMouseMotionListener( new MouseAdapter() { /* Drag */ public void mouseDragged(MouseEvent event) { point = event.getPoint(); px = point.x; py = point.y; /* Clearing mechanism */ if (k++ > 95) { k = 1; } for (int i = 0; i < x.length; i++) { if (isDragged(px,py,x[i],y[i])) { z[k] = i; if (z[k] == z[k-1]) //Resolve layer conflicts { x[i] = px; y[i] = py; } } } repaint(); } } ); } /* Execute action */ public void actionPerformed(ActionEvent actionEvent) { if (actionEvent.getSource() == OK) { if ( x[0] >= 698 && x[0] <= 712 && y[0] >= 273 && y[0] <= 287 && //5 // Judgment criteria···· //··· //··· ) { endTime = (System.currentTimeMillis() - startTime) / 1000; if (endTime < 190) { JOptionPane.showMessageDialog(null,name + ", PERFECT, Time cost: " + endTime + "s","Success prompt",JOptionPane.PLAIN_MESSAGE); } else { JOptionPane.showMessageDialog(null,name + ", GOOD, Time cost: " + endTime +"s","Success prompt",JOptionPane.PLAIN_MESSAGE); } } else { JOptionPane.showMessageDialog(null,name + ", WRONG","Error prompt",JOptionPane.ERROR_MESSAGE); } } else if (actionEvent.getSource() == restart) { x[0] = x[1] = x[2] = x[3] = x[4] = x[5] = x[6] = x[7] = 40; // Reset position slightly······ //··· //··· /* Start time reset */ startTime = System.currentTimeMillis(); } } /* Image rendering module */ public void paintComponent(Graphics g) { super.paintComponent(g); /* If the player name is blank or no input, it defaults to player */ if (name == null) { System.exit(0); } if (name.length() == 0) { name = "player"; } /* Drag location, player and time information */ g.drawString("X: " + px + " Y: " + py + " WELCOME, " + name,5,15); /* Place the black and white backplane module */ g.fillRect(700,200,410,290); g.setColor(Color.WHITE); int m = 0, wy[] = {205,345,275,415};//y coordinate of white square for (int i = 705; i <= 1005; i += 100) { for (int j = 0; j < 2; j++) { g.fillRect(i,wy[m],100,70); m++; } if (m == 4) { m = 0; } } /* Reference figure */ g.drawImage(new ImageIcon("imgs\\pic.jpg").getImage(),50,50,230,130,this); /* Puzzle stacking */ for (int i = 0; i < x.length; i++) { g.drawImage(new ImageIcon(s[i]).getImage(),x[i],y[i],96,60,this); } } } /* Combine menu bar */ public class JigsawGraphics extends JFrame implements ActionListener { JMenuBar mb = new JMenuBar(); JMenu game = new JMenu("game"); JMenu help = new JMenu("help"); JMenuItem exit=new JMenuItem("sign out"); JMenuItem about = new JMenuItem("about"); JMenuItem nohelp = new JMenuItem("No help"); public JigsawGraphics(String title) { super(title); setJMenuBar(mb); mb.add(game); mb.add(help); game.add(exit); game.add(about); help.add(nohelp); exit.addActionListener(this); about.addActionListener(this); Jigsaw jg = new Jigsaw(); add(jg); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setSize(1200,820); setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource() == exit) { System.exit(0); } else if (e.getSource() == about) { JOptionPane.showMessageDialog(null, "Development time of this game: 20200616\n Version No.: 1.0.1","about",JOptionPane.PLAIN_MESSAGE); } } public static void main(String[] args) { JigsawGraphics JG = new JigsawGraphics("Jigsaw puzzle·Famous paintings"); } }