ovalportrayal2d.java

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

JAVA
55
字号
package sim.portrayal.simple;import sim.portrayal.*;import java.awt.*;import java.awt.geom.*;/**   A simple portrayal for 2D visualization of ovals. It extends the SimplePortrayal2D and   it manages the drawing and hit-testing for oval shapes.*/public class OvalPortrayal2D extends SimplePortrayal2D    {    public Paint paint;    public double scale;        public OvalPortrayal2D() { this(Color.gray,1.0); }    public OvalPortrayal2D(Paint paint) { this(paint,1.0); }    public OvalPortrayal2D(double scale) { this(Color.gray,scale); }        public OvalPortrayal2D(Paint paint, double scale)        {        this.paint = paint;        this.scale = scale;        }        // 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.fillOval(x,y,w, h);        //System.out.println(""+x+" "+y+" "+w+" "+h);        }    /** If drawing area intersects selected area, add last portrayed object to the bag */    public boolean hitObject(Object object, DrawInfo2D range)        {        final double SLOP = 1.0;  // need a little extra area to hit objects        final double width = range.draw.width*scale;        final double height = range.draw.height*scale;        Ellipse2D.Double ellipse = new Ellipse2D.Double( range.draw.x-width/2-SLOP, range.draw.y-height/2-SLOP, width+SLOP*2,height+SLOP*2 );        return ( ellipse.intersects( range.clip.x, range.clip.y, range.clip.width, range.clip.height ) );        }    }

⌨️ 快捷键说明

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