⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sparsegridportrayal2d.java

📁 MASON代表多主体邻里或网络仿真(Multi-Agent Simulator of Neighborhoods or Networks)。它是乔治梅森大学用Java开发的离散事件多主体仿真核心库
💻 JAVA
字号:
package sim.portrayal.grid;import sim.portrayal.*;import sim.portrayal.simple.*;import sim.field.grid.*;import sim.util.*;import java.awt.*;import java.util.*;import java.awt.geom.*;/**   Can be used to draw both continuous and descrete sparse fields*/public class SparseGridPortrayal2D extends FieldPortrayal2D    {    public DrawPolicy policy;    public SparseGridPortrayal2D()        {        super();        }    public SparseGridPortrayal2D (DrawPolicy policy)        {        super();        this.policy = policy;        }    // a grey oval.  You should provide your own protrayals...    SimplePortrayal2D defaultPortrayal = new OvalPortrayal2D();    public Portrayal getDefaultPortrayal()        {        return defaultPortrayal;        }    public void setField(Object field)        {        dirtyField = true;        if (field instanceof SparseGrid2D ) this.field = field;        else throw new RuntimeException("Invalid field for Sparse2DPortrayal: " + field);        }            protected void hitOrDraw(Graphics2D graphics, DrawInfo2D info, Bag putInHere)        {        final SparseGrid2D field = (SparseGrid2D) this.field;        if (field==null) return;        int maxX = field.getWidth();         int maxY = field.getHeight();        final double xScale = info.draw.width / maxX;        final double yScale = info.draw.height / maxY;        final int startx = (int)((info.clip.x - info.draw.x) / xScale);        final 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 the person has specified a policy, we have to iterate through the        // bags.  At present we have to do this by using a hash table iterator        // (yuck -- possibly expensive, have to search through empty locations).        if (policy != null)            {            Bag policyBag = new Bag();            Iterator iterator = field.locationBagIterator();            while(iterator.hasNext())                {                Bag objects = (Bag)(iterator.next());                                // restrict the number of objects to draw                policyBag.clear();  // fast                boolean val = policy.objectToDraw(objects,policyBag);                if (val == DrawPolicy.DONE)                    objects = policyBag;                // draw 'em                for(int x=0;x<objects.numObjs;x++)                    {                    final Object portrayedObject = objects.objs[x];                    Int2D loc = field.getObjectLocation(portrayedObject);                    // here we only draw the object if it's within our range.  However objects                    // might leak over to other places, so I dunno...  I give them the benefit                    // of the doubt that they might be three times the size they oughta be, hence the -2 and +2's                    if (loc.x >= startx -2 && loc.x < endx + 4 &&                        loc.y >= starty -2 && loc.y < endy + 4)                        {                        Portrayal p = getPortrayalForObject(portrayedObject);                        if (!(p instanceof SimplePortrayal2D))                            throw new RuntimeException("Unexpected Portrayal " + p + " for object " +                                                        portrayedObject + " -- 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) * loc.x);                        newinfo.draw.y = (int)(info.draw.y + (yScale) * loc.y);                        newinfo.draw.width = (int)(info.draw.x + (xScale) * (loc.x+1)) - newinfo.draw.x;                        newinfo.draw.height = (int)(info.draw.y + (yScale) * (loc.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 (portrayal.hitObject(portrayedObject, newinfo))                                putInHere.add(getWrapper(portrayedObject));                            }                        else                            {                            // MacOS X 10.3 Panther has a bug which resets the clip, YUCK                            graphics.setClip(clip);                            portrayal.draw(portrayedObject, graphics, newinfo);                            }                        }                    }                }            }        else            // the easy way -- draw the objects one by one            {            Bag objects = field.getAllObjects();            for(int x=0;x<objects.numObjs;x++)                {                final Object portrayedObject = objects.objs[x];                Int2D loc = field.getObjectLocation(portrayedObject);                // here we only draw the object if it's within our range.  However objects                // might leak over to other places, so I dunno...  I give them the benefit                // of the doubt that they might be three times the size they oughta be, hence the -2 and +2's                if (loc.x >= startx -2 && loc.x < endx + 4 &&                    loc.y >= starty -2 && loc.y < endy + 4)                    {                    Portrayal p = getPortrayalForObject(portrayedObject);                    if (!(p instanceof SimplePortrayal2D))                        throw new RuntimeException("Unexpected Portrayal " + p + " for object " +                                                    portrayedObject + " -- 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) * loc.x);                    newinfo.draw.y = (int)(info.draw.y + (yScale) * loc.y);                    newinfo.draw.width = (int)(info.draw.x + (xScale) * (loc.x+1)) - newinfo.draw.x;                    newinfo.draw.height = (int)(info.draw.y + (yScale) * (loc.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 (portrayal.hitObject(portrayedObject, newinfo))                            putInHere.add(getWrapper(portrayedObject));                        }                    else                        {                        // MacOS X 10.3 Panther has a bug which resets the clip, YUCK                        graphics.setClip(clip);                        portrayal.draw(portrayedObject, graphics, newinfo);                        }                    }                }            }        }    // The easiest way to make an inspector which gives the location of my objects    public LocationWrapper getWrapper(Object object)        {        final SparseGrid2D field = (SparseGrid2D) this.field;        return new LocationWrapper( object, null, this )  // don't care about location            {            public Object getLocation()                {                if (field==null) return null;                else return field.getObjectLocation(this.object);                }                            public String getLocationName()                {                Object loc = getLocation();                if (loc == null) return "Gone";                else return ((Int2D)loc).toCoordinates();                }            };        }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -