rectangleportrayal2d.java

来自「MASON代表多主体邻里或网络仿真(Multi-Agent Simulator 」· Java 代码 · 共 52 行

JAVA
52
字号
package sim.portrayal.simple;import sim.portrayal.*;import java.awt.*;/**   A simple portrayal for 2D visualization of rectangles. It extends the SimplePortrayal2D and   it manages the drawing and hit-testing for rectangular shapes.*/public class RectanglePortrayal2D extends SimplePortrayal2D    {    public Paint paint;    public double scale;    public RectanglePortrayal2D() { this(Color.gray,1.0); }    public RectanglePortrayal2D(Paint paint) { this(paint,1.0); }    public RectanglePortrayal2D(double scale) { this(Color.gray,scale); }        public RectanglePortrayal2D(Paint paint, double scale)        {        this.paint = paint;        this.scale = scale;        }                    /** If drawing area intersects selected area, add last portrayed object to the bag */    public boolean hitObject(Object object, DrawInfo2D range)        {        final double width = range.draw.width*scale;        final double height = range.draw.height*scale;        return( range.clip.intersects( range.draw.x-width/2, range.draw.y-height/2, width, height ) );        }    // assumes the graphics already has its color set    public void draw(Object object, Graphics2D graphics, DrawInfo2D info)        {        final double width = info.draw.width*scale;        final double height = info.draw.height*scale;        graphics.setPaint(paint);        // we are doing a simple draw, so we ignore the info.clip        final int x = (int)(info.draw.x - width / 2.0);        final int y = (int)(info.draw.y - height / 2.0);        final int w = (int)(width);        final int h = (int)(height);        // draw centered on the origin        graphics.fillRect(x,y,w,h);        }            }

⌨️ 快捷键说明

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