Java 滑块难题

Java 滑块难题,java,swing,Java,Swing,这是我的滑块益智游戏。到目前为止,它只能玩3x3个游戏。当我尝试传递电路板的变量l和w(长度和宽度)时,它不起作用。它只在我将变量ROWS和COLS设置为final时起作用。当我尝试改变它时,我会出错。我不知道该怎么办,任何帮助都将不胜感激 目前,用户可以输入目前无法执行任何操作的值。当游戏开始时,会生成一个3x3的棋盘。用户可以使用另一个加扰的棋盘重新启动游戏,但解算棋盘的按钮和将棋盘重置为原始状态的按钮尚未工作 public class SlidePuzzle { public stati

这是我的滑块益智游戏。到目前为止,它只能玩3x3个游戏。当我尝试传递电路板的变量l和w(长度和宽度)时,它不起作用。它只在我将变量ROWS和COLS设置为final时起作用。当我尝试改变它时,我会出错。我不知道该怎么办,任何帮助都将不胜感激

目前,用户可以输入目前无法执行任何操作的值。当游戏开始时,会生成一个3x3的棋盘。用户可以使用另一个加扰的棋盘重新启动游戏,但解算棋盘的按钮和将棋盘重置为原始状态的按钮尚未工作

public class SlidePuzzle {

public static void main(String[] args) 
{

    JFrame window = new JFrame("Slide Puzzle");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String length = JOptionPane.showInputDialog("Length");
    String width = JOptionPane.showInputDialog("Width");
    int l = Integer.parseInt(length);
    int w = Integer.parseInt(width);
    window.setContentPane(new SlidePuzzleGUI());
    window.pack();  
    window.show();  
    window.setResizable(false);

}
}

public class SlidePuzzleGUI extends JPanel
{

private GraphicsPanel    _puzzleGraphics;
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel();
此类包含滑块拼图的GUI

public SlidePuzzleGUI() {

    JButton newGameButton = new JButton("New Game");
    JButton resetButton = new JButton("Reset");
    JButton solveButton = new JButton("I GIVE UP :(");

    resetButton.addActionListener(new ResetAction());
    newGameButton.addActionListener(new NewGameAction());


    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());
    controlPanel.add(newGameButton);
    controlPanel.add(resetButton);
    controlPanel.add(solveButton);


    _puzzleGraphics = new GraphicsPanel();

    this.setLayout(new BorderLayout());
    this.add(controlPanel, BorderLayout.NORTH);
    this.add(_puzzleGraphics, BorderLayout.CENTER);
}
这是图形面板

class GraphicsPanel extends JPanel implements MouseListener {
    private static final int ROWS = 3;
    private static final int COLS = 3;

    private static final int CELL_SIZE = 80; 
    private Font _biggerFont;



    public GraphicsPanel() {
        _biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE/2);
        this.setPreferredSize(
               new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS));
        this.setBackground(Color.black);
        this.addMouseListener(this); 
    }



    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int r=0; r<ROWS; r++) {
            for (int c=0; c<COLS; c++) {
                int x = c * CELL_SIZE;
                int y = r * CELL_SIZE;
                String text = _puzzleModel.getFace(r, c);
                if (text != null) {
                    g.setColor(Color.gray);
                    g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);
                    g.setColor(Color.black);
                    g.setFont(_biggerFont);
                    g.drawString(text, x+20, y+(3*CELL_SIZE)/4);
                }
            }
        }
    }



    public void mousePressed(MouseEvent e) {

        int col = e.getX()/CELL_SIZE;
        int row = e.getY()/CELL_SIZE;

        if (!_puzzleModel.moveTile(row, col)) {

            Toolkit.getDefaultToolkit().beep();
        }

        this.repaint();
    }



    public void mouseClicked (MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered (MouseEvent e) {}
    public void mouseExited  (MouseEvent e) {}
}

public class NewGameAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        _puzzleModel.reset();
        _puzzleGraphics.repaint();
    }
}
public class ResetAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        _puzzleModel.tryAgain();
        _puzzleGraphics.repaint();

    }

}

