drawing-area: scale sprite images with tile resolution

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@766 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-07-21 15:34:56 +00:00
parent 1429407d9f
commit 6851224c10
2 changed files with 39 additions and 10 deletions

View file

@ -106,13 +106,13 @@ public class MicropolisDrawingArea extends JComponent
assert sprite.isVisible();
Point p = new Point(
sprite.x * TILE_WIDTH / 16,
sprite.y * TILE_HEIGHT / 16
(sprite.x + sprite.offx) * TILE_WIDTH / 16,
(sprite.y + sprite.offy) * TILE_HEIGHT / 16
);
Image img = tileImages.getSpriteImage(sprite.kind, sprite.frame-1);
if (img != null) {
gr.drawImage(img, p.x + sprite.offx, p.y + sprite.offy, null);
gr.drawImage(img, p.x, p.y, null);
}
else {
gr.setColor(Color.RED);
@ -313,9 +313,11 @@ public class MicropolisDrawingArea extends JComponent
private Rectangle getSpriteBounds(Sprite sprite, int x, int y)
{
return new Rectangle(
x*TILE_WIDTH/16+sprite.offx,
y*TILE_HEIGHT/16+sprite.offy,
sprite.width, sprite.height);
(x+sprite.offx)*TILE_WIDTH/16,
(y+sprite.offy)*TILE_HEIGHT/16,
sprite.width*TILE_WIDTH/16,
sprite.height*TILE_HEIGHT/16
);
}
public Rectangle getTileBounds(int xpos, int ypos)

View file

@ -100,14 +100,41 @@ public class TileImages
}
}
static Image loadSpriteImage(SpriteKind kind, int frameNo)
Image loadSpriteImage(SpriteKind kind, int frameNo)
{
String resourceName = "/obj"+kind.objectId+"-"+frameNo+".png";
URL iconUrl = TileImages.class.getResource(resourceName);
String resourceName = "/obj"+kind.objectId+"-"+frameNo;
// first, try to load specific size image
URL iconUrl = TileImages.class.getResource(resourceName+"_"+TILE_WIDTH+"x"+TILE_HEIGHT+".png");
if (iconUrl != null) {
return new ImageIcon(iconUrl).getImage();
}
iconUrl = TileImages.class.getResource(resourceName+".png");
if (iconUrl == null)
return null;
return new ImageIcon(iconUrl).getImage();
if (TILE_WIDTH==16 && TILE_HEIGHT==16) {
return new ImageIcon(iconUrl).getImage();
}
// scale the image ourselves
ImageIcon ii = new ImageIcon(iconUrl);
int destWidth = ii.getIconWidth() * TILE_WIDTH / 16;
int destHeight = ii.getIconHeight() * TILE_HEIGHT / 16;
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice dev = env.getDefaultScreenDevice();
GraphicsConfiguration conf = dev.getDefaultConfiguration();
BufferedImage bi = conf.createCompatibleImage(destWidth, destHeight, Transparency.TRANSLUCENT);
Graphics2D gr = bi.createGraphics();
gr.drawImage(ii.getImage(),
0, 0, destWidth, destHeight,
0, 0,
ii.getIconWidth(), ii.getIconHeight(),
null);
return bi;
}
}