[LIB-9] Separate chesshog-graphics module
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / DiagramBuilder.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018. Developed by Hedgecode.
+ * 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.
@@ -19,7 +19,6 @@ package org.hedgecode.chess.img;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.image.BufferedImage;
-import java.awt.image.RenderedImage;
 import java.io.File;
 import java.io.IOException;
 import java.util.Map;
@@ -53,12 +52,7 @@ public class DiagramBuilder implements ImageBuilder {
     }
 
     @Override
-    public String build(Position position) {
-        return ""; // todo
-    }
-
-    @Override
-    public RenderedImage build(Position position, String boardType, String pieceType)
+    public Diagram build(Position position, String boardType, String pieceType)
             throws ImageException
     {
         Map<Square, ColorPiece> squares = position.getSquares();
@@ -67,19 +61,19 @@ public class DiagramBuilder implements ImageBuilder {
         PieceSet pieces = pieceSetLoader.load(pieceType);
 
         if (board == null || pieces == null) {
-            throw new ImageException("Couldn't find image resources!"); // todo: locale
+            throw new ImageException("image.unable.find.resource");
         }
 
         int squareSize = board.squareSize();
 
-        BufferedImage diagram = board.render();
-        Graphics diagramGraphics = diagram.getGraphics();
+        BufferedImage image = board.render();
+        Graphics imageGraphics = image.getGraphics();
         for (int y = 0; y < Square.getSize(); ++y) {
             for (int x = 0; x < Square.getSize(); ++x) {
                 Square square = Square.getSquare(x, Square.getSize() - (y + 1));
                 ColorPiece colorPiece = squares.get(square);
                 if (colorPiece != null) {
-                    diagramGraphics.drawImage(
+                    imageGraphics.drawImage(
                             pieces.get(colorPiece).getScaledInstance(
                                     squareSize, squareSize, Image.SCALE_SMOOTH
                             ),
@@ -90,7 +84,9 @@ public class DiagramBuilder implements ImageBuilder {
                 }
             }
         }
-        return diagram;
+        return new Diagram(
+                position, image
+        );
     }
 
     public static ImageBuilder getInstance() {
@@ -101,7 +97,9 @@ public class DiagramBuilder implements ImageBuilder {
     public static void main(String[] args) throws IOException, ImageException {
 
         ImageIO.write(
-                DiagramBuilder.getInstance().build(Positions.INITIAL.getPosition(), "test", "shade"),
+                DiagramBuilder.getInstance().build(
+                        Positions.INITIAL.getPosition(), "test", "shade"
+                ).getImage(),
                 ImageFormat.PNG.name(),
                 new File("chessboard" + "." + ImageFormat.PNG.getExt())
         );