2 * Copyright (c) 2018. 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;
22 import java.awt.image.RenderedImage;
24 import java.io.IOException;
27 import javax.imageio.ImageIO;
29 import org.hedgecode.chess.img.board.Board;
30 import org.hedgecode.chess.img.board.BoardLoader;
31 import org.hedgecode.chess.img.piece.PieceSet;
32 import org.hedgecode.chess.img.piece.PieceSetLoader;
33 import org.hedgecode.chess.position.ColorPiece;
34 import org.hedgecode.chess.position.Position;
35 import org.hedgecode.chess.position.Positions;
36 import org.hedgecode.chess.position.Square;
41 * @author Dmitry Samoshin aka gotty
43 public class DiagramBuilder implements ImageBuilder {
45 private static ImageBuilder _instance = new DiagramBuilder();
47 private BoardLoader boardLoader;
48 private PieceSetLoader pieceSetLoader;
50 private DiagramBuilder() {
51 boardLoader = new BoardLoader();
52 pieceSetLoader = new PieceSetLoader();
56 public String build(Position position) {
61 public RenderedImage build(Position position, String boardType, String pieceType)
64 Map<Square, ColorPiece> squares = position.getSquares();
66 Board board = boardLoader.load(boardType);
67 PieceSet pieces = pieceSetLoader.load(pieceType);
69 if (board == null || pieces == null) {
70 throw new ImageException("Couldn't find image resources!"); // todo: locale
73 int squareSize = board.squareSize();
75 BufferedImage diagram = board.render();
76 Graphics diagramGraphics = diagram.getGraphics();
77 for (int y = 0; y < Square.getSize(); ++y) {
78 for (int x = 0; x < Square.getSize(); ++x) {
79 Square square = Square.getSquare(x, Square.getSize() - (y + 1));
80 ColorPiece colorPiece = squares.get(square);
81 if (colorPiece != null) {
82 diagramGraphics.drawImage(
83 pieces.get(colorPiece).getScaledInstance(
84 squareSize, squareSize, Image.SCALE_SMOOTH
96 public static ImageBuilder getInstance() {
101 public static void main(String[] args) throws IOException, ImageException {
104 DiagramBuilder.getInstance().build(Positions.INITIAL.getPosition(), "test", "shade"),
105 ImageFormat.PNG.name(),
106 new File("chessboard" + "." + ImageFormat.PNG.getExt())
110 String[] formatNames;
111 Set<String> set = new HashSet<>();
113 formatNames = ImageIO.getReaderFormatNames();
114 for (String formatName : formatNames)
115 set.add(formatName.toLowerCase());
116 System.out.println("Supported read formats: " + set);
120 formatNames = ImageIO.getWriterFormatNames();
121 for (String formatName : formatNames)
122 set.add(formatName.toLowerCase());
123 System.out.println("Supported write formats: " + set);
127 formatNames = ImageIO.getReaderMIMETypes();
128 for (String formatName : formatNames)
129 set.add(formatName.toLowerCase());
130 System.out.println("Supported read MIME types: " + set);
134 formatNames = ImageIO.getWriterMIMETypes();
135 for (String formatName : formatNames)
136 set.add(formatName.toLowerCase());
137 System.out.println("Supported write MIME types: " + set);