2 * Copyright (c) 2018-2019. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.img;
19 import java.awt.Graphics;
20 import java.awt.Image;
21 import java.awt.image.BufferedImage;
23 import java.io.IOException;
26 import javax.imageio.ImageIO;
28 import org.hedgecode.chess.img.board.Board;
29 import org.hedgecode.chess.img.board.BoardLoader;
30 import org.hedgecode.chess.img.piece.PieceSet;
31 import org.hedgecode.chess.img.piece.PieceSetLoader;
32 import org.hedgecode.chess.position.ColorPiece;
33 import org.hedgecode.chess.position.Position;
34 import org.hedgecode.chess.position.Positions;
35 import org.hedgecode.chess.position.Square;
38 * Builder for chess diagrams.
40 * @author Dmitry Samoshin aka gotty
42 public class DiagramBuilder implements ImageBuilder {
44 private static ImageBuilder _instance = new DiagramBuilder();
46 private BoardLoader boardLoader;
47 private PieceSetLoader pieceSetLoader;
49 private DiagramBuilder() {
50 boardLoader = new BoardLoader();
51 pieceSetLoader = new PieceSetLoader();
55 public Diagram build(Position position, String boardType, String pieceType)
58 Map<Square, ColorPiece> squares = position.getSquares();
60 Board board = boardLoader.load(boardType);
61 PieceSet pieces = pieceSetLoader.load(pieceType);
63 if (board == null || pieces == null) {
64 throw new ImageException("image.unable.find.resource");
67 int squareSize = board.squareSize();
69 BufferedImage image = board.getBoard();
70 Graphics imageGraphics = image.getGraphics();
71 for (int y = 0; y < Square.getSize(); ++y) {
72 for (int x = 0; x < Square.getSize(); ++x) {
73 Square square = Square.getSquare(x, Square.getSize() - (y + 1));
74 ColorPiece colorPiece = squares.get(square);
75 if (colorPiece != null) {
76 imageGraphics.drawImage(
77 pieces.get(colorPiece).getScaledInstance(
78 squareSize, squareSize, Image.SCALE_SMOOTH
79 ), // todo: scale in other place
92 public static ImageBuilder getInstance() {
97 public static void main(String[] args) throws IOException, ImageException {
100 DiagramBuilder.getInstance().build(
101 Positions.INITIAL.getPosition(), "test", "chess24"
103 ImageFormat.PNG.name(),
104 new File("chessboard" + "." + ImageFormat.PNG.getExt())
108 String[] formatNames;
109 Set<String> set = new HashSet<>();
111 formatNames = ImageIO.getReaderFormatNames();
112 for (String formatName : formatNames)
113 set.add(formatName.toLowerCase());
114 System.out.println("Supported read formats: " + set);
118 formatNames = ImageIO.getWriterFormatNames();
119 for (String formatName : formatNames)
120 set.add(formatName.toLowerCase());
121 System.out.println("Supported write formats: " + set);
125 formatNames = ImageIO.getReaderMIMETypes();
126 for (String formatName : formatNames)
127 set.add(formatName.toLowerCase());
128 System.out.println("Supported read MIME types: " + set);
132 formatNames = ImageIO.getWriterMIMETypes();
133 for (String formatName : formatNames)
134 set.add(formatName.toLowerCase());
135 System.out.println("Supported write MIME types: " + set);