【问题标题】:Lights out game using 2d arrays使用二维数组熄灯游戏
【发布时间】:2016-06-09 02:10:00
【问题描述】:

我正在尝试制作一款熄灯游戏,我将其简化为一次只更改一个图块,但我仍然遇到一个错误。我可以单击以从黄色变为黑色,但我无法单击并发生相反的情况。如果你不熟悉,这里是游戏:http://www.logicgamesonline.com/lightsout/

我的代码:

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

public class LightsOutPanel extends JPanel implements MouseListener {
    private boolean[][] lights;

    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Lights Out!");
        frame.setResizable(false);
        frame.setVisible(true);

        LightsOutPanel panel = new LightsOutPanel();
        if (panel.lights == null) {
            System.out.println("You did not initialize your light array!" +
                               "It's still null...");
            System.exit(-1);
        }
        panel.addMouseListener(panel);
        panel.setPreferredSize(new Dimension(601, 501));
        panel.setMinimumSize(new Dimension(601, 501));

        Container c = frame.getContentPane();
        c.setLayout(new BorderLayout());
        c.add(panel, BorderLayout.CENTER);

        frame.pack();
    }

    public LightsOutPanel() {
        lights = new boolean[5][6];
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 6; j++) {
                lights[i][j] = true;
            }
        }

    }

    // unused methods
    public void mouseClicked(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void paint(Graphics g) {
        int boxWidth = 600 / 6;
        int boxHeight = 500 / 5;

        int y = 0;
        for (int row = 0; row < 5; row++) {
            int x = 0;
            for (int col = 0; col < 6; col++) {
                if (lights[row][col]==true) {
                    g.setColor(Color.YELLOW);
                } else {
                    g.setColor(Color.BLACK);
                }
                g.fillRect(x, y, boxWidth, boxHeight);

                g.setColor(Color.BLUE);
                g.drawRect(x, y, boxWidth, boxHeight);
                x += boxWidth;
            }
            y += boxHeight;
        }
    }

    // called when the mouse is pressed - determines what row/column the user
    // has clicked
    public void mousePressed(MouseEvent e) {
        int mouseX = e.getX();
        int mouseY = e.getY();

        int panelWidth = getWidth();
        int panelHeight = getHeight();

        int boxWidth = panelWidth / lights[0].length;
        int boxHeight = panelHeight / lights.length;

        int col = mouseX / boxWidth;
        int row = mouseY / boxHeight;

        toggle(row, col);
        repaint();
    }

    // called to "toggle" the selected row and column, as well as the four
    // adjacent lights
    public void toggle(int row, int col) {

        if (row >= 0 && col >= 0 && row < lights.length && 
            col < lights[0].length)
        {
            if (lights[row][col] = true) {
                lights[row][col] = false;
            } else {
                lights[row][col] = true;
            }
        }

    }
}

【问题讨论】:

  • @MadProgrammer 会建议您不要覆盖 paint(),而是覆盖 paintComponent()
  • 你这是什么意思?
  • 另外,toggle() 中的 if 语句使用赋值运算符 = 而不是相等运算符 ==。你的paint(Graphics g) 方法应该先调用super(g)
  • 天哪,我所缺少的只是一个等于,谢谢。
  • 您还应该考虑覆盖 paintComponent(Graphics g) 而不是 paint(Graphics g)。看看Concerns about the function of JPanel: paintcomponent()

标签: java swing


【解决方案1】:

您的“核心”问题在于您的toggle 方法...

if (lights[row][col] = true) {

= 是一个分配,所以你说将true 分配给lights[row][col],如果lights[row][col] == true 则执行以下操作......所以它总是正确的。

一个更简单的方法是......

lights[row][col] = !lights[row][col];

不,if's or else's,很简单。

您还应该看看Painting in AWT and SwingPerforming Custom Painting。你违反了绘制方法链合约(没有调用super.paint

您应该使用paintComponent 而不是覆盖paint,并且您应该在进行任何自定义绘画之前调用super.paintComponent,这将确保Graphics 上下文为您做好了充分的准备

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多