[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / board / Board.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.img.board;
18
19 import java.awt.Graphics;
20 import java.awt.image.BufferedImage;
21
22 import org.hedgecode.chess.position.Square;
23
24 /**
25  * Storage class for chess board images.
26  *
27  * @author Dmitry Samoshin aka gotty
28  */
29 public class Board {
30
31     private BufferedImage board;
32     private SquarePair<BufferedImage> squares;
33
34     private int squareSize;
35
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);
41     }
42
43     private Board(BufferedImage board) {
44         this.board = board;
45         this.squareSize = Math.max(board.getWidth(), board.getHeight()) / Square.getSize();
46     }
47
48     public int squareSize() {
49         return squareSize;
50     }
51
52     public int boardSize() {
53         return squareSize * Square.getSize();
54     }
55
56     public BufferedImage getBoard() {
57         if (board == null && squares != null) {
58             board = renderBoard();
59         }
60         return board;
61     }
62
63     private BufferedImage renderBoard() {
64         BufferedImage board = new BufferedImage(
65                 squareSize * Square.getSize(),
66                 squareSize * Square.getSize(),
67                 BufferedImage.TYPE_INT_ARGB
68         );
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(
73                         (x + y) % 2 == 0
74                                 ? squares.getLight()
75                                 : squares.getDark(),
76                         x * squareSize,
77                         y * squareSize,
78                         null
79                 );
80             }
81         }
82         return board;
83     }
84
85     public static Board create(BufferedImage board) {
86         return new Board(board);
87     }
88
89     public static Board create(SquarePair<BufferedImage> squares) {
90         return new Board(squares);
91     }
92
93 }