[LIB-9] Add functional for build image chess diagrams
[chesshog.git] / src / main / java / org / hedgecode / chess / img / board / Board.java
diff --git a/src/main/java/org/hedgecode/chess/img/board/Board.java b/src/main/java/org/hedgecode/chess/img/board/Board.java
new file mode 100644 (file)
index 0000000..f251e13
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2018-2019. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.img.board;
+
+import java.awt.Graphics;
+import java.awt.image.BufferedImage;
+
+import org.hedgecode.chess.position.Square;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class Board {
+
+    private SquarePair<BufferedImage> squares;
+    private int squareSize;
+
+    Board(SquarePair<BufferedImage> squares) {
+        this.squares = squares;
+        int width = Math.max(squares.getDark().getWidth(), squares.getLight().getWidth());
+        int height = Math.max(squares.getDark().getHeight(), squares.getLight().getHeight());
+        this.squareSize = Math.max(width, height);
+    }
+
+    public int squareSize() {
+        return squareSize;
+    }
+
+    public int boardSize() {
+        return squareSize * Square.getSize();
+    }
+
+    public BufferedImage render() {
+        BufferedImage board = new BufferedImage(
+                squareSize * Square.getSize(),
+                squareSize * Square.getSize(),
+                BufferedImage.TYPE_INT_ARGB
+        );
+        Graphics boardGraphics = board.getGraphics();
+        for (int y = 0; y < Square.getSize(); ++y) {
+            for (int x = 0; x < Square.getSize(); ++x) {
+                boardGraphics.drawImage(
+                        (x + y) % 2 == 0
+                                ? squares.getLight()
+                                : squares.getDark(),
+                        x * squareSize,
+                        y * squareSize,
+                        null
+                );
+            }
+        }
+        return board;
+    }
+
+    public static Board create(SquarePair<BufferedImage> squares) {
+        return new Board(squares);
+    }
+
+}