【问题标题】:How do I move chess piece in java program如何在java程序中移动棋子
【发布时间】:2013-11-11 11:14:50
【问题描述】:

我目前必须制作一个四方棋盘,并在棋盘上沿上、下、左、右、右上图、左上图、右下图和左下图的方向移动棋子。我已经布置好了电路板和按钮,但我对移动这件作品感到困惑。 我只需要移动一件。如果我不能正确移动另一个任务,我会感到困惑,我该如何找到它?

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

public class javaAssignment extends JFrame implements ActionListener {
    JPanel top, bottom, panel1, panel2, panel3, panel4, buttons;
    JButton up, down, right, left, lud, rud, rdd, ldd;
    JLabel l1, l2, l3, l4;

    javaAssignment() {

        Container c = getContentPane();

        // Import image.

        Icon chess = new ImageIcon("images/piece.png");

        // Create panels.

        top = new JPanel();
        bottom = new JPanel();
        buttons = new JPanel();

        // Create chess board.

        l1 = new JLabel(chess);
        l2 = new JLabel();
        l3 = new JLabel();
        l4 = new JLabel();

        panel1 = new JPanel();
        panel1.setBackground(Color.black);
        panel1.setOpaque(true);
        panel1.setPreferredSize(new Dimension(90, 90));
        panel2 = new JPanel();
        panel2.setBackground(Color.white);
        panel2.setOpaque(true);
        panel2.setPreferredSize(new Dimension(90, 90));
        panel3 = new JPanel();
        panel3.setBackground(Color.white);
        panel3.setOpaque(true);
        panel3.setPreferredSize(new Dimension(90, 90));
        panel4 = new JPanel();
        panel4.setBackground(Color.black);
        panel4.setOpaque(true);
        panel4.setPreferredSize(new Dimension(90, 90));

        // Create buttons.

        up = new JButton("Up");
        down = new JButton("Down");
        right = new JButton("Right");
        left = new JButton("Left");
        lud = new JButton("Left Up Diag");
        ldd = new JButton("Left Down Diag");
        rud = new JButton("Right Up Diag");
        rdd = new JButton("Right Down Diag");

        // Add panels and buttons.

        panel1.add(l1);
        panel2.add(l2);
        panel3.add(l3);
        panel4.add(l4);
        top.add(panel1);
        top.add(panel2);
        bottom.add(panel3);
        bottom.add(panel4);
        buttons.add(up);
        buttons.add(down);
        buttons.add(left);
        buttons.add(right);
        buttons.add(lud);
        buttons.add(ldd);
        buttons.add(rud);
        buttons.add(rdd);
        c.add(top);
        c.add(bottom);
        c.add(buttons);

        // Set Layouts.

        top.setLayout(new GridLayout(2,3));
        bottom.setLayout(new GridLayout(2,3));
        buttons.setLayout(new GridLayout(2,3));
        c.setLayout(new FlowLayout());

        // Display frame.

        setVisible(true);
        setSize(600, 300);

    }

    public static void main(String[] args) {
        javaAssignment output = new javaAssignment();
    }

    public void actionPerformed(ActionEvent e) {

    }

}

【问题讨论】:

  • 你的董事会在哪里?即碎片在哪家商店?
  • 我在 JPanel 内的 JLabel 中有一个图像,其中包含一个图像。我只需要按照上面列出的方向移动那块。
  • 创建一个板,例如。 2*2 阵列 od 片,然后用它来驱动显示器。
  • 另见examplevariation

标签: java swing awt chess


【解决方案1】:

您可以创建一个二维字符数组(字符,因为您可以将差异字符用于不同的数字)并将每个字符设置为“e”,其中国际象棋字段为空,否则您有一个“k”,例如国王(只是示例)。

使用 2 个 for 循环,您可以检查数组中的每个字段是否有“棋子”,因此如果国王有“k”,您可以在该位置绘制国王(您通过

 x = i * square_size;
 y = j * square_size;

当你的 vars 是 i 和 j)。

当你想移动棋子时,你必须检查棋子是否还在你的数组边界内(你必须使数组和你的棋场一样大)

如需更多帮助,我需要更多详细信息

【讨论】:

  • 好的,所以如果我创建一个 [1][1] 数组并从 [0][0] 开始我的作品,这是一个 4 方板的左上角,那么当有人点击方向 我去我的数组并检查移动是否可用,例如他们不能移动到 [2][2] 因为它超出了界限。我选对了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多