📄 rasterlayer.java
字号:
/* given an offset and output width we can use memoryimagesource to
* do the actual drawing
*/
off= (int)((starty)*dw+startx);
/* the following works arround a bug(?) in netscape 4.05 where it
* crashes if you try to draw part of the array in the bottom R
* corner. Since it doesn't do this in Appletviewer or ME IE4/5
* I blame netscape and work round it, the problem may be in my code
* or my understanding of how memoryimage source handles offsets
*/
if(debug)System.out.println("RaL->dw "+dw+" "+w+" "+(dw-w));
if(debug)System.out.println("RaL->dh "+dh+" "+h+" "+(dh-h));
int first_offset = dw*(dh-h); // the whole block at the top
int second_offset = (dw-w)*h; // the bit down the left side
int out_bit = w*h; // the actual image to draw
// the sum of these three bits must be less than the total image
if(debug)System.out.println("RaL->off "+first_offset+" "+second_offset+" "+
out_bit+" -> "+(first_offset+second_offset+out_bit)+" "+data.length);
if(first_offset+second_offset+out_bit>data.length){ return;} // this is bad
/* It seems that this netscape fix breaks other vms - what do I know*/
//if(first_offset+second_offset+out_bit==data.length){ h--;} // this is netscape
if(debug)System.out.println("RaL->offsets "+startx+" "+(starty)+" "+off);
if(debug)System.out.println("RaL->w/h 2"+w+" "+h);
if(debug)System.out.println("RaL->data "+data.length+" off:"+off+" "+(w*h)+" "+(off+w*h));
source = new MemoryImageSource(w,h,data,off,dw);
image = obs.createImage(source);
/* now we need to know where to draw the image, I think that this can
* cause a slight jump to the output raster as we're moving from a
* double to the nearest int pixel position but thats life!
*/
int origin[]=scale.toGraphics(out.x,out.y);
int gh=scale.toGraphics(out.height);
int gw=scale.toGraphics(out.width);
if(debug)System.out.println("RaL->out "+out.x+" "+out.y);
if(debug)System.out.println("RaL-> -->"+origin[0]+" "+origin[1]+" "+(origin[1]-gh));
// boolean fred = g.drawImage(image,origin[0],origin[1]-gh,obs);
// we should use image.getScaledInstance here
boolean fred = g.drawImage(image,origin[0],origin[1]-gh,
gw,gh,obs);
if(debug)g.setColor(Color.yellow);
if(debug)g.drawRect(origin[0],origin[1]-gh,gw,gh);
if(grid){
for(int i = origin[0];i<origin[0]+gw;i+=scale.toGraphics(cellsize)){
g.drawLine(i,origin[1]-gh,i,origin[1]);
}
for(int i = origin[1];i<origin[1]+gh;i+=scale.toGraphics(cellsize)){
g.drawLine(origin[0],i-gh,origin[0]+gw,i-gh);
}
int r=(int)(out.y/cellsize);
int c=(int)(out.x/cellsize);
int c2=scale.toGraphics(cellsize)/2;
g.setColor(Color.black);
for(int j = origin[1];j<origin[1]+gh;j+=scale.toGraphics(cellsize)){
for(int i = origin[0];i<origin[0]+gw;i+=scale.toGraphics(cellsize)){
g.drawString((""+(c++)+","+r),i,j-gh+c2);
}
r++;
c=(int)(out.x/cellsize);
}
}
} else{ // off screen
}
}
/**
* @return the id of the point
*/
public int getID(GeoPoint p){
return r.getCellPos(p.x,p.y);
}
/**
* @return the id of the point
*/
public int getID(double x, double y){
return r.getCellPos(x,y);
}
/**
* Returns all grid cell IDs indicated by rect and mode.
*
* @param rect the rectange that describes the region to select from
* @param mode the mode to use when selecting gridCells
*
* @return an array of grid cell IDs
*
* @see SelectionManager
**/
public int[] getIDs( GeoRectangle rect, int mode ) {
double originX = r.getOriginx();
double originY = r.getOriginy();
double cellSize = r.getCellSize();
int minXCoord = 0;
double minX = 0;
int minYCoord = 0;
double minY = 0;
int xWidth = 0;
int yWidth = 0;
switch ( mode ) {
case SelectionManager.CONTAINS:
minXCoord = (int)Math.ceil( (rect.getX() - originX) / cellSize );
minX = r.getXCell( minXCoord );
minYCoord = (int)Math.ceil( (rect.getY() - originY) / cellSize );
minY = r.getYCell( minYCoord );
xWidth = (int)Math.floor( (rect.getX() + rect.getWidth() - minX)
/ cellSize );
yWidth = (int)Math.floor( (rect.getY() + rect.getHeight() - minY)
/ cellSize );
break;
case SelectionManager.CROSSES:
minXCoord = (int)Math.floor( (rect.getX() - originX) / cellSize );
minX = r.getXCell( minXCoord );
minYCoord = (int)Math.floor( (rect.getY() - originY) / cellSize );
minY = r.getYCell( minYCoord );
xWidth = (int)Math.ceil( (rect.getX() + rect.getWidth() - minX)
/ cellSize );
yWidth = (int)Math.ceil( (rect.getY() + rect.getHeight() - minY)
/ cellSize );
break;
default:
throw new RuntimeException
( "invalid mode: expected either SelectionManager.CONTAINS or "
+ "SelectionManager.CROSSES" );
}
int[] arr = new int[ xWidth * yWidth ];
int index = 0;
for ( int x = 0; x < xWidth; x ++ ) {
for ( int y = 0; y < yWidth; y ++ ) {
arr[ index ] = r.getCellID( minXCoord + x, minYCoord + y );
index++;
}
}
return arr;
}
/**
* @return the maximum value of the contained raster
*/
public double getMax(){
return r.getMax();
}
/**
* @return the minimum value of the contained raster
*/
public double getMin(){
return r.getMin();
}
public double getNZMin(){
return r.getNZMin();
}
/**
* return the data associated with this layer
*/
public GeoData getGeoData(){
// shoud do something in future
return r;
}
/**
* set the data associated with this layer
*/
public void setGeoData(GeoData data){
if(data instanceof Raster){
r=(Raster)data;
}else{
// complain loudly
}
}
public double getSparseness(){
return r.getSparseness();
}
public boolean isSparse(){
return r.isSparse();
}
public void changeRaster(Raster n){
r=n;
set=false;
System.gc();
notifyLayerChangedListeners(LayerChangedEvent.GEOGRAPHY);
}
public void Changed(ChangedEvent e){
set=false;
notifyLayerChangedListeners(e.getReason());
}
public GeoRectangle getBoundsOf(int id){
int row = id%r.getWidth();
int col = id/r.getWidth();
double x = r.getXCell(col);
double y = r.getYCell(row);
return new GeoRectangle(x,y,r.getCellSize(),r.getCellSize());
}
public GeoRectangle getBoundsOf(int id[]){
GeoRectangle gr = new GeoRectangle();
for(int i=0;i<id.length;i++){
gr.add(getBoundsOf(id[i]));
}
return gr;
}
public Image getImage(Shader shade){// returns a copy of the current screen image
Canvas obs = new Canvas();
MemoryImageSource source;
data = new int[r.getHeight()*r.getWidth()];
int h=r.getHeight();
int w=r.getWidth();
int off=0;
int pt =0;
// set up the underlying image data
for (int i=0;i<h;i++){
for (int j=0; j<w;j++){
data[pt++]=shade.getRGB(r.getCell(i,j));
}
}
if(debug)System.out.println("RaL->Painted "+(pt)+" pixels");
set=false;
source = new MemoryImageSource(w,h,data,off,w);
image = obs.createImage(source);
return image;
}
}
/*
* $Log: RasterLayer.java,v $
* Revision 1.7 2001/08/01 12:32:46 ianturton
* modification submited by Michael Becke <becke@u.washington.edu> to reduce
* memory usage.
*
*
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -