c059361b70ce5a4f02831440189b3616247c37fb
[chesshog.git] / src / main / java / org / hedgecode / chess / img / DiagramBuilder.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.img;
18
19 import java.awt.Graphics;
20 import java.awt.Image;
21 import java.awt.image.BufferedImage;
22 import java.awt.image.RenderedImage;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Map;
26
27 import javax.imageio.ImageIO;
28
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;
37
38 /**
39  *
40  *
41  * @author Dmitry Samoshin aka gotty
42  */
43 public class DiagramBuilder implements ImageBuilder {
44
45     private static ImageBuilder _instance = new DiagramBuilder();
46
47     private BoardLoader boardLoader;
48     private PieceSetLoader pieceSetLoader;
49
50     private DiagramBuilder() {
51         boardLoader = new BoardLoader();
52         pieceSetLoader = new PieceSetLoader();
53     }
54
55     @Override
56     public String build(Position position) {
57         return ""; // todo
58     }
59
60     @Override
61     public RenderedImage build(Position position, String boardType, String pieceType)
62             throws ImageException
63     {
64         Map<Square, ColorPiece> squares = position.getSquares();
65
66         Board board = boardLoader.load(boardType);
67         PieceSet pieces = pieceSetLoader.load(pieceType);
68
69         if (board == null || pieces == null) {
70             throw new ImageException("Couldn't find image resources!"); // todo: locale
71         }
72
73         int squareSize = board.squareSize();
74
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
85                             ),
86                             x * squareSize,
87                             y * squareSize,
88                             null
89                     );
90                 }
91             }
92         }
93         return diagram;
94     }
95
96     public static ImageBuilder getInstance() {
97         return _instance;
98     }
99
100
101     public static void main(String[] args) throws IOException, ImageException {
102
103         ImageIO.write(
104                 DiagramBuilder.getInstance().build(Positions.INITIAL.getPosition(), "test", "shade"),
105                 ImageFormat.PNG.name(),
106                 new File("chessboard" + "." + ImageFormat.PNG.getExt())
107         );
108
109 /*
110         String[] formatNames;
111         Set<String> set = new HashSet<>();
112
113         formatNames = ImageIO.getReaderFormatNames();
114         for (String formatName : formatNames)
115             set.add(formatName.toLowerCase());
116         System.out.println("Supported read formats: " + set);
117
118         set.clear();
119
120         formatNames = ImageIO.getWriterFormatNames();
121         for (String formatName : formatNames)
122             set.add(formatName.toLowerCase());
123         System.out.println("Supported write formats: " + set);
124
125         set.clear();
126
127         formatNames = ImageIO.getReaderMIMETypes();
128         for (String formatName : formatNames)
129             set.add(formatName.toLowerCase());
130         System.out.println("Supported read MIME types: " + set);
131
132         set.clear();
133
134         formatNames = ImageIO.getWriterMIMETypes();
135         for (String formatName : formatNames)
136             set.add(formatName.toLowerCase());
137         System.out.println("Supported write MIME types: " + set);
138 */
139     }
140
141 }