eclipse 滑动拼图代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15401128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:10:48  来源:igfitidea点击:

sliding puzzle code

javaeclipsepuzzlesliding

提问by TomL

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?

我们的小组项目是使用 Java Eclipse 创建一个滑动益智游戏。我们有代码来创建一个具有相同图片的 3x3 网格(它应该是一张图片但分成 9 块,但稍后会很好地学习)。我的部分是创建一个鼠标侦听器,单击选定的“磁贴”,然后显示一条消息测试以查看所选磁贴是否被正确单击。(例如:如果用户点击 tile 1,消息显示“tile 1 is clicked”)我想我必须为每个 tile 创建单独的鼠标侦听器。有什么建议?

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();
}

}

}

回答by Patashu

The code you have already makes one listener for the whole puzzle. So in the mouseClickedhandler 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?

您的代码已经为整个谜题制作了一个侦听器。因此,在mouseClicked处理程序中,您可以计算鼠标相对于拼图位置的位置,计算它是否位于顶部/底部/中间三分之一和左侧/右侧/中间三分之一,然后您可以计算出玩家点击了哪一块?

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

您可以使用e.getX()和获取鼠标位置的坐标e.getY()

回答by RE60K

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:

你可以得到鼠标点击的 X 和 Y 坐标,然后分别用宽度和高度除以你可以得到块号..但它会返回 X: 0,1,2 Y:0,1,2 [对于 3x3 块] .. 所以一定要在最后添加一个 (+1) 。你的代码看起来像:

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

Xblock = event.getX()/blockwidth + 1;并且类似地对于 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;

您可能需要获取边框宽度和其他内容才能获得正确的 Click Position 以使拼图的左上角为 0,0;