root/trunk/BuildingMap.java

Revision 3, 4.9 kB (checked in by dpaola2, 6 days ago)

old pathways project added

Line 
1 /**
2  * BuildingMap.java
3  *
4  * This is a map which just shows one floor of a building.
5  */
6
7 import java.awt.*;
8 import java.awt.geom.*;
9 import java.awt.image.*;
10 import java.io.*;
11 import java.net.*;
12
13 public class BuildingMap extends BackgroundMap implements Serializable {
14         //static final long serialVersionUID = 0L;
15
16
17         /**
18          * Creates a map which has a grid of rows x cols which covers an area
19          * of the world specified by realRectangle (in real coordinates).
20          */
21
22         public BuildingMap(
23                 int floor,
24                 int height,
25                 int width,
26                 AffineTransform imageToReal,
27                 String rootUri,
28                 String treeUri,
29                 String extension)
30         {
31                 this.floor = floor;
32                 this.height = height;
33                 this.width = width;
34                 setImageToReal(imageToReal);
35                 setRootUri(rootUri);
36                 setTreeUri(treeUri);
37                 setExtension(extension);
38         }
39
40         public void setImageToReal(AffineTransform imageToReal) {
41                 this.imageToReal = imageToReal;
42                 try {
43                         realToImage = imageToReal.createInverse();
44                 } catch(NoninvertibleTransformException e) {
45                         throw new IllegalArgumentException("Couldn't invert the transform");
46                 }
47         }
48
49         public AffineTransform getImageToReal() { return imageToReal; }
50         public AffineTransform getRealToImage() { return realToImage; }
51
52         public Rectangle2D getRealBoundingRectangle() {
53                 return imageToReal
54                         .createTransformedShape(new Rectangle(0, 0, width, height))
55                         .getBounds2D();
56         };
57
58         /**
59          * Paints the map onto the given Graphics2D object, using realToScreen
60          * to transform the real coordinates the map knows about to screen
61          * coordinates it uses to draw on the Graphics2D.
62          */
63         public void paint(Graphics2D g, AffineTransform realToScreen, ImageObserver observer) {
64                
65                 if(image == null) image = getImage(floor);
66                
67                 // image (imageToReal)-> real (realToScreen)-> screen
68                 AffineTransform imageToScreen = new AffineTransform(realToScreen);
69                 imageToScreen.concatenate(imageToReal);
70
71                 // ok, draw it.
72                 g.drawImage(image, imageToScreen, observer);
73         }
74
75         private Image getImage(int floor) {
76                 System.out.println("BuildingMap requesting image " + getUrl(floor));
77                 return Toolkit.getDefaultToolkit().getImage(getUrl(floor));
78         }
79
80         /**
81          * This returns the filename (no path components) for an image file
82          * for the given floor.  It will be concatenated with
83          * the base and tree URIs.  It can be overridden for flexibility, though
84          * it may be easier in the long run not to.
85          *
86          * Note that it cannot be declared as static, because then even if
87          * subclasses override it, the subclass's version won't be run because
88          * the call will be resolved at compile time to this implementation.
89          */
90
91         public String getFilename(int floor) {
92                 return new String("floor_" + floor + "." + extension);
93         }
94
95         public URL getUrl(int floor) {
96                 try {
97                         if(rootUri == null) rootUri = (new File(".")).toURI();
98                         if(treeUri == null) treeUri = new URI("");
99                         return rootUri.resolve(treeUri).resolve(new URI(getFilename(floor))).toURL();
100                 } catch(Exception e) {
101                         throw new RuntimeException(e);
102                 }
103         }
104
105         public URI getRootUri() { return rootUri; }
106         public void setRootUri(String rootUri) {
107                 if(rootUri == null || rootUri.equals("")) {
108                         this.rootUri = null;
109                         return;
110                 }                       
111                 try {
112                         this.rootUri = new URI(rootUri);
113                 } catch(URISyntaxException e) {
114                         throw new IllegalArgumentException("malformed URI " + rootUri);
115                 }
116         }
117
118         public URI getTreeUri() { return treeUri; }
119         public void setTreeUri(String treeUri) {
120                 try {
121                         this.treeUri = new URI(treeUri);
122                 } catch(URISyntaxException e) {
123                         throw new IllegalArgumentException("malformed URI " + treeUri);
124                 }
125         }
126
127         public String getExtension() { return extension; }
128         public void setExtension(String extension) {
129                 this.extension = extension;
130         }
131
132         private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
133                 in.defaultReadObject();
134         }
135
136         // in pixels per meter
137         public double getPixelForPixelScale() {
138                 Rectangle2D boundingRect = getRealBoundingRectangle();
139                 double xScale = width / boundingRect.getWidth();
140                 double yScale = height / boundingRect.getHeight();
141                 return (xScale + yScale) / 2.0;
142         }
143        
144         public int getFloor() { return floor; }
145         public void setFloor(int floor) {
146                 this.floor = floor;
147                 image = null;
148         }
149
150         /**
151          * Where we get our pictures from.  This can be blank for the filesystem,
152          * or http://example.com/pathways/, for example.  Essentially the
153          * document root.
154          */
155         private transient URI rootUri;
156
157         /**
158          * The location in the tree our pictures come from.  For example,
159          * images/uiucmap/.  This is so that a map knows were to get its
160          * pictures, but we can move the directory tree between servers without
161          * changing code.
162          */
163         private URI treeUri;
164
165         /**
166          * The extension for the image files
167          */
168         private String extension;
169
170         // these must be kept inverse of each other
171         private AffineTransform imageToReal;
172         private AffineTransform realToImage;
173
174         /// size in pixels of the map
175         private int height, width;
176        
177         private int floor;
178
179         /// image for the current floor
180         private transient Image image;
181 }
Note: See TracBrowser for help on using the browser.