1 /**
2  * The chess framework library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2006 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.Component;
25 import java.awt.Graphics;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.util.Properties;
29 
30 import free.util.IOUtilities;
31 
32 
33 /**
34  * This class is provided for backwards compatibility with uses as a
35  * <code>ResourceBoardPainter</code>. The actual drawing is now done by either
36  * {@link BoardImageBoardPainter} or {@link SquareImagesBoardPainter}.
37  */
38 
39 public final class ImageBoardPainter implements ResourceBoardPainter{
40 
41 
42 
43   /**
44    * The delegate that does the actual drawing for us.
45    */
46 
47   private ResourceBoardPainter delegate;
48 
49 
50 
51   /**
52    * Since <code>ImageBoardPainter</code>s are immutable, simply returns
53    * <code>this</code>.
54    */
55 
freshInstance()56   public BoardPainter freshInstance(){
57     return this;
58   }
59 
60 
61 
62 
63   /**
64    * Loads the board images from the specified URL. The structure at the
65    * specified url is described below.
66    * A properties file named "definition" must be located at the base URL.
67    * That file should contain the two property:
68    * <ul>
69    *   <li><code>type</code>: The value is either "single" or "light-dark". This
70    *       specifies whether there is a single image of the entire board or two
71    *       pattern images for the light and dark squares. If this is omitted,
72    *       "light-dark" is assumed.
73    * </ul>
74    * Based on the value of this property, the actual drawing is done by either
75    * a {@link BoardImageBoardPainter} or a {@link SquareImagesBoardPainter}.
76    */
77 
load(URL url)78   public void load(URL url) throws IOException{
79     if (delegate != null)
80       throw new IllegalStateException("This ImageBoardPainter has already been loaded");
81 
82     URL defURL = new URL(url, "definition");
83 
84     Properties def = IOUtilities.loadProperties(defURL, true);
85     if (def == null)
86       def = new Properties();
87 
88     String type = def.getProperty("type", "light-dark");
89     if ("single".equals(type))
90       delegate = new BoardImageBoardPainter();
91     else if ("light-dark".equals(type))
92       delegate = new SquareImagesBoardPainter();
93     else
94       throw new IOException("Unrecognized type value: " + type);
95     delegate.load(url);
96   }
97 
98 
99 
100   /**
101    * Merely delegates the drawing to the actual board painter.
102    */
103 
paintBoard(Graphics g, Component component, int x, int y, int width, int height)104   public void paintBoard(Graphics g, Component component, int x, int y, int width, int height){
105     delegate.paintBoard(g, component, x, y, width, height);
106   }
107 
108 
109 
110 }
111