Home » Eclipse Projects » GEF » Creating custom shapes
Creating custom shapes [message #147244] |
Wed, 11 August 2004 14:29 |
Eclipse User |
|
|
|
Originally posted by: none.bphnx.com
In one of my EditParts' createFigure() methods, I need to create a diamond
shape.
So I extended the Polygon like this:
class Diamond extends Polygon
{
public Diamond()
{
PointList points = new PointList(4);
points.addPoint(50,0);
points.addPoint(0,50);
points.addPoint(50,50);
points.addPoint(100,50);
setPoints(points);
setOpaque(true);
}
}
then I return a new Diamond() in the createFigure() method.
However, it draws a triangle (huh? I have 4 points, not 3), and the figure
is positioned in the upper left corner of the drawing and cannot be moved.
All of my other shapes work fine, i.e. Ellipse, Rectangle, etc. so why
doesn't this work?
many thanks,
B
|
|
|
Re: Creating custom shapes [message #147284 is a reply to message #147244] |
Wed, 11 August 2004 15:08 |
Eclipse User |
|
|
|
Originally posted by: none.us.ibm.com
The points are incorrect.
Polygon does not implement translate or setBounds. It cannot be used in the
way you are using it.
I recommend that you subclass Shape, and implement drawShape and fillShape
to simply do:
PointList points = new PointList(4);
Rectangle r = getBounds();
points.addPoint(r.getTop());
points.addPoint(r.getRight());
.......
"bandrews" <none@bphnx.com> wrote in message
news:cfdafk$1cv$1@eclipse.org...
> In one of my EditParts' createFigure() methods, I need to create a diamond
> shape.
> So I extended the Polygon like this:
>
> class Diamond extends Polygon
> {
> public Diamond()
> {
> PointList points = new PointList(4);
> points.addPoint(50,0);
> points.addPoint(0,50);
> points.addPoint(50,50);
> points.addPoint(100,50);
> setPoints(points);
> setOpaque(true);
> }
> }
>
> then I return a new Diamond() in the createFigure() method.
> However, it draws a triangle (huh? I have 4 points, not 3), and the figure
> is positioned in the upper left corner of the drawing and cannot be moved.
> All of my other shapes work fine, i.e. Ellipse, Rectangle, etc. so why
> doesn't this work?
>
>
> many thanks,
>
> B
>
|
|
|
Re: Creating custom shapes [message #147292 is a reply to message #147244] |
Wed, 11 August 2004 15:09 |
Pratik Shah Messages: 1077 Registered: July 2009 |
Senior Member |
|
|
The last three points you are giving it are on the same line (all have y =
50). So, the baseline of the triangle actually has 3 pts. Change 50,50 to
50,100.
"bandrews" <none@bphnx.com> wrote in message
news:cfdafk$1cv$1@eclipse.org...
> In one of my EditParts' createFigure() methods, I need to create a diamond
> shape.
> So I extended the Polygon like this:
>
> class Diamond extends Polygon
> {
> public Diamond()
> {
> PointList points = new PointList(4);
> points.addPoint(50,0);
> points.addPoint(0,50);
> points.addPoint(50,50);
> points.addPoint(100,50);
> setPoints(points);
> setOpaque(true);
> }
> }
>
> then I return a new Diamond() in the createFigure() method.
> However, it draws a triangle (huh? I have 4 points, not 3), and the figure
> is positioned in the upper left corner of the drawing and cannot be moved.
> All of my other shapes work fine, i.e. Ellipse, Rectangle, etc. so why
> doesn't this work?
>
>
> many thanks,
>
> B
>
|
|
|
Re: Creating custom shapes [message #147316 is a reply to message #147244] |
Wed, 11 August 2004 16:03 |
Eclipse User |
|
|
|
Originally posted by: none.bphnx.com
The PointList was incorrect, but even after correcting it, I still could
not move the shape in the editor for the reason Randy described. So my
Diamond class ended up like this, which works just fine! Thanks for the
advice!
public class Diamond extends Shape
{
private PointList diamond;
public Diamond()
{
}
protected void fillShape(Graphics graphics)
{
diamond = new PointList(4);
Rectangle r = getBounds();
diamond.addPoint(r.getTop());
diamond.addPoint(r.getLeft());
diamond.addPoint(r.getBottom());
diamond.addPoint(r.getRight());
graphics.fillPolygon(diamond);
}
protected void outlineShape(Graphics graphics)
{
graphics.setForegroundColor(graphics.getBackgroundColor());
if (diamond != null)
graphics.drawPolygon(diamond);
}
}
bandrews wrote:
> In one of my EditParts' createFigure() methods, I need to create a diamond
> shape.
> So I extended the Polygon like this:
> class Diamond extends Polygon
> {
> public Diamond()
> {
> PointList points = new PointList(4);
> points.addPoint(50,0);
> points.addPoint(0,50);
> points.addPoint(50,50);
> points.addPoint(100,50);
> setPoints(points);
> setOpaque(true);
> }
> }
> then I return a new Diamond() in the createFigure() method.
> However, it draws a triangle (huh? I have 4 points, not 3), and the figure
> is positioned in the upper left corner of the drawing and cannot be moved.
> All of my other shapes work fine, i.e. Ellipse, Rectangle, etc. so why
> doesn't this work?
> many thanks,
> B
|
|
|
Goto Forum:
Current Time: Sun Dec 22 06:09:08 GMT 2024
Powered by FUDForum. Page generated in 0.03078 seconds
|