[LIB-9] Fix some pom.xml dependencies
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / DiagramBuilder.java
1 /*
2  * Copyright (c) 2018-2019. 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.io.File;
23 import java.io.IOException;
24 import java.util.Map;
25
26 import javax.imageio.ImageIO;
27
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;
36
37 /**
38  *
39  *
40  * @author Dmitry Samoshin aka gotty
41  */
42 public class DiagramBuilder implements ImageBuilder {
43
44     private static ImageBuilder _instance = new DiagramBuilder();
45
46     private BoardLoader boardLoader;
47     private PieceSetLoader pieceSetLoader;
48
49     private DiagramBuilder() {
50         boardLoader = new BoardLoader();
51         pieceSetLoader = new PieceSetLoader();
52     }
53
54     @Override
55     public Diagram build(Position position, String boardType, String pieceType)
56             throws ImageException
57     {
58         Map<Square, ColorPiece> squares = position.getSquares();
59
60         Board board = boardLoader.load(boardType);
61         PieceSet pieces = pieceSetLoader.load(pieceType);
62
63         if (board == null || pieces == null) {
64             throw new ImageException("image.unable.find.resource");
65         }
66
67         int squareSize = board.squareSize();
68
69         BufferedImage image = board.render();
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                             ),
80                             x * squareSize,
81                             y * squareSize,
82                             null
83                     );
84                 }
85             }
86         }
87         return new Diagram(
88                 position, image
89         );
90     }
91
92     public static ImageBuilder getInstance() {
93         return _instance;
94     }
95
96
97     public static void main(String[] args) throws IOException, ImageException {
98
99         ImageIO.write(
100                 DiagramBuilder.getInstance().build(
101                         Positions.INITIAL.getPosition(), "test", "shade"
102                 ).getImage(),
103                 ImageFormat.PNG.name(),
104                 new File("chessboard" + "." + ImageFormat.PNG.getExt())
105         );
106
107 /*
108         String[] formatNames;
109         Set<String> set = new HashSet<>();
110
111         formatNames = ImageIO.getReaderFormatNames();
112         for (String formatName : formatNames)
113             set.add(formatName.toLowerCase());
114         System.out.println("Supported read formats: " + set);
115
116         set.clear();
117
118         formatNames = ImageIO.getWriterFormatNames();
119         for (String formatName : formatNames)
120             set.add(formatName.toLowerCase());
121         System.out.println("Supported write formats: " + set);
122
123         set.clear();
124
125         formatNames = ImageIO.getReaderMIMETypes();
126         for (String formatName : formatNames)
127             set.add(formatName.toLowerCase());
128         System.out.println("Supported read MIME types: " + set);
129
130         set.clear();
131
132         formatNames = ImageIO.getWriterMIMETypes();
133         for (String formatName : formatNames)
134             set.add(formatName.toLowerCase());
135         System.out.println("Supported write MIME types: " + set);
136 */
137     }
138
139 }