gpt4 book ai didi

java - 这个棋盘小程序有什么问题?

转载 作者:行者123 更新时间:2023-12-01 15:04:09 26 4
gpt4 key购买 nike

我尝试使用 Java 7 小程序技术编写棋盘代码。但我得到的只是一 block 大而单一的“白色”单元板。那么出了什么问题呢?这是我的两个源文件:

ChessWebLauncher.java

/*
* This file is part of ChessWeb.

ChessWeb is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ChessWeb is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ChessWeb. If not, see <http://www.gnu.org/licenses/>
*/
package fr.loloof64.java_applet.chess_web.main;

import java.applet.Applet;

import fr.loloof64.java_applet.chess_web.views.BoardCanvas;

/**
* The application applet.
* @author laurent-bernabe
*
*/
public class ChessWebLaucher extends Applet {

@Override
public void init() {
/*
* Here I add the board canvas.
*/
add(boardCanvas);
}

private BoardCanvas boardCanvas = new BoardCanvas(this);

/**
* Multi-threading security id.
*/
private static final long serialVersionUID = -7638783779678653225L;

}

BoardCanvas.java

/*
* This file is part of ChessWeb.

ChessWeb is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ChessWeb is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ChessWeb. If not, see <http://www.gnu.org/licenses/>
*/

package fr.loloof64.java_applet.chess_web.views;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.Panel;

public class BoardCanvas extends Panel {

/**
* Simple constructor accepting a parent Container (java.awt)
* @param parent - {@link Container} - the parent Container.
*/
public BoardCanvas(Container parent) {
adjustSize(new Dimension(DEFAULT_SIZE_PX, DEFAULT_SIZE_PX));
}

/**
* Adjust size with the least value (between width and eight).
* @param parent - {@link Container} - the parent Container.
* @param layout - {@link LayoutManager} - the layoutmanager.
*/
public BoardCanvas(Container parent, LayoutManager layout) {
super(layout);
Dimension requestedDimensions = layout.preferredLayoutSize(parent);
adjustSize(new Dimension(requestedDimensions.width, requestedDimensions.height));
}

@Override
public void paint(Graphics graphics) {
for (int rankFromTopIndex = 0; rankFromTopIndex < 8; rankFromTopIndex++){
for (int fileFromLeftIndex = 0; fileFromLeftIndex < 8; fileFromLeftIndex++){
Color cellColor = ((rankFromTopIndex+fileFromLeftIndex) % 2) == 0 ?
WHITE_CELL_COLOR : BLACK_CELL_COLOR;

int xAbsCoordinate = currentCellsSizePx * fileFromLeftIndex;
int yAbsCoordinate = currentCellsSizePx * rankFromTopIndex;

/*
* Finally paints the current (loop) cell.
*/
graphics.setColor(cellColor);
graphics.fillRect(xAbsCoordinate, yAbsCoordinate, currentCellsSizePx, currentCellsSizePx);
}
}
}

private void adjustSize(Dimension requestedDimensions){
int requestedWidth = requestedDimensions.width;
int requestedHeight = requestedDimensions.height;
currentCellsSizePx = requestedWidth > requestedHeight ? requestedWidth : reqestedHeight;
setPreferredSize(new Dimension(currentCellsSizePx, currentCellsSizePx));
}

/**
* Current cells size (in pixels).
*/
private int currentCellsSizePx;

/**
* Default cells size (in pixels).
*/
private static final int DEFAULT_SIZE_PX = 256;

/**
* White cells colors.
*/
private static final Color WHITE_CELL_COLOR = new Color(0xE9E441);

/**
* Black cells colors.
*/
private static final Color BLACK_CELL_COLOR = new Color(0xEBAD39);

/**
* Multi-threading security code.
*/
private static final long serialVersionUID = 7321516321051309085L;

}

提前致谢。

最佳答案

您似乎没有调用paint()方法。

据我所知,您只使用

public BoardCanvas(Container parent) {
adjustSize(new Dimension(DEFAULT_SIZE_PX, DEFAULT_SIZE_PX));
}

这样你就有了一个调整后大小的正方形,默认为白色。

关于java - 这个棋盘小程序有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13209114/

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