I forgot that if you change the size of the portrayal, this affects both the display of the portrayal and its hit-testing. So a revision:
OvalPortrayal2D oval = new OvalPortrayal2D(Color.red)
{
public void draw(Object object, Graphics2D graphics, DrawInfo2D info)
{
MyObjectClass obj = (MyObjectClass) object;
scale = obj.getMyAttribute();
super.draw(object, graphics, info);
}
public boolean hitObject(Object object, DrawInfo2D range)
{
MyObjectClass obj = (MyObjectClass) object;
scale = obj.getMyAttribute();
super.hitObject(object, range);
}
};
.... or .....
double AREA_CONSTANT = 10.0; // or something like that
OvalPortrayal2D oval = new OvalPortrayal2D(Color.red)
{
public void draw(Object object, Graphics2D graphics, DrawInfo2D info)
{
MyObjectClass obj = (MyObjectClass) object;
scale = Math.sqrt(obj.getMyAttribute() * AREA_CONSTANT);
super.draw(object, graphics, info);
}
public boolean hitObject(Object object, DrawInfo2D range)
{
MyObjectClass obj = (MyObjectClass) object;
scale = Math.sqrt(obj.getMyAttribute() * AREA_CONSTANT);
super.hitObject(object, range);
}
};
Maybe I need to set up a getScale() and getColor() method on these things because this request is so common.
Sean
On Aug 2, 2012, at 6:19 PM, Sean Luke wrote:
> Eesh. Never screw around with the affine transform of the graphics object.
>
> 1. METHOD 1. Actually what you want to do can be accomplished in nearly the exact same way as my example below. Each SimplePortrayal2D has a scaling variable. So you can do it like this:
>
> OvalPortrayal2D oval = new OvalPortrayal2D(Color.red)
> {
> public void draw(Object object, Graphics2D graphics, DrawInfo2D info)
> {
> MyObjectClass obj = (MyObjectClass) object;
> scale = obj.getMyAttribute();
> super.draw(object, graphics, info);
> }
> };
>
> This will scale the object exactly according to the attribute.
>
> 2. METHOD 2. To be more accurate, you probably want to scale things so that the *area* of the circle reflects the underlying attribute, not the *diameter* (or radius). This would be something like:
>
>
> double AREA_CONSTANT = 10.0; // or something like that
> OvalPortrayal2D oval = new OvalPortrayal2D(Color.red)
> {
> public void draw(Object object, Graphics2D graphics, DrawInfo2D info)
> {
> MyObjectClass obj = (MyObjectClass) object;
> scale = Math.sqrt(obj.getMyAttribute() * AREA_CONSTANT);
> super.draw(object, graphics, info);
> }
> };
>
> I'd stay away from TransformedPortrayal2D for this task.
>
>
> On Aug 2, 2012, at 8:54 AM, Eric 'Siggy' Scott wrote:
>
>> Similar question. I also have heterogeneous objects to display, but I want to render them with differing sizes.
>>
>> I tried this, but it messes up the locations of the objects horribly, as if I've scaled the entire grid or something:
>>
>> final int maxOvalSize = 10;
>> OvalPortrayal2D myPortrayal = new OvalPortrayal2D(Color.WHITE, maxOvalSize) {
>> @Override
>> public void draw(Object object, Graphics2D graphics, DrawInfo2D drawInfo)
>> {
>> MyObject ob = (MyObject) object;
>> double objectScale = ob.getAttribute();
>> graphics.scale(objectScale, objectScale);
>> super.draw(object, graphics, drawInfo);
>> }
>> };
>>
>> On Wed, Aug 1, 2012 at 4:48 PM, Sean Luke <[log in to unmask]> wrote:
>> On Aug 2, 2012, at 5:19 AM, Luís de Sousa wrote:
>>
>>> I have a SparseGrid harbouring objects of assorted classes. I'd like
>>> to portray one of these classes with a ColourMap, like it is made with
>>> ValueGrid. This way each object would be portrayed with a different
>>> colour gradient depending on a particular attribute. Is this possible
>>> to do? How?
>>
>> SparseGrid does not support color maps directly. But it's easy enough to do it. Choose or create a SimplePortrayal to display your object. Let's say it's OvalPortrayal2D. Then you can say:
>>
>>
>> final SimpleColorMap map = new SimpleColorMap( ....... ); // set it up like you'd like
>>
>> OvalPortrayal2D oval = new OvalPortrayal2D()
>> {
>> public void draw(Object object, Graphics2D graphics, DrawInfo2D info)
>> {
>> MyObjectClass obj = (MyObjectClass) object;
>> double val = obj.getMyAttribute();
>> paint = new Color(map.getColor(val));
>> super.draw(object, graphics, info);
>> }
>> };
>>
>> Then just use 'oval' as your portrayal.
>>
>> Sean
>>
>>
>>
>> --
>> Ph.D. Student in Computer Science
>> Volgenau School of Engineering
>> George Mason University
>>
>
|