| 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 TiledMap extends BackgroundMap implements Serializable { |
|---|
| 14 |
static final long serialVersionUID = 3890326970229567605L; |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
public TiledMap(int rows, int columns, int rowHeight, int columnWidth, AffineTransform imageToReal) { |
|---|
| 23 |
this.rows = rows; |
|---|
| 24 |
this.columns = columns; |
|---|
| 25 |
this.rowHeight = rowHeight; |
|---|
| 26 |
this.columnWidth = columnWidth; |
|---|
| 27 |
|
|---|
| 28 |
setImageToReal(imageToReal); |
|---|
| 29 |
|
|---|
| 30 |
images = new Image[rows][columns]; |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
public void setImageToReal(AffineTransform imageToReal) { |
|---|
| 34 |
this.imageToReal = imageToReal; |
|---|
| 35 |
try { |
|---|
| 36 |
realToImage = imageToReal.createInverse(); |
|---|
| 37 |
} catch(NoninvertibleTransformException e) { |
|---|
| 38 |
throw new IllegalArgumentException("Couldn't invert the transform"); |
|---|
| 39 |
} |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
public AffineTransform getImageToReal() { return imageToReal; } |
|---|
| 43 |
public AffineTransform getRealToImage() { return realToImage; } |
|---|
| 44 |
|
|---|
| 45 |
public Rectangle2D getRealBoundingRectangle() { |
|---|
| 46 |
return imageToReal |
|---|
| 47 |
.createTransformedShape(new Rectangle(0, 0, columns * columnWidth, rows * rowHeight)) |
|---|
| 48 |
.getBounds2D(); |
|---|
| 49 |
}; |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
public void paint(Graphics2D g, AffineTransform realToScreen, ImageObserver observer) { |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
Rectangle2D screenClip = g.getClipBounds(); |
|---|
| 61 |
if(screenClip == null) { |
|---|
| 62 |
System.out.println("no clipping region"); |
|---|
| 63 |
screenClip = new Rectangle2D.Double( |
|---|
| 64 |
Double.NEGATIVE_INFINITY, |
|---|
| 65 |
Double.NEGATIVE_INFINITY, |
|---|
| 66 |
Double.POSITIVE_INFINITY, |
|---|
| 67 |
Double.POSITIVE_INFINITY |
|---|
| 68 |
); |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
AffineTransform screenToReal; |
|---|
| 73 |
try { |
|---|
| 74 |
screenToReal = realToScreen.createInverse(); |
|---|
| 75 |
} catch(NoninvertibleTransformException e) { |
|---|
| 76 |
throw new IllegalArgumentException("Couldn't invert the transform"); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
Shape imageClip = realToImage.createTransformedShape(screenToReal.createTransformedShape(screenClip)); |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
Rectangle imageRectangle = new Rectangle(0, 0, columnWidth, rowHeight); |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
for(int row = 0; row < rows; row++) { |
|---|
| 87 |
for(int column = 0; column < columns; column++) { |
|---|
| 88 |
|
|---|
| 89 |
imageRectangle.x = column * columnWidth; |
|---|
| 90 |
imageRectangle.y = row * rowHeight; |
|---|
| 91 |
if(!imageClip.intersects(imageRectangle)) continue; |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
Image image = images[row][column]; |
|---|
| 96 |
if(image == null) { |
|---|
| 97 |
image = getImage(row, column); |
|---|
| 98 |
images[row][column] = image; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
AffineTransform tileToScreen = new AffineTransform(realToScreen); |
|---|
| 103 |
tileToScreen.concatenate(imageToReal); |
|---|
| 104 |
tileToScreen.translate(imageRectangle.x, imageRectangle.y); |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
g.drawImage(image, tileToScreen, observer); |
|---|
| 108 |
|
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
private Image getImage(int row, int column) { |
|---|
| 114 |
System.out.println("loading " + getUrl(row, column)); |
|---|
| 115 |
return Toolkit.getDefaultToolkit().getImage(getUrl(row, column)); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
public String getFilename(int row, int column) { |
|---|
| 131 |
return new String("row" + row + "column" + column + "." + extension); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
public URL getUrl(int row, int column) { |
|---|
| 135 |
try { |
|---|
| 136 |
if(rootUri == null) rootUri = (new File(".")).toURI(); |
|---|
| 137 |
if(treeUri == null) treeUri = new URI(""); |
|---|
| 138 |
return rootUri.resolve(treeUri).resolve(new URI(getFilename(row, column))).toURL(); |
|---|
| 139 |
} catch(Exception e) { |
|---|
| 140 |
throw new RuntimeException(e); |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
public URI getRootUri() { return rootUri; } |
|---|
| 145 |
public void setRootUri(String rootUri) { |
|---|
| 146 |
if(rootUri == null || rootUri.equals("")) { |
|---|
| 147 |
this.rootUri = null; |
|---|
| 148 |
return; |
|---|
| 149 |
} |
|---|
| 150 |
try { |
|---|
| 151 |
this.rootUri = new URI(rootUri); |
|---|
| 152 |
} catch(URISyntaxException e) { |
|---|
| 153 |
throw new IllegalArgumentException("malformed URI " + rootUri); |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
public URI getTreeUri() { return treeUri; } |
|---|
| 158 |
public void setTreeUri(String treeUri) { |
|---|
| 159 |
try { |
|---|
| 160 |
this.treeUri = new URI(treeUri); |
|---|
| 161 |
} catch(URISyntaxException e) { |
|---|
| 162 |
throw new IllegalArgumentException("malformed URI " + treeUri); |
|---|
| 163 |
} |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
public String getExtension() { return extension; } |
|---|
| 167 |
public void setExtension(String extension) { |
|---|
| 168 |
this.extension = extension; |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { |
|---|
| 172 |
in.defaultReadObject(); |
|---|
| 173 |
images = new Image[rows][columns]; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
public double getPixelForPixelScale() { |
|---|
| 178 |
Rectangle2D boundingRect = getRealBoundingRectangle(); |
|---|
| 179 |
double xScale = columns * columnWidth / boundingRect.getWidth(); |
|---|
| 180 |
double yScale = rows * rowHeight / boundingRect.getHeight(); |
|---|
| 181 |
return (xScale + yScale) / 2.0; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
private transient URI rootUri; |
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
private URI treeUri; |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
private String extension; |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
private AffineTransform imageToReal; |
|---|
| 204 |
private AffineTransform realToImage; |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
private int rows, columns; |
|---|
| 208 |
|
|---|
| 209 |
private int rowHeight, columnWidth; |
|---|
| 210 |
|
|---|
| 211 |
private transient Image[][] images; |
|---|
| 212 |
} |
|---|