2 * Copyright (c) 2018-2019. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.img.board;
19 import java.awt.Graphics;
20 import java.awt.image.BufferedImage;
22 import org.hedgecode.chess.position.Square;
25 * Storage class for chess board images.
27 * @author Dmitry Samoshin aka gotty
31 private BufferedImage board;
32 private SquarePair<BufferedImage> squares;
34 private int squareSize;
36 private Board(SquarePair<BufferedImage> squares) {
37 this.squares = squares;
38 int width = Math.max(squares.getDark().getWidth(), squares.getLight().getWidth());
39 int height = Math.max(squares.getDark().getHeight(), squares.getLight().getHeight());
40 this.squareSize = Math.max(width, height);
43 private Board(BufferedImage board) {
45 this.squareSize = Math.max(board.getWidth(), board.getHeight()) / Square.getSize();
48 public int squareSize() {
52 public int boardSize() {
53 return squareSize * Square.getSize();
56 public BufferedImage getBoard() {
57 if (board == null && squares != null) {
58 board = renderBoard();
63 private BufferedImage renderBoard() {
64 BufferedImage board = new BufferedImage(
65 squareSize * Square.getSize(),
66 squareSize * Square.getSize(),
67 BufferedImage.TYPE_INT_ARGB
69 Graphics boardGraphics = board.getGraphics();
70 for (int y = 0; y < Square.getSize(); ++y) {
71 for (int x = 0; x < Square.getSize(); ++x) {
72 boardGraphics.drawImage(
85 public static Board create(BufferedImage board) {
86 return new Board(board);
89 public static Board create(SquarePair<BufferedImage> squares) {
90 return new Board(squares);