Print

Print


Javier, the fact that you're storing a java.awt.Rectangle in your field is immaterial to MASON.  You could have stored a String there, or a Socket, for all MASON cared.  In Continuous2D, simple portrayals like RectanglePortrayal2D simply represent the objects they're portraying by (typically) drawing some kind of shape which fills a 1x1 square region centered at the object's location.  That's why you're seeing a square: because that's what you told MASON to do.

If you want to draw a rectangle of some sort, you'll need to customize RectanglePortrayal2D to draw a rectangle as appropriate.  Let's say you want to use your Rectangle object's width and height (you'll ignore its x and y since you already have a specified location for it).  You could override the draw method to do something like this:

    public void draw(Object object, Graphics2D graphics, DrawInfo2D info)
        {
	Rectangle obj = (Rectangle)object;     // I added this line
        Rectangle2D.Double draw = info.draw;
        final double width = draw.width*scale * obj.width;  // I modified this line
        final double height = draw.height*scale * obj.height;  // I modified this line

        graphics.setPaint(paint);
        if (info.precise)
            {
            if (preciseRectangle == null) preciseRectangle= new Rectangle2D.Double();
            preciseRectangle.setFrame(info.draw.x - width/2.0, info.draw.y - height/2.0, width, height);
            if (filled) graphics.fill(preciseRectangle);
            else graphics.draw(preciseRectangle);
            return;
            }
                
        final int x = (int)(draw.x - width / 2.0);
        final int y = (int)(draw.y - height / 2.0);
        final int w = (int)(width);
        final int h = (int)(height);

        if (filled) graphics.fillRect(x,y,w,h);
        else graphics.drawRect(x,y,w,h);
        }
       
You can do a similar thing for the hit testing function as well.

Given that you're trying to specify rectangles and draw them, I think you may be misunderstanding the purpose of MASON's SimplePortrayal facility.  The default simple portrayals are really designed for small squarish or point objects; if you want something else you have to make your own custom portrayal.

Sean




On Jan 28, 2014, at 11:38 AM, Kuu <[log in to unmask]> wrote:

> Hello everyone,
> 
> I have the following problem. I'm painting a Continuous2D world (this is the field that I paint) and I have a region in this world that I want to draw in another color, as well as use it as a special zone for my agents.
> 
> I have the information of this region stored in a class that I defined, called "Rectangle" (as the shape is a rectangle, so original name :-) ). With the position and the size. Then, what I'm trying to do is to paint the zone as follows:
> 
> In the start() method of my SimState I've done:
> 
> world.setObjectLocation(AREA, new Double2D(AREA.getX(), AREA.getY()));
> 
> While in setupPortrayals() in the GUIState class I've done:
> 
> entityPortrayal.setPortrayalForClass(Rectangle.class, new RectanglePortrayal2D() {
>             @Override
>             public void draw(Object object, Graphics2D graphics, DrawInfo2D info) {
>                 paint = new Color(0f, 0f, 0f, 0.3f);
>                 super.draw(object, graphics, info);
>             }
>         });
> 
> With this line I'm drawing a semi-transparent black rectangle, which center is located in the position that I gave. However, it is drawn as a square (and a big one by the way). 
> The point is that I don't know how to specify the shape and size of this rectangle so it is drawn properly. I've been trying to look into the MASON examples but I don't get it...
> 
> I'm also wondering how the "getNeighbors" would work if I want to know if my agent is in the zone (actually I don't need to access to this information this way as the rectangle is fixed to the same position and I can do the calculus about the zone manually).
> 
> Thanks in advance for the help!
> 
> Regards,
> Javier