[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / board / Board.java
index f251e13..ab4371f 100644 (file)
@@ -22,22 +22,29 @@ import java.awt.image.BufferedImage;
 import org.hedgecode.chess.position.Square;
 
 /**
- *
+ * Storage class for chess board images.
  *
  * @author Dmitry Samoshin aka gotty
  */
 public class Board {
 
+    private BufferedImage board;
     private SquarePair<BufferedImage> squares;
+
     private int squareSize;
 
-    Board(SquarePair<BufferedImage> squares) {
+    private 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);
     }
 
+    private Board(BufferedImage board) {
+        this.board = board;
+        this.squareSize = Math.max(board.getWidth(), board.getHeight()) / Square.getSize();
+    }
+
     public int squareSize() {
         return squareSize;
     }
@@ -46,7 +53,14 @@ public class Board {
         return squareSize * Square.getSize();
     }
 
-    public BufferedImage render() {
+    public BufferedImage getBoard() {
+        if (board == null && squares != null) {
+            board = renderBoard();
+        }
+        return board;
+    }
+
+    private BufferedImage renderBoard() {
         BufferedImage board = new BufferedImage(
                 squareSize * Square.getSize(),
                 squareSize * Square.getSize(),
@@ -68,6 +82,10 @@ public class Board {
         return board;
     }
 
+    public static Board create(BufferedImage board) {
+        return new Board(board);
+    }
+
     public static Board create(SquarePair<BufferedImage> squares) {
         return new Board(squares);
     }