1 package se
.umu
.cs
.dit06ajnajs
.map
;
3 import java
.awt
.image
.BufferedImage
;
4 import java
.io
.IOException
;
6 import java
.util
.HashMap
;
8 import javax
.imageio
.ImageIO
;
11 * MapSquarePrototypeFactory is used to create new instances of different
12 * implementations of MapSquare from a String. Implemented as a Singleton.
14 * @author Anton Johansson, dit06ajn@cs.umu.se
17 public class MapSquarePrototypeFactory
{
18 // private static Logger logger = Logger.getLogger("AntiTD");
20 private static final MapSquarePrototypeFactory INSTANCE
21 = new MapSquarePrototypeFactory();
23 private java
.util
.Map
<String
, MapSquare
> squareMap
;
26 * Gets the only instance of this class, if none exists one is created.
28 * @return The only instance of this class.
30 public static MapSquarePrototypeFactory
getInstance() {
35 * MapSquarePrototypeFactory initialices all different types of MapSquares
36 * and puts them in a java.util.Map.
38 * Private constructor makes sure there can only be one instance of this
39 * class, and that instance is created by the class itself from the first
40 * invocation of getInstance()
43 private MapSquarePrototypeFactory() {
44 squareMap
= new HashMap
<String
, MapSquare
>();
47 // Create TowerSquare Prototype
48 URL url
= this.getClass().getResource("/resources/grass.jpg");
49 BufferedImage image
= ImageIO
.read(url
);
50 squareMap
.put("TowerSquare", new TowerSquare(-1, -1, image
));
52 // Create PathSquare Prototype
53 url
= this.getClass().getResource("/resources/path.jpg");
54 image
= ImageIO
.read(url
);
55 squareMap
.put("PathSquare", new PathSquare(-1, -1, image
));
57 // Create BlockedSquare Prototype
58 url
= this.getClass().getResource("/resources/effielTower.gif");
59 image
= ImageIO
.read(url
);
60 squareMap
.put("BlockedSquare", new BlockedSquare(-1, -1, image
));
61 } catch (IOException e
) {
62 System
.err
.println("Couldn't find image. Exception: "
68 * Creates a new instance of a MapSquare with the type specified by the
71 * @param type A String representing a type exisiting in this factory.
72 * @param x The x-position of the resulting MapSquare.
73 * @param y The y-position of the resulting MapSquare
74 * @return A newly create MapSquare.
76 public MapSquare
createMapSquare(String type
, int x
, int y
) {
77 MapSquare resultSquare
= squareMap
.get(type
);
78 if (resultSquare
== null) {
79 throw new IllegalArgumentException("The specified type '" + type
82 resultSquare
= (MapSquare
) resultSquare
.clone();
89 // TODO Överlagra clone() och kasta CloneNotSupportetException för Singelton-mönstret?