- public Image build(Position position, String type) {
- // BufferedImage
- // Image image = ImageIO.read(new URL(urlname));
- // Graphics.drawImage()
-
-/*
- Image image = ImageIO.read(new File("f://image1.jpg"));
-
- ImageIO.write(image, "JPEG", new File("f://image2.jpg"));
-*/
-
- return null;
+ public RenderedImage 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("Couldn't find image resources!"); // todo: locale
+ }
+
+ int squareSize = board.squareSize();
+
+ BufferedImage diagram = board.render();
+ Graphics diagramGraphics = diagram.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) {
+ diagramGraphics.drawImage(
+ pieces.get(colorPiece).getScaledInstance(
+ squareSize, squareSize, Image.SCALE_SMOOTH
+ ),
+ x * squareSize,
+ y * squareSize,
+ null
+ );
+ }
+ }
+ }
+ return diagram;