[LIB-9] Separate chesshog-hedgefish module
[chesshog.git] / 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  *
26  *
27  * @author Dmitry Samoshin aka gotty
28  */
29 public class Board {
30
31     private SquarePair<BufferedImage> squares;
32     private int squareSize;
33
34     Board(SquarePair<BufferedImage> squares) {
35         this.squares = squares;
36         int width = Math.max(squares.getDark().getWidth(), squares.getLight().getWidth());
37         int height = Math.max(squares.getDark().getHeight(), squares.getLight().getHeight());
38         this.squareSize = Math.max(width, height);
39     }
40
41     public int squareSize() {
42         return squareSize;
43     }
44
45     public int boardSize() {
46         return squareSize * Square.getSize();
47     }
48
49     public BufferedImage render() {
50         BufferedImage board = new BufferedImage(
51                 squareSize * Square.getSize(),
52                 squareSize * Square.getSize(),
53                 BufferedImage.TYPE_INT_ARGB
54         );
55         Graphics boardGraphics = board.getGraphics();
56         for (int y = 0; y < Square.getSize(); ++y) {
57             for (int x = 0; x < Square.getSize(); ++x) {
58                 boardGraphics.drawImage(
59                         (x + y) % 2 == 0
60                                 ? squares.getLight()
61                                 : squares.getDark(),
62                         x * squareSize,
63                         y * squareSize,
64                         null
65                 );
66             }
67         }
68         return board;
69     }
70
71     public static Board create(SquarePair<BufferedImage> squares) {
72         return new Board(squares);
73     }
74
75 }