简体   繁体   English

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

[英]What's wrong with this chess board applet?

I tried to code a chess board with Java 7, in applet technology. 我试图用applet技术用Java 7编写棋盘。 But all I get is a big and single "white" cell board. 但是我得到的只是一块大而单一的“白色”电池板。 So what's wrong ? 那怎么了? Here are my two sources files : 这是我的两个源文件:

ChessWebLauncher.java 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 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;

}

Thanks in advance. 提前致谢。

You don't seem to call the paint() method. 您似乎没有调用paint()方法。

As far as I can see, you only use 据我所知,您只能使用

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

so you have a square of your adjusted size with a default white color. 因此您拥有一个已调整大小的正方形,默认为白色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM