📄 objectgridportrayal2d.java
字号:
package sim.portrayal.grid;import sim.portrayal.*;import sim.portrayal.simple.*;import sim.field.grid.*;import java.awt.*;import java.awt.geom.*;import sim.util.*;/** A portrayal for grids containing objects, such as maybe agents or agent bodies.*/public class ObjectGridPortrayal2D extends FieldPortrayal2D { // a grey oval. You should provide your own protrayals... SimplePortrayal2D defaultPortrayal = new OvalPortrayal2D(); public ObjectGridPortrayal2D() { super(); } public void setField(Object field) { dirtyField = true; if (field instanceof ObjectGrid2D ) this.field = field; else throw new RuntimeException("Invalid field for ObjectGridPortrayal2D: " + field); } public Portrayal getDefaultPortrayal() { return defaultPortrayal; } protected void hitOrDraw(Graphics2D graphics, DrawInfo2D info, Bag putInHere) { final ObjectGrid2D field = (ObjectGrid2D)(this.field); if (field==null) return; // Scale graphics to desired shape -- according to p. 90 of Java2D book, // this will change the line widths etc. as well. Maybe that's not what we // want. // first question: determine the range in which we need to draw. // We assume that we will fill exactly the info.draw rectangle. // We can do the item below because we're an expensive operation ourselves final int maxX = field.getWidth(); final int maxY = field.getHeight(); if (maxX == 0 || maxY == 0) return; final double xScale = info.draw.width / maxX; final double yScale = info.draw.height / maxY; int startx = (int)((info.clip.x - info.draw.x) / xScale); int starty = (int)((info.clip.y - info.draw.y) / yScale); // assume that the X coordinate is proportional -- and yes, it's _width_ int endx = /*startx +*/ (int)((info.clip.x - info.draw.x + info.clip.width) / xScale) + /*2*/ 1; // with rounding, width be as much as 1 off int endy = /*starty +*/ (int)((info.clip.y - info.draw.y + info.clip.height) / yScale) + /*2*/ 1; // with rounding, height be as much as 1 off final Rectangle clip = (graphics==null ? null : graphics.getClipBounds()); DrawInfo2D newinfo = new DrawInfo2D(new Rectangle2D.Double(0,0, xScale, yScale), info.clip); // we don't do further clipping if (endx > maxX) endx = maxX; if (endy > maxY) endy = maxY; if( startx < 0 ) startx = 0; if( starty < 0 ) starty = 0; for(int x=startx;x<endx;x++) for(int y=starty;y<endy;y++) { Object obj = field.field[x][y]; Portrayal p = getPortrayalForObject(obj); if (!(p instanceof SimplePortrayal2D)) throw new RuntimeException("Unexpected Portrayal " + p + " for object " + obj + " -- expected a SimplePortrayal2D"); SimplePortrayal2D portrayal = (SimplePortrayal2D)p; // translate --- the + newinfo.width/2.0 etc. moves us to the center of the object newinfo.draw.x = (int)(info.draw.x + (xScale) * x); newinfo.draw.y = (int)(info.draw.y + (yScale) * y); newinfo.draw.width = (int)(info.draw.x + (xScale) * (x+1)) - newinfo.draw.x; newinfo.draw.height = (int)(info.draw.y + (yScale) * (y+1)) - newinfo.draw.y; // adjust drawX and drawY to center newinfo.draw.x += newinfo.draw.width / 2.0; newinfo.draw.y += newinfo.draw.height / 2.0; if (graphics == null) { if (obj != null && portrayal.hitObject(obj, newinfo)) putInHere.add(getWrapper(new Int2D(x,y))); } else { // MacOS X 10.3 Panther has a bug which resets the clip, YUCK // graphics.setClip(clip); portrayal.draw(obj, graphics, newinfo); } } } public LocationWrapper getWrapper(Int2D location) { final ObjectGrid2D field = (ObjectGrid2D)(this.field); return new LocationWrapper(null, location, this) { Int2D loc = (Int2D) this.location; public Object getObject() { return field.field[loc.x][loc.y]; } public String getLocationName() { return ((Int2D)this.location).toCoordinates(); } }; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -