Fix to fill scalable polygon node with gradient background [message #636301] |
Sat, 30 October 2010 14:42 |
Nigel Messages: 7 Registered: October 2010 |
Junior Member |
|
|
Hi,
I've seen several questions on how to fill a scalable polygon with a
gradient background and have had a lot of trouble getting this to work
myself. It turns out that all of the time the code I used was working
but there must be a bug in GMF/GEF/Draw2D? that ignores the position of
the node. To work around the bug I used the code below. The code is
located in the ScalablePolygonNodeEditPart->ScalablePolygonNodeFigure class.
/**
* @generated NOT
*/
// MOD fillShape method added to fill with a gradient
@Override
protected void fillShape(Graphics graphics)
{
// Get the current rectangle to set the start and end locations of the
gradient
Rectangle r = getBounds().getCopy();
Point topLeft = r.getTopLeft();
Point bottomRight = r.getBottomRight();
// Create the gradient pattern and set it, I'm going from the
background colour to white
Pattern pattern = new Pattern(Display.getCurrent(), topLeft.x,
topLeft.y, bottomRight.x, bottomRight.y,
ColorConstants.white, THIS_BACK);
graphics.setBackgroundPattern(pattern);
// Now apply the location offset to the polygons point list
PointList pointList = this.getScaledPoints();
PointList positionList = new PointList();
Point location = this.getLocation();
for (int i=0;i<pointList.size();i++)
{
Point p = pointList.getPoint(i);
Point newP = new Point(p.x+location.x, p.y+location.y);
positionList.addPoint(newP);
}
// Fill the polygon and clean up
graphics.fillPolygon(positionList);
graphics.setBackgroundPattern(null);
pattern.dispose();
}
Enjoy :-)
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03406 seconds