public class solveAction implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent e)
        {
            _puzzleModel.solve();
            _puzzleGraphics.repaint();

        }

    }
}
public class SlidePuzzleModel {
private static final int ROWS = 3;
private static final int COLS = 3;

private Tile[][] _contents;  
private Tile[][] _solved;
private Tile     _emptyTile; 



public SlidePuzzleModel() {
    _contents = new Tile[ROWS][COLS];
    reset();
}



String getFace(int row, int col) {
    return _contents[row][col].getFace();
}



public void reset() {
    for (int r=0; r<ROWS; r++) {
        for (int c=0; c<COLS; c++) {
            _contents[r][c] = new Tile(r, c, "" + (r*COLS+c+1));
        }
    }

    _emptyTile = _contents[ROWS-1][COLS-1];
    _emptyTile.setFace(null);


    for (int r=0; r<ROWS; r++) {
        for (int c=0; c<COLS; c++) {
            exchangeTiles(r, c, (int)(Math.random()*ROWS)
                              , (int)(Math.random()*COLS));
        }
    }
}

public void tryAgain()
{

}

public void solve()
{
    for (int i = 1; i < ROWS+1;i++)
    {
        for(int j = 1; j < COLS+1;j++)
        {
            exchangeTiles(i, j, i, j);
        }
    }
}


public boolean moveTile(int r, int c) {

    return checkEmpty(r, c, -1, 0) || checkEmpty(r, c, 1, 0)
        || checkEmpty(r, c, 0, -1) || checkEmpty(r, c, 0, 1);
}

private boolean checkEmpty(int r, int c, int rdelta, int cdelta) {
    int rNeighbor = r + rdelta;
    int cNeighbor = c + cdelta;

    if (isLegalRowCol(rNeighbor, cNeighbor) 
              && _contents[rNeighbor][cNeighbor] == _emptyTile) {
        exchangeTiles(r, c, rNeighbor, cNeighbor);
        return true;
    }
    return false;
}

public boolean isLegalRowCol(int r, int c) {
    return r>=0 && r<ROWS && c>=0 && c<COLS;
}



private void exchangeTiles(int r1, int c1, int r2, int c2) {
    Tile temp = _contents[r1][c1];
    _contents[r1][c1] = _contents[r2][c2];
    _contents[r2][c2] = temp;
}

public boolean isGameOver() {
    for (int r=0; r<ROWS; r++) {
        for (int c=0; c<ROWS; c++) {
            Tile trc = _contents[r][c];
            return trc.isInFinalPosition(r, c);
        }
    }


    return true;
}
}

