gpt4 book ai didi

java - 棋盘游戏的 GUI 操作

转载 作者:行者123 更新时间:2023-12-02 10:31:22 25 4
gpt4 key购买 nike

我对 Java 完全陌生,我想用 Java 创建一个单人棋盘游戏。

我已经有许多定义游戏的类,但是,对于我的问题,我将问题限制为为我的棋盘游戏创建 GUI 板。
我非常具体的问题是,基于下面的代码:

  1. 如何在棋盘游戏中移动玩家? (玩家类存在)
  2. 这是创建带有 GUI 的 Java 板的正确方法吗?有更好的替代方法吗?
  3. 如何仅将棋盘游戏的特定部分设置为可见? (当玩家移动时,这需要改变!)

提前感谢您的体面回答。因为我对java很陌生,所以请清楚地解释你的答案。

这是下面代码的输出:

output of the code

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;

public class SwingPaintDemo3 {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

private static void createAndShowGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Single Player Game");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.pack();
f.setVisible(true);
}
}

class MyPanel extends JPanel {


private int squareX = 50;
private int squareY = 50;
private int squareW = 20;
private int squareH = 20;

public MyPanel() {

setBorder(BorderFactory.createLineBorder(Color.black));


public Dimension getPreferredSize() {
return new Dimension(250,200);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("This is the game board ",10,20);
g.setColor(Color.GREEN);
g.fillRect(squareX,squareY,squareW,squareH);
g.setColor(Color.BLACK);
g.drawRect(squareX,squareY,squareW,squareH);
g.drawRect(squareX+20,squareY,squareW,squareH);
g.drawRect(squareX+40,squareY,squareW,squareH);
g.drawRect(squareX+60,squareY,squareW,squareH);
g.drawRect(squareX+60,squareY,squareW,squareH);
g.drawRect(squareX+80,squareY,squareW,squareH);
g.drawRect(squareX,squareY+20,squareW,squareH);
g.drawRect(squareX+20,squareY+20,squareW,squareH);
g.drawRect(squareX+40,squareY+20,squareW,squareH);
g.drawRect(squareX+60,squareY+20,squareW,squareH);
g.drawRect(squareX+80,squareY+20,squareW,squareH);
g.drawRect(squareX,squareY+40,squareW,squareH);
g.drawRect(squareX+20,squareY+40,squareW,squareH);
g.drawRect(squareX+40,squareY+40,squareW,squareH);
g.drawRect(squareX+60,squareY+40,squareW,squareH);
g.drawRect(squareX+80,squareY+40,squareW,squareH);

}
}

最佳答案

以下是mcve使用模型- View - Controller 模式演示游戏板和玩家的非常基本的实现。
此模式中的模型包含 View 所需的所有信息(“我已经有一个单独的类“Board.Java”,其中包含棋盘游戏的矩阵表示,包括坐标”是一个模型)。
View 就是这样, Controller 链接 View 和模型,并编排显示:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GameControler {

private Model model;
private View view;

public GameControler() {

model = new Model();
view = new View(model);
view.getButton().addActionListener(e-> movePlayer());
}

//move player to a random position for demo purpose only
private void movePlayer() {
final Random rnd = new Random();
model.setPlayerX(rnd.nextInt(100));//arbitrary limit which maybe outside bounds
model.setPlayerY(rnd.nextInt(100));//arbitrary limit which maybe outside bounds
view.refresh();
}

public static void main(String[] args) {
new GameControler();
}
}

class View {

private final static int GAP = 2;
Model model;
private MainPanel mainPanel;

View(Model model){
this.model = model;
createAndShowGUI();
}

void refresh() {
mainPanel.repaint();
}

private void createAndShowGUI() {
JFrame f = new JFrame("Single Player Game");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new MainPanel();
f.add(mainPanel);
f.pack();
f.setVisible(true);
}

JButton getButton() { return mainPanel.getButton(); }

class MainPanel extends JPanel {

private BottomPanel bPanel;

MainPanel() {
setLayout(new BorderLayout(GAP,GAP));
add(new TopPanel(), BorderLayout.PAGE_START);
add(new BoardPanel(), BorderLayout.CENTER);
bPanel = new BottomPanel();
add(bPanel, BorderLayout.PAGE_END);
}

JButton getButton() { return bPanel.getButton(); }
}

class TopPanel extends JPanel {
TopPanel() {
setLayout(new FlowLayout(FlowLayout.LEADING));
add(new JLabel("This is the game board "));
}
}

class BoardPanel extends JPanel {

Player player;

BoardPanel() {

setBorder(BorderFactory.createLineBorder(Color.BLACK, GAP));
GridLayout layout = new GridLayout(model.getBoardRows(),
model.getBoardCols());
setLayout(layout);

for (int i = 0; i <model.getBoardRows(); i++) {

for (int j = 0; j < model.getBoardCols(); j++) {
add(new Tile());
}
}

player = new Player();
player.setBounds(new Rectangle(100,100, 100, 100));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
player.paint(g);
}
}

class Tile extends JLabel {

Tile() {
setPreferredSize(new Dimension(model.getSquareSize(), model.getSquareSize()));
setBorder(BorderFactory.createLineBorder(Color.BLACK, GAP));
}
}

class Player extends JLabel{

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(model.getPlayerX(), model.getPlayerY(), model.getPlayerSize(),model.getPlayerSize());
}
}

class BottomPanel extends JPanel {

JButton button = new JButton("Move Player");

BottomPanel(){
add(button);
}

JButton getButton() { return button; }
}
}

class Model{

private int boardRows = 3, boardCols = 5, squareSize = 50;
private int playerX = 0, playerY = 0, playerSize =15;

int getPlayerX() { return playerX; }

void setPlayerX(int playerX) { this.playerX = playerX; }

int getPlayerY() {return playerY; }

void setPlayerY(int playerY) { this.playerY = playerY; }

int getPlayerSize() {return playerSize; }

int getBoardRows() {return boardRows; }

int getBoardCols() {return boardCols; }

int getSquareSize() {return squareSize; }
}

enter image description here

可以将代码复制粘贴到一个文件 (GameControler.java) 中并运行。请检查并根据您的需求进行修改,并询问您需要澄清的地方。

关于java - 棋盘游戏的 GUI 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53593173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com