简体   繁体   中英

sliding puzzle code

Our group project is to create a sliding puzzle game using Java Eclipse. We have the code to create a 3x3 grid with the same picture(its supposed to be one picture but split into 9 pieces but well learn that later on). My part is to create a mouse listener that clicks the selected 'tile' and then displays a message testing to see if the selected tile was clicked correctly. (Example: if user clicks tile 1, message displays "tile 1 is clicked") I think I will have to create individual mouse listeners for each tile. Any suggestions?

Heres the code:

   import javax.swing.*;
   import javax.swing.event.*;
   import java.awt.*;
   import java.awt.event.*;

   public class Puzzle 
   {
// Initialize the Frame for the Puzzle
private JFrame frame = new JFrame("Puzzle");
private JPanel puzzlePanel = new JPanel( null );
private JLabel status;


// Itemize Menu
private JMenuBar menu = new JMenuBar();
private JMenu menuFile = new JMenu("File");
private JMenuItem menuFileNew = new JMenuItem("New Game");
private JMenuItem menuFileQuit = new JMenuItem("Quit");
private JMenu menuHelp = new JMenu("Help");
private JMenuItem menuHelpAbout = new JMenuItem("About");

// Variables
// Easy(3) Medium(4) Hard(5)
private int dimm = 3;
private int spacing = 5;
private int tileDimm = 96;

// Constructor
public Puzzle() 
{
    status = new JLabel("Default");
    frame.add(status,BorderLayout.SOUTH);


    // Build Menu
    frame.setJMenuBar(menu);
    menuFile.add(menuFileNew);
    menuFile.add(menuFileQuit);
    menuHelp.add(menuHelpAbout);
    menu.add(menuFile);
    menu.add(menuHelp);

    // Panel
    puzzlePanel.setPreferredSize(new Dimension((dimm*tileDimm)+(spacing*(dimm+1)),(dimm*tileDimm)+(spacing*(dimm+1))) );
    frame.add(puzzlePanel);

    // Puzzle Logic
    PuzzleLogic puzzleLogic = new PuzzleLogic(dimm);



    Tile[][] tiles = puzzleLogic.createTileArray();
    puzzleLogic.shuffleTiles(tiles);
    for(int i = 0; i < dimm; i++) 
    {
        for(int ii = 0; ii < dimm; ii++) 
        {
            puzzlePanel.add(tiles[i][ii].getLabel());
            int x = i*tileDimm+(spacing*(i+1));
            int y = ii*tileDimm+(spacing*(ii+1));
            tiles[i][ii].getLabel().setBounds(x,y,tileDimm,tileDimm);

            System.out.print(tiles[i][ii].getContent() + " ");
        }
    }

    HandlerClass handler = new HandlerClass();
    puzzlePanel.addMouseListener(handler);
}

// create listener - inner class
class HandlerClass implements MouseListener
{

    public void mouseClicked(MouseEvent e)
    {

        status.setText(String.format("Tile 1 is clicked"));
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) 
    {
        status.setText(String.format(" "));

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub  
    }

}

public void launch() 
{
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack(); //Adjusts panel to components for display
    frame.setVisible(true);
    frame.setResizable(false);
}

// Calls Everything to Action
public static void main(String[] args) 
{
    Puzzle puzzle = new Puzzle();
    puzzle.launch();
}

}

The code you have already makes one listener for the whole puzzle. So in the mouseClicked handler you could calculate the mouse's position relative to the position in the puzzle, calculate if it's in the top/bottom/middle third and left/right/middle third and from there you can compute which piece the player clicked on?

You can get the coordinates of the mouse position by using e.getX() and e.getY() .

You can get X and Y coordinate of Mouse Click and then divide with width and height respectively you can get the block number.. but it will return X: 0,1,2 Y:0,1,2 [for a 3x3 block].. So make sure to add one (+1) at the last .Your code will somewhat look like:

Xblock = event.getX()/blockwidth + 1; and similialy for Y

You may have to take border width and rest of the things to get correct Click Position to make the upper-left corner of your puzzle 0,0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM