| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 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 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 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 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
public void paint(Graphics2D g, AffineTransform realToScreen, ImageObserver observer) { |
|---|
| 64 |
|
|---|
| 65 |
if(image == null) image = getImage(floor); |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
AffineTransform imageToScreen = new AffineTransform(realToScreen); |
|---|
| 69 |
imageToScreen.concatenate(imageToReal); |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 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 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
private transient URI rootUri; |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
private URI treeUri; |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
private String extension; |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
private AffineTransform imageToReal; |
|---|
| 172 |
private AffineTransform realToImage; |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
private int height, width; |
|---|
| 176 |
|
|---|
| 177 |
private int floor; |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
private transient Image image; |
|---|
| 181 |
} |
|---|