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 
26 
27 /**
28  * A skeleton implementation of <code>ColoredPiecePainter</code>.
29  */
30 
31 public abstract class AbstractColoredPiecePainter implements ColoredPiecePainter{
32 
33 
34 
35   /**
36    * The color of the white pieces.
37    */
38 
39   private Color whiteColor;
40 
41 
42 
43   /**
44    * The color of the black pieces.
45    */
46 
47   private Color blackColor;
48 
49 
50 
51   /**
52    * The color of the outline of the white pieces.
53    */
54 
55   private Color whiteOutline;
56 
57 
58 
59   /**
60    * The color of the outline of the black pieces.
61    */
62 
63   private Color blackOutline;
64 
65 
66 
67   /**
68    * Creates a new <code>AbstractColoredPiecePainter</code>. White pieces will
69    * be drawn with white black pieces with black.
70    */
71 
AbstractColoredPiecePainter()72   public AbstractColoredPiecePainter(){
73     this(Color.white,Color.black);
74   }
75 
76 
77 
78 
79   /**
80    * Creates a new <code>AbstractColoredPiecePainter</code> which will draw
81    * white and black pieces using the given colors. The outline of the pieces
82    * will have an RGB value reverse to the RGB values of the given colors (if
83    * the color for the white pieces is for example R=255 G=128 B=0 then the
84    * outline of the white pieces will be R=0 G=127 B=255).
85    *
86    * @param whiteColor The color for the white pieces.
87    * @param blackColor The color for the black pieces.
88    */
89 
AbstractColoredPiecePainter(Color whiteColor, Color blackColor)90   public AbstractColoredPiecePainter(Color whiteColor, Color blackColor){
91     this(whiteColor, blackColor, getReversed(whiteColor), getReversed(blackColor));
92   }
93 
94 
95 
96 
97   /**
98    * Creates a new <code>AbstractColoredPiecePainter</code> which will produce
99    * white and black pieces with the given colors and the given outline colors.
100    *
101    * @param whiteColor The color for the white pieces.
102    * @param blackColor The color for the black pieces.
103    * @param whiteOutline The color for the outline of white pieces.
104    * @param blackOutline The color for the outline of black pieces.
105    */
106 
AbstractColoredPiecePainter(Color whiteColor, Color blackColor, Color whiteOutline, Color blackOutline)107   public AbstractColoredPiecePainter(Color whiteColor, Color blackColor,
108       Color whiteOutline, Color blackOutline){
109 
110     if (whiteColor == null)
111       throw new IllegalArgumentException("Null white color");
112     if (blackColor == null)
113       throw new IllegalArgumentException("Null black color");
114     if (whiteOutline == null)
115       throw new IllegalArgumentException("Null white outline color");
116     if (blackOutline == null)
117       throw new IllegalArgumentException("Null black outline color");
118 
119     this.whiteColor = whiteColor;
120     this.blackColor = blackColor;
121     this.whiteOutline = whiteOutline;
122     this.blackOutline = blackOutline;
123   }
124 
125 
126 
127   /**
128    * Returns the color with the opposite RGB values from the given color.
129    *
130    * @param color The color to reverse.
131    */
132 
getReversed(Color color)133   private static Color getReversed(Color color){
134     return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
135   }
136 
137 
138 
139   /**
140    * Retrieves the color with which white pieces are drawn.
141    *
142    * @return The color for the white pieces.
143    */
144 
getWhiteColor()145   public Color getWhiteColor(){
146     return whiteColor;
147   }
148 
149 
150 
151   /**
152    * Sets the color with which white pieces are drawn.
153    */
154 
setWhiteColor(Color color)155   public void setWhiteColor(Color color){
156     if (color == null)
157       throw new IllegalArgumentException("Null color");
158 
159     whiteColor = color;
160   }
161 
162 
163 
164   /**
165    * Retrieves the color with which black pieces are drawn.
166    *
167    * @return The color for the black pieces.
168    */
169 
getBlackColor()170   public Color getBlackColor(){
171     return blackColor;
172   }
173 
174 
175 
176   /**
177    * Sets the color with which black pieces are drawn.
178    */
179 
setBlackColor(Color color)180   public void setBlackColor(Color color){
181     if (color == null)
182       throw new IllegalArgumentException("Null color");
183 
184     blackColor = color;
185   }
186 
187 
188 
189   /**
190    * Retrieves the color with which the outline of white pieces is drawn.
191    *
192    * @return The color for the outline of white pieces.
193    */
194 
getWhiteOutline()195   public Color getWhiteOutline(){
196     return whiteOutline;
197   }
198 
199 
200 
201   /**
202    * Sets the color with which the outline of white pieces is drawn.
203    */
204 
setWhiteOutline(Color color)205   public void setWhiteOutline(Color color){
206     if (color == null)
207       throw new IllegalArgumentException("Null color");
208 
209     whiteOutline = color;
210   }
211 
212 
213 
214   /**
215    * Retrieves the color with which the outline of black pieces is drawn.
216    *
217    * @return The color for the outline of black pieces.
218    */
219 
getBlackOutline()220   public Color getBlackOutline(){
221     return blackOutline;
222   }
223 
224 
225 
226   /**
227    * Sets the color with which the outline of black pieces is drawn.
228    */
229 
setBlackOutline(Color color)230   public void setBlackOutline(Color color){
231     if (color == null)
232       throw new IllegalArgumentException("Null color");
233 
234     blackOutline = color;
235   }
236 
237 
238 
239   /**
240    * Returns with which the specified piece should be drawn.
241    */
242 
getPieceColor(Piece piece, boolean isShaded)243   public Color getPieceColor(Piece piece, boolean isShaded){
244     if (piece == null)
245       return null;
246 
247     Color color = piece.isWhite() ? getWhiteColor() : getBlackColor();
248     return isShaded ? getShaded(color) : color;
249   }
250 
251 
252 
253   /**
254    * Returns the color with which the outline of the specified piece should be
255    * drawn.
256    */
257 
getOutlineColor(Piece piece, boolean isShaded)258   public Color getOutlineColor(Piece piece, boolean isShaded){
259     if (piece == null)
260       return null;
261 
262     Color color = piece.isWhite() ? getWhiteOutline() : getBlackOutline();
263     return isShaded ? getShaded(color) : color;
264   }
265 
266 
267 
268   /**
269    * Returns a shaded version of the specified color.
270    */
271 
getShaded(Color color)272   protected Color getShaded(Color color){
273     int r = (color.getRed() + 128*2)/3;
274     int g = (color.getGreen() + 128*2)/3;
275     int b = (color.getBlue() + 128*2)/3;
276 
277     return new Color(r, g, b);
278   }
279 
280 
281 
282 
283 }
284