Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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_CELLS_SIZE_PX, DEFAULT_CELLS_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 : requestedHeight;
- setPreferredSize(new Dimension(currentCellsSizePx * 8, currentCellsSizePx * 8));
- }
- /**
- * Current cells size (in pixels).
- */
- private int currentCellsSizePx;
- /**
- * Default cells size (in pixels).
- */
- private static final int DEFAULT_CELLS_SIZE_PX = 42;
- /**
- * 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(0xDF7401);
- /**
- * Multi-threading security code.
- */
- private static final long serialVersionUID = 7321516321051309085L;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement