我正在嘗試制作一個棋盤,它將在整個棋盤上隨機分配棋子的位置。
以下是我到目前為止所擁有的
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class ChessBoard extends JFrame {
JLayeredPane layeredpane;
JPanel chessboard;
JButton[][] chessboardButtons;
Color black;
JLabel [][] chessboardLabels;
UIManager Ui;
ChessBoard() {
Dimension Size = new Dimension(600, 600);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setResizable(true);
setLocationRelativeTo(null);
setVisible(true);
setSize(600, 600);
setTitle("Chess Board");
layeredpane = new JLayeredPane();
getContentPane().add(layeredpane);
layeredpane.setPreferredSize(Size);
chessboard = new JPanel();
layeredpane.add(chessboard, JLayeredPane.DEFAULT_LAYER);
chessboard.setLayout(new GridLayout(8, 8));
chessboard.setPreferredSize(Size);
chessboard.setBounds(0, 0, Size.width, Size.height);
Ui = new UIManager();
chessboardButtons = new JButton[8][8];
black = Color.black;
ButtonHandler handler = new ButtonHandler();
for (int i = 0; i < 8; i ) {
for (int j = 0; j < 8; j ) {
chessboardButtons[i][j] = new JButton();
chessboardButtons[i][j].setBorderPainted(false);
if ((i j) % 2 != 0) {
chessboardButtons[i][j].setBackground(black);
chessboardButtons[i][j].setOpaque(true);
}
chessboard.add(chessboardButtons[i][j]);
chessboardButtons[i][j].addActionListener(handler);
}
}
chessboardLabels = new JLabel[8][8];
for (int i = 0; i < 8; i ) {
for (int j = 0; j < 8; j ) {
chessboardLabels[i][j] = new JLabel();
chessboardLabels[i][j].setFont(new Font("Ariel", Font.BOLD, 20));
chessboardLabels[i][j].setText("H");
chessboardLabels[i][j].setHorizontalAlignment(JLabel.CENTER);
chessboardLabels[i][j].setVerticalAlignment(JLabel.CENTER);
chessboardLabels[i][j].setOpaque(true);
chessboardButtons[i][j].add(chessboardLabels[i][j]);
if(chessboardButtons[i][j].getBackground() == Color.black) {
chessboardLabels[i][j].setBackground(Color.black);
chessboardLabels[i][j].setForeground(Color.white);
}
}
}
}
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == chessboardButtons[0][0]) {
System.out.println("Button 0,0");
}
if (e.getSource() == chessboardButtons[0][1]) {
System.out.println("Button 0,1");
}
}
}
}
現在,我的所有按鈕上都有字母 H。我需要它做的是:
- 將板上“H”的數量限制為 32、16 個“白色”和 16 個“黑色”,并且
- 隨機放置整個板子。
任何想法都會有所幫助!
uj5u.com熱心網友回復:
我幾年前就嘗試過,但遇到了幾個問題。最大的一個問題是將一塊從一個方塊 (JComponent) 拖到另一個方塊很難,因為每個 JComponent 都有自己的邊界,而它的 Graphics 剪輯會阻止您在這些邊界之外進行繪制。有一些解決方法,例如在 JLayeredPane 中將影像添加到更高級別,但這仍然很難做到正確。
你真的想讓你的 GUI 成為一個自定義的 JComponent/JPanel,它可以繪制整個板并可以在整個板上獲取滑鼠事件。
但在此之前,正確的起點是創建一個封裝游戲邏輯的 ChessModel。如果您首先這樣做并對其進行徹底測驗,那么在其上添加 GUI 比其他方式更容易。
public class ChessModel {
char[][] board = new char[8][8];
...
public Point[] getPossibleMoves(Point pieceLocation) {
...
}
}
uj5u.com熱心網友回復:
- 創建一個 ArrayList,其中包含 64 個整數,編號為 0 - 63
- 使用 Collections.shuffle(...) 隨機打亂數字
- 從 ArrayList 中取出前 16 個值,并根據整數值將白塊添加到板上。
- 從 ArrayList 中取出接下來的 16 個值并將黑色塊添加到板上。
uj5u.com熱心網友回復:
首先,我將從解耦系統的各個部分開始,這將為您提供更大的靈活性。
例如,您想將“視覺”與“虛擬”分離。組件不是在“網格”中管理,而是在一個串列中維護,因此,您需要某種方式可以快速輕松地確定螢屏上各種組件的位置以及它們與“虛擬”概念的關系游戲或網格。
這是“模型-視圖-控制器”的核心概念,其中模型代表“碎片”的“網格”,視圖用于向用戶直觀地表示模型。所以你最終會進行一些翻譯。
現在,你可以做一些像......
int row = (value / 8);
int col = (value % 8);
給定一個組件索引,它會給你它們所代表的行/列,但我也很懶惰??,所以,我將孤立...的概念Piece。
public class Piece extends JPanel {
private JLabel label;
private Point cell;
public Piece(int index) {
setLayout(new GridBagLayout());
label = new JLabel(Integer.toString(index));
label.setForeground(Color.RED);
add(label);
setOpaque(false);
}
public void setCell(Point cell) {
this.cell = cell;
}
public Point getCell() {
return cell;
}
}
這對我做了幾件事,首先,它給了我一個簡單的構建塊,可用于表示一段資料并維護該段的“虛擬”位置,因此我可以輕松地獨立查找它。
它還使板與板分離,這將使其更易于維護(恕我直言)。
接下來,我構建板...
setLayout(new GridLayout(8, 8));
int index = 0;
for (int row = 0; row < 8; row ) {
for (int col = 0; col < 8; col ) {
JPanel panel = new JPanel(new GridBagLayout()) {
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
// Indexed via x/y
cells[col][row] = panel;
if (index % 2 == 0) {
panel.setBackground(Color.WHITE);
} else {
panel.setBackground(Color.BLACK);
}
add(panel);
index ;
}
index ;
}
這里沒什么特別的,只是上面GridLayout放了一堆顏色面板。
“花哨”的部分是這樣的想法,而不是使用復雜的東西,比如JLayeredPane,我只是將Pieces 直接添加到每個單元格。
這將我們引向了問題的核心,即如何隨機化單元格的位置。本質上,我將創建一個從 0 到 63 的數字串列,隨機化串列,然后從串列中彈出每個數字,直到我完成。
現在,您可以使用陣列,但是用亂數填充陣列并不是一項簡單的任務(特別是如果您想保證唯一性 ??)
// Fill a list of numbers
int totalCells = 8 * 8;
List<Integer> locations = new ArrayList<>(totalCells);
for (int value = 0; value < totalCells; value ) {
locations.add(value);
}
// Randomise the list
Collections.shuffle(locations);
// For al the white pieces, randomise their positions
for (index = 0; index < 16; index ) {
int value = locations.remove(0);
// Virtual coordinates
int row = (value / 8);
int col = (value % 8);
Point cell = new Point(col, row);
Piece piece = new Piece(index);
whitePieces[index] = piece;
piece.setCell(cell);
// Get the component offset by the value (physical)
JPanel cellPane = (JPanel) getComponent(value);
cellPane.add(piece);
}
// Now you can continue with the black pieces, just like above
// and because you've removed the "used" cell indexes from the
// list, you won't end up with duplicate positions
現在你可能正在為這一切而撓頭。但簡單地說,當你想移動一個“塊”時,你只需將它從它當前的父容器中移除,計算位置 ( (row * 8) col),獲取新的父組件(如上)并添加它,很簡單。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JPanel[][] cells = new JPanel[8][8];
private Piece[] whitePieces = new Piece[16];
public TestPane() {
setLayout(new GridLayout(8, 8));
int index = 0;
for (int row = 0; row < 8; row ) {
for (int col = 0; col < 8; col ) {
JPanel panel = new JPanel(new GridBagLayout()) {
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
// Indexed via x/y
cells[col][row] = panel;
if (index % 2 == 0) {
panel.setBackground(Color.WHITE);
} else {
panel.setBackground(Color.BLACK);
}
add(panel);
index ;
}
index ;
}
int totalCells = 8 * 8;
List<Integer> locations = new ArrayList<>(totalCells);
for (int value = 0; value < totalCells; value ) {
locations.add(value);
}
Collections.shuffle(locations);
for (index = 0; index < 16; index ) {
int value = locations.remove(0);
int row = (value / 8);
int col = (value % 8);
Point cell = new Point(col, row);
Piece piece = new Piece(index);
whitePieces[index] = piece;
piece.setCell(cell);
JPanel cellPane = (JPanel) getComponent(value);
cellPane.add(piece);
}
}
}
public class Piece extends JPanel {
private JLabel label;
private Point cell;
public Piece(int index) {
setLayout(new GridBagLayout());
label = new JLabel(Integer.toString(index));
label.setForeground(Color.RED);
add(label);
setOpaque(false);
}
public void setCell(Point cell) {
this.cell = cell;
}
public Point getCell() {
return cell;
}
}
}
哦,以防萬一它很重要,您也可以使用基于組件的拖放
- 如何使用 ImageIcon 制作可拖動組件
- JLayeredPanel 布局管理器自由移動物件
但是,需要驅動想要??
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385141.html