class Tile {

private int _row;     
private int _col;     
private String _face;  

public Tile(int row, int col, String face) {
    _row = row;
    _col = col;
    _face = face;
}
public void setFace(String newFace) {
    _face = newFace;
}
public String getFace() {
    return _face;
}
public boolean isInFinalPosition(int r, int c) {
    return r==_row && c==_col;
}
}
} 公共类SlidePuzzleGUI扩展了JPanel {

private GraphicsPanel\u puzzleGraphics;
私有SlidePuzzleModel_puzzleModel=新SlidePuzzleModel();
公共幻灯片puzzlegui(int l,int w){
JButton newGameButton=新JButton(“新游戏”);
JButton resetButton=新JButton(“重置”);
JButton solveButton=新JButton(“我放弃:(”);
addActionListener(新的ResetAction());
addActionListener(newNewGameAction());
JPanel controlPanel=新的JPanel();
setLayout(新的FlowLayout());
控制面板。添加(新游戏按钮);
控制面板。添加(重置按钮);
控制面板。添加(按钮);
_puzzleGraphics=新的GraphicsPanel(l,w);
此.setLayout(新的BorderLayout());
添加(控制面板,BorderLayout.NORTH);
添加(_puzzleGraphics,BorderLayout.CENTER);
}
类GraphicsPanel扩展JPanel实现MouseListener{
私有int行;
私人公司;
专用静态最终整数单元格大小=80;
私人字体(biggerFont);;
公共图形面板(内部l、内部w){
_biggerFont=新字体(“SansSerif”,Font.BOLD,单元格大小/2);
此.setPreferredSize(
新维度(单元格大小*列,单元格大小*行);
这个.背景(颜色.黑色);
这个。addMouseListener(这个);
行=l;
COLS=w;
}
公共组件(图形g){
超级组件(g);

对于(int r=0;r您的键类没有允许您将长度和宽度传递给它们的setter方法或构造函数参数。如果您想更改其状态的这一方面,则它们必须有一种机制来执行此操作。目前,GraphicsPanel已硬编码为使用ROWS和COLS常量,这将很难更改。Instead替换所有这些硬编码变量,确保它们默认为默认构造函数中的ROWS和COLS常量,但也提供一个非默认构造函数,允许程序员传入不同的row和col设置


编辑
你必须

  • 使用构造函数参数设置类字段,以便整个类可以使用这些值,而不仅仅是构造函数
  • 调用适当的构造函数,即需要参数的构造函数
i、 e


你得到了什么错误,在哪里?好吧,现在你正在输入长度和宽度,但没有对它们做任何处理,并且在
GraphicsPanel
中为行和列使用常量。可能会将长度和宽度参数添加到
SlidePuzzleGUI
构造函数和
GraphicsPanel
构造函数中?然后rogram在构造
SlidePuzzleGUI
时使用它们,
SlidePuzzleGUI
在构造
GraphicsPanel
时转身并使用它们……或者还有其他问题吗?我尝试将它们放入构造函数的参数中,但这给了我错误,如果你给我一分钟时间,我可以重新键入我尝试的内容earlier@user2776808:将更改后的代码添加到当前文章的底部,但同时保留当前代码。@user2776808:请参见我的编辑。这实际上只是基本Java,与Swing或高级概念没有多大关系。它仍然不起作用,我正在得到一个我认为尺寸正确的板,但窗口不正确@用户2776808:如果您有错误,请显示它们。您所说的“窗口不正确”是什么意思?
public static void main(String[] args) 
{

    JFrame window = new JFrame("Slide Puzzle");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String length = JOptionPane.showInputDialog("Length");
    String width = JOptionPane.showInputDialog("Width");
    int l = Integer.parseInt(length);
    int w = Integer.parseInt(width);
    window.setContentPane(new SlidePuzzleGUI());
    window.pack();  
    window.show();  
    window.setResizable(false);

}
private GraphicsPanel    _puzzleGraphics;
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel();




public SlidePuzzleGUI(int l, int w) {

    JButton newGameButton = new JButton("New Game");
    JButton resetButton = new JButton("Reset");
    JButton solveButton = new JButton("I GIVE UP :(");

    resetButton.addActionListener(new ResetAction());
    newGameButton.addActionListener(new NewGameAction());


    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());
    controlPanel.add(newGameButton);
    controlPanel.add(resetButton);
    controlPanel.add(solveButton);


    _puzzleGraphics = new GraphicsPanel(l,w);

    this.setLayout(new BorderLayout());
    this.add(controlPanel, BorderLayout.NORTH);
    this.add(_puzzleGraphics, BorderLayout.CENTER);
}

class GraphicsPanel extends JPanel implements MouseListener {
    private int ROWS;
    private int COLS;

    private static final int CELL_SIZE = 80; 
    private Font _biggerFont;



    public GraphicsPanel(int l, int w) {
        _biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE/2);
        this.setPreferredSize(
               new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS));
        this.setBackground(Color.black);
        this.addMouseListener(this); 
        ROWS = l;
        COLS = w;
    }



    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int r=0; r<ROWS; r++) {
            for (int c=0; c<COLS; c++) {
                int x = c * CELL_SIZE;
                int y = r * CELL_SIZE;
                String text = _puzzleModel.getFace(r, c);
                if (text != null) {
                    g.setColor(Color.gray);
                    g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);
                    g.setColor(Color.black);
                    g.setFont(_biggerFont);
                    g.drawString(text, x+20, y+(3*CELL_SIZE)/4);
                }
            }
        }
    }



    public void mousePressed(MouseEvent e) {

        int col = e.getX()/CELL_SIZE;
        int row = e.getY()/CELL_SIZE;

        if (!_puzzleModel.moveTile(row, col)) {

            Toolkit.getDefaultToolkit().beep();
        }

        this.repaint();
    }



    public void mouseClicked (MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered (MouseEvent e) {}
    public void mouseExited  (MouseEvent e) {}
}

public class NewGameAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        _puzzleModel.reset();
        _puzzleGraphics.repaint();
    }
}
public class ResetAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        _puzzleModel.tryAgain();
        _puzzleGraphics.repaint();

    }

}

public class solveAction implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent e)
        {
            _puzzleModel.solve();
            _puzzleGraphics.repaint();

        }

    }
}
public MyConstructor(int x, int y) {
   this.x = x;
   this.y = y;
}