1 /** 2 * The chess framework library. 3 * More information is available at http://www.jinchess.com/. 4 * Copyright (C) 2002 Alexander Maryanovsky. 5 * All rights reserved. 6 * 7 * The chess framework library is free software; you can redistribute 8 * it and/or modify it under the terms of the GNU Lesser General Public License 9 * as published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * The chess framework library is distributed in the hope that it will 13 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with the chess framework library; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 package free.chess; 23 24 import java.awt.*; 25 import free.chess.Piece; 26 import free.chess.ChessPiece; 27 28 29 /** 30 * An abstract class offering a convenient partial implementation for vector 31 * based piece sets. 32 */ 33 34 public abstract class VectorPiecePainter extends AbstractColoredPiecePainter{ 35 36 37 38 /** 39 * The size of the cached pieces. 40 */ 41 42 private Dimension cachedPieceSize = new Dimension(-1, -1); 43 44 45 46 47 /** 48 * The cached piece polygons. 49 */ 50 51 private Polygon kingPolygon, queenPolygon, rookPolygon, bishopPolygon, knightPolygon, pawnPolygon; 52 53 54 55 56 /** 57 * Creates a new <code>VectorPiecePainter</code>. 58 */ 59 VectorPiecePainter()60 public VectorPiecePainter(){ 61 super(Color.white, Color.black); 62 } 63 64 65 66 /** 67 * Creates a new <code>VectorPiecePainter</code> which will draw white and 68 * black pieces using the given colors. 69 * 70 * @param whiteColor The color for the white pieces. 71 * @param blackColor The color for the black pieces. 72 */ 73 VectorPiecePainter(Color whiteColor, Color blackColor)74 public VectorPiecePainter(Color whiteColor, Color blackColor){ 75 super(whiteColor, blackColor); 76 } 77 78 79 80 /** 81 * Creates a new <code>VectorPiecePainter</code> which will produce white and 82 * black pieces with the given colors and the given outline colors. 83 * 84 * @param whiteColor The color for the white pieces. 85 * @param blackColor The color for the black pieces. 86 * @param whiteOutline The color for the outline of white pieces. 87 * @param blackOutline The color for the outline of black pieces. 88 */ 89 VectorPiecePainter(Color whiteColor, Color blackColor, Color whiteOutline, Color blackOutline)90 public VectorPiecePainter(Color whiteColor, Color blackColor, 91 Color whiteOutline, Color blackOutline){ 92 super(whiteColor, blackColor, whiteOutline, blackOutline); 93 } 94 95 96 97 98 99 /** 100 * Creates and returns a Polygon of a king when fit into the given dimensions. 101 * This polygon will be used to draw king pieces. 102 */ 103 createKingPolygon(int width, int height)104 protected abstract Polygon createKingPolygon(int width, int height); 105 106 107 108 109 /** 110 * Creates and returns a Polygon of a queen when fit into the given dimensions. 111 * This polygon will be used to draw queen pieces. 112 */ 113 createQueenPolygon(int width, int height)114 protected abstract Polygon createQueenPolygon(int width, int height); 115 116 117 118 119 /** 120 * Creates and returns a Polygon of a rook when fit into the given dimensions. 121 * This polygon will be used to draw rook pieces. 122 */ 123 createRookPolygon(int width, int height)124 protected abstract Polygon createRookPolygon(int width, int height); 125 126 127 128 129 /** 130 * Creates and returns a Polygon of a bishop when fit into the given dimensions. 131 * This polygon will be used to draw bishop pieces. 132 */ 133 createBishopPolygon(int width, int height)134 protected abstract Polygon createBishopPolygon(int width, int height); 135 136 137 138 139 /** 140 * Creates and returns a Polygon of a knight when fit into the given dimensions. 141 * This polygon will be used to draw knight pieces. 142 */ 143 createKnightPolygon(int width, int height)144 protected abstract Polygon createKnightPolygon(int width, int height); 145 146 147 148 149 /** 150 * Creates and returns a Polygon of a pawn when fit into the given dimensions. 151 * This polygon will be used to draw pawn pieces. 152 */ 153 createPawnPolygon(int width, int height)154 protected abstract Polygon createPawnPolygon(int width, int height); 155 156 157 158 159 /** 160 * Clears the caching of piece polygons. 161 */ 162 clearPieceCache()163 private void clearPieceCache(){ 164 kingPolygon = null; 165 queenPolygon = null; 166 rookPolygon = null; 167 bishopPolygon = null; 168 knightPolygon = null; 169 pawnPolygon = null; 170 } 171 172 173 174 /** 175 * Draws the given piece at the given coordinates with the given size on 176 * the given Graphics. 177 */ 178 paintPiece(Piece piece, Graphics g, Component component, Rectangle rect, boolean isShaded)179 public final void paintPiece(Piece piece, Graphics g, Component component, Rectangle rect, 180 boolean isShaded){ 181 182 Color pieceColor = getPieceColor(piece, isShaded); 183 Color outlineColor = getOutlineColor(piece, isShaded); 184 185 int x = rect.x; 186 int y = rect.y; 187 int width = rect.width; 188 int height = rect.height; 189 190 g.translate(x,y); 191 192 if (!(piece instanceof ChessPiece)) 193 return; 194 195 ChessPiece cPiece = (ChessPiece)piece; 196 197 if ((cachedPieceSize.width != width) || (cachedPieceSize.height != height)){ 198 clearPieceCache(); 199 cachedPieceSize.width = width; 200 cachedPieceSize.height = height; 201 } 202 203 if (cPiece.isKing()) 204 drawKingImage(g, width, height, pieceColor, outlineColor); 205 else if (cPiece.isQueen()) 206 drawQueenImage(g, width, height, pieceColor, outlineColor); 207 else if (cPiece.isRook()) 208 drawRookImage(g, width, height, pieceColor, outlineColor); 209 else if (cPiece.isBishop()) 210 drawBishopImage(g, width, height, pieceColor, outlineColor); 211 else if (cPiece.isKnight()) 212 drawKnightImage(g, width, height, pieceColor, outlineColor); 213 else if (cPiece.isPawn()) 214 drawPawnImage(g, width, height, pieceColor, outlineColor); 215 216 g.translate(-x, -y); 217 } 218 219 220 221 /** 222 * Paints an image of a king of the given size with the given color and 223 * outline color using the given Graphics. 224 */ 225 drawKingImage(Graphics g, int width, int height, Color pieceColor, Color outlineColor)226 protected void drawKingImage(Graphics g, int width, int height, 227 Color pieceColor, Color outlineColor){ 228 229 if (kingPolygon == null) 230 kingPolygon = createKingPolygon(width, height); 231 232 g.setColor(pieceColor); 233 g.fillPolygon(kingPolygon); 234 235 g.setColor(outlineColor); 236 g.drawPolygon(kingPolygon); 237 } 238 239 240 241 242 243 244 /** 245 * Paints an image of a queen of the given size with the given color and 246 * outline color using the given Graphics. 247 */ 248 drawQueenImage(Graphics g, int width, int height, Color pieceColor, Color outlineColor)249 protected void drawQueenImage(Graphics g, int width, int height, 250 Color pieceColor, Color outlineColor){ 251 252 if (queenPolygon == null) 253 queenPolygon = createQueenPolygon(width, height); 254 255 g.setColor(pieceColor); 256 g.fillPolygon(queenPolygon); 257 258 g.setColor(outlineColor); 259 g.drawPolygon(queenPolygon); 260 } 261 262 263 264 265 /** 266 * Paints an image of a rook of the given size with the given color and 267 * outline color using the given Graphics. 268 */ 269 drawRookImage(Graphics g, int width, int height, Color pieceColor, Color outlineColor)270 protected void drawRookImage(Graphics g, int width, int height, 271 Color pieceColor, Color outlineColor){ 272 273 if (rookPolygon == null) 274 rookPolygon = createRookPolygon(width, height); 275 276 g.setColor(pieceColor); 277 g.fillPolygon(rookPolygon); 278 279 g.setColor(outlineColor); 280 g.drawPolygon(rookPolygon); 281 } 282 283 284 285 286 287 288 /** 289 * Paints an image of a bishop of the given size with the given color and 290 * outline color using the given Graphics. 291 */ 292 drawBishopImage(Graphics g, int width, int height, Color pieceColor, Color outlineColor)293 protected void drawBishopImage(Graphics g, int width, int height, 294 Color pieceColor, Color outlineColor){ 295 296 if (bishopPolygon == null) 297 bishopPolygon = createBishopPolygon(width, height); 298 299 g.setColor(pieceColor); 300 g.fillPolygon(bishopPolygon); 301 302 g.setColor(outlineColor); 303 g.drawPolygon(bishopPolygon); 304 } 305 306 307 308 309 310 311 /** 312 * Paints an image of a knight of the given size with the given color and 313 * outline color using the given Graphics. 314 */ 315 drawKnightImage(Graphics g, int width, int height, Color pieceColor, Color outlineColor)316 protected void drawKnightImage(Graphics g, int width, int height, 317 Color pieceColor, Color outlineColor){ 318 319 if (knightPolygon == null) 320 knightPolygon = createKnightPolygon(width, height); 321 322 g.setColor(pieceColor); 323 g.fillPolygon(knightPolygon); 324 325 g.setColor(outlineColor); 326 g.drawPolygon(knightPolygon); 327 } 328 329 330 331 332 /** 333 * Paints an image of a pawn of the given size with the given color and 334 * outline color using the given Graphics. 335 */ 336 drawPawnImage(Graphics g, int width, int height, Color pieceColor, Color outlineColor)337 protected void drawPawnImage(Graphics g, int width, int height, 338 Color pieceColor, Color outlineColor){ 339 340 if (pawnPolygon == null) 341 pawnPolygon = createPawnPolygon(width, height); 342 343 g.setColor(pieceColor); 344 g.fillPolygon(pawnPolygon); 345 346 g.setColor(outlineColor); 347 g.drawPolygon(pawnPolygon); 348 } 349 350 351 352 } 353