/*
* Created on 16.03.2005
*
* To change the template for this generated file go to
* Window&Preferences&Java&Code Generation&Code and Comments
*/
package fight4zuerich.citymap;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.Vector;
import fight4zuerich.core.Log;
/**
* @author z102-11
*
* To change the template for this generated type comment go to
* Window&Preferences&Java&Code Generation&Code and Comments
*/
public class CityMapView implements ImageObserver {
public static final int DEFAULT_STEP_WIDTH = 20;
public static final int DEFAULT_STEP_HEIGHT = 20;
public static final int DEFAULT_WIDTH = 450;
public static final int DEFAULT_HEIGHT = 333;
private int mWidthStep;
private int mHeightStep;
private int mPositionX;
private int mPositionY;
private int mWidth;
private int mHeight;
private CityMap mCityMap;
private Image mImage;
private BufferedImage mBufferedImage;
public CityMapView(CityMap _cityMap) {
mCityMap = _cityMap;
setWidthStep(CityMapView.DEFAULT_STEP_WIDTH);
setHeightStep(CityMapView.DEFAULT_STEP_HEIGHT);
setWidth(CityMapView.DEFAULT_WIDTH);
setHeight(CityMapView.DEFAULT_HEIGHT);
setPositionX((mCityMap.getWidth()/2)-(getWidth()/2));
setPositionY((mCityMap.getHeight()/2)-(getHeight()/2));
mBufferedImage = new BufferedImage(mCityMap.getWidth(), mCityMap.getHeight(), BufferedImage.TYPE_INT_RGB);
mBufferedImage.getGraphics().drawImage(mCityMap.getImage(), 0, 0, this);
updateImage();
}
public Vector getSensitiveMapAreas() {
Vector result = new Vector();
Rectangle cityMapViewRect = new Rectangle(getPositionX(), getPositionY(), getWidth(), getHeight());
Vector input = mCityMap.getSensitiveMapAreas();
for(int i=0;i<input.size();i++) {
SensitiveMapArea a = ((SensitiveMapArea)input.get(i));
Rectangle areaRectangle = new Rectangle(a.getPositionX(), a.getPositionY(), a.getWidth(), a.getHeight());
if(cityMapViewRect.contains(areaRectangle)) {
result.add(a);
}
}
return result;
}
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
Log.debug(getClass(), "infoflags: "+infoflags);
if(infoflags==ImageObserver.ALLBITS) {
updateImage();
return false;
} /*else if(infoflags==ImageObserver.PROPERTIES) {
updateImage();
//return false;
}*/
return true;
}
private void updateImage() {
mImage = mBufferedImage.getSubimage(getPositionX(), getPositionY(), getWidth(), getHeight());
}
public Image getImage() {
if(mImage==null) {
updateImage();
}
return mImage;
}
public boolean moveUp() {
//System.out.println("[CityMapView.moveUp] aufgerufen");
if(getPositionY()==0) {
return false;
}
if(getPositionY()-getHeightStep()<0) {
setPositionY(0);
updateImage();
return true;
} else {
setPositionY(getPositionY()-getHeightStep());
updateImage();
return true;
}
}
public boolean moveDown() {
//System.out.println("[CityMapView.moveDown] aufgerufen");
if(getPositionY()+getHeight()==mCityMap.getHeight()) {
return false;
}
if(getPositionY()+getHeight()+getHeightStep()>mCityMap.getHeight()) {
setPositionY(mCityMap.getHeight()-getHeight());
updateImage();
return true;
} else {
setPositionY(getPositionY()+getHeightStep());
updateImage();
return true;
}
}
public boolean moveRight() {
//System.out.println("[CityMapView.moveRight] aufgerufen");
if(getPositionX()+getWidth()==mCityMap.getWidth()) {
return false;
}
if(getPositionX()+getWidth()+getWidthStep()>mCityMap.getWidth()) {
setPositionX(mCityMap.getWidth()-getWidth());
updateImage();
return true;
} else {
setPositionX(getPositionX()+getWidthStep());
updateImage();
return true;
}
}
public boolean moveLeft() {
//System.out.println("[CityMapView.moveLeft] aufgerufen");
if(getPositionX()==0) {
return false;
}
if(getPositionX()-getWidthStep()<0) {
setPositionX(0);
updateImage();
return true;
} else {
setPositionX(getPositionX()-getWidthStep());
updateImage();
return true;
}
}
public CityMap getCityMap() {
return mCityMap;
}
}