--- /dev/null
+/*
+ * 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;
+
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+
+import javax.imageio.ImageIO;
+
+import org.hedgecode.chess.img.board.Board;
+import org.hedgecode.chess.img.board.BoardLoader;
+import org.hedgecode.chess.img.piece.PieceSet;
+import org.hedgecode.chess.img.piece.PieceSetLoader;
+import org.hedgecode.chess.position.ColorPiece;
+import org.hedgecode.chess.position.Position;
+import org.hedgecode.chess.position.Positions;
+import org.hedgecode.chess.position.Square;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class DiagramBuilder implements ImageBuilder {
+
+ private static ImageBuilder _instance = new DiagramBuilder();
+
+ private BoardLoader boardLoader;
+ private PieceSetLoader pieceSetLoader;
+
+ private DiagramBuilder() {
+ boardLoader = new BoardLoader();
+ pieceSetLoader = new PieceSetLoader();
+ }
+
+ @Override
+ public Diagram build(Position position, String boardType, String pieceType)
+ throws ImageException
+ {
+ Map<Square, ColorPiece> squares = position.getSquares();
+
+ Board board = boardLoader.load(boardType);
+ PieceSet pieces = pieceSetLoader.load(pieceType);
+
+ if (board == null || pieces == null) {
+ throw new ImageException("image.unable.find.resource");
+ }
+
+ int squareSize = board.squareSize();
+
+ 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) {
+ imageGraphics.drawImage(
+ pieces.get(colorPiece).getScaledInstance(
+ squareSize, squareSize, Image.SCALE_SMOOTH
+ ),
+ x * squareSize,
+ y * squareSize,
+ null
+ );
+ }
+ }
+ }
+ return new Diagram(
+ position, image
+ );
+ }
+
+ public static ImageBuilder getInstance() {
+ return _instance;
+ }
+
+
+ public static void main(String[] args) throws IOException, ImageException {
+
+ ImageIO.write(
+ DiagramBuilder.getInstance().build(
+ Positions.INITIAL.getPosition(), "test", "shade"
+ ).getImage(),
+ ImageFormat.PNG.name(),
+ new File("chessboard" + "." + ImageFormat.PNG.getExt())
+ );
+
+/*
+ String[] formatNames;
+ Set<String> set = new HashSet<>();
+
+ formatNames = ImageIO.getReaderFormatNames();
+ for (String formatName : formatNames)
+ set.add(formatName.toLowerCase());
+ System.out.println("Supported read formats: " + set);
+
+ set.clear();
+
+ formatNames = ImageIO.getWriterFormatNames();
+ for (String formatName : formatNames)
+ set.add(formatName.toLowerCase());
+ System.out.println("Supported write formats: " + set);
+
+ set.clear();
+
+ formatNames = ImageIO.getReaderMIMETypes();
+ for (String formatName : formatNames)
+ set.add(formatName.toLowerCase());
+ System.out.println("Supported read MIME types: " + set);
+
+ set.clear();
+
+ formatNames = ImageIO.getWriterMIMETypes();
+ for (String formatName : formatNames)
+ set.add(formatName.toLowerCase());
+ System.out.println("Supported write MIME types: " + set);
+*/
+ }
+
+}