0

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

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;

}

提前致谢。

4

1 回答 1

1

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

据我所知,你只使用

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

所以你有一个调整大小的正方形,默认为白色。

于 2012-11-03T12:45:46.510 回答