如何使用 GridLayout 中的特定元素? [英] How to work with a specific element in GridLayout?

查看:38
本文介绍了如何使用 GridLayout 中的特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向 gridlayout 中的特定元素(网格?)添加图标时遇到问题.我的网格布局包含 64 个砖块",旨在用作棋盘.

I have a problem adding an icon to a spesific element(grid?) in gridlayout. My gridlayout contains 64 "bricks" which is intended to work as a chessboard.

我的网格代码如下所示:

My gridcode looks like this:

棋盘

public class SjakkBrett extends JFrame implements Config {

public ChessBoard() {

    setSize(800,800);
    setLayout(new GridLayout(BRICKS/ ROWS, 0) );

    for (int id = 0; id < BRICKS; id++)         
        add(new Brick(id)); 

    setVisible(true);

}

配置

public interface Config {
    public int ROWS= 8;
    public int BRICKS= 64;
}

我的问题是我似乎找不到将图标添加到板上特定砖块的方法,例如 setIcon(new ImageIcon("pawn.png")); 因为我不知道如何使用我正在制作的积木 ID.

My problem is that I can't seem to find a way to add icons to a specific brick in the board, such as setIcon(new ImageIcon("pawn.png")); because I don't know how to use the brick ID's I'm making.

有谁能帮帮我吗?

推荐答案

回答您的具体问题:

List<Brick> bricks = new ArrayList<Brick>();

public ChessBoard() {

    setSize(800,800);
    setLayout(new GridLayout(BRICKS/ ROWS, 0) );

    for (int id = 0; id < BRICKS; id++) {
        Brick brick = new Brick(id);        
        add(brick); 
        bricks.add(brick);
    }

    setVisible(true);

}

public void setBrick(int id, int piece) {
    bricks.get(id).setPiece(piece);
}

为了回答您未提出的问题,让我们想一想国际象棋游戏.

To answer your unasked questions, let's think about a game of chess for a bit.

棋盘已经有符号.典型的第一步是 e4.由于没有指定一块,这意味着一个棋子.唯一可以移动到 e4 的棋子是坐在 e2 上的棋子.因此,e4 是将 pawn 从 e2 移动到 e4"的简写方式.

A chess board already has a notation. A typical first move is e4. Since a piece is not specified, that means a pawn. The only pawn that can move to e4 is the pawn sitting on e2. So, e4 is a shorthand way of saying "move the pawn from e2 to e4".

因此,我们将砖块(正方形)排列成一块板.根据每块不同的规则,我们有能够从一块砖移到另一块的块.我们还有捕获规则和确定谁获胜的规则.

So, we have bricks (squares) that are arranged to make a board. We have pieces with the ability to move from one brick to another, according to rules that are different for each piece. We also have capture rules and rules for determining who wins.

所有这些元素都必须以对象或方法的形式出现在游戏中.

All of these elements have to be present in the game as either objects or methods.

那么,让我们来谈谈对象吧.

So, let's talk about objects.

我们有一块砖(正方形).我们有一组称为板的砖块.我们有碎片.

We have a brick (square). We have a collection of bricks called a board. We have pieces.

这些对象是相互关联的.它们的共同点是位置的概念.

These objects are interrelated. What they all have in common is the idea of location.

砖块位于特定位置 (e2).董事会需要知道如何将一个点 (e2) 转换为有意义的东西(第 1 行,第 4 列;假设第 0 行,第 0 列是左下角).一块需要知道它的位置(e2),它可以合法去哪里(e3,e4),以及它会去哪里(e4).

A brick is located in a specific spot (e2). A board needs to know how to translate a spot (e2) into something meaningful (row 1, column 4; assuming row 0, column 0 is the lower left corner). A piece needs to know where it's located (e2), where it can legally go (e3, e4), and where it will go (e4).

这应该足以让您入门.

这篇关于如何使用 GridLayout 中的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