1

Whilst I'm familiar with overriding Swing's paintComponent(Graphics g) method and drawing custom shapes, I'd struggling to paint a rectangle with some inverse curves (see https://i.stack.imgur.com/Nc7iT.jpg).

Does anyone know how I'd go about creating the Shape object that'll let me paint this?

Michael Hillman
  • 1,217
  • 3
  • 15
  • 24
  • It's a cheap workaround but I think your best chance would be to just draw an image you made in some paint program. – Name Jan 04 '13 at 16:25
  • Maybe look into using lines, and than allowing curving of the line via click and drag, or if you made your own shape class with `draw(Graphics2D g2d,int x,int y)` method which would than draw that shape using lines at given point on graphics object) – David Kroukamp Jan 04 '13 at 16:29

1 Answers1

3

I believe GeneralPath is a typical way of describing an arbitrary shape.

In your case, it looks like you will have two lines described with lineTo and two (the curves) descibed with quadTo, then call closePath() to represent a closed polygon, something like (just picking convenient coordinates here, you'll probably want something a good deal larger):

GeneralPath polygon = 
    new GeneralPath(GeneralPath.WIND_EVEN_ODD, 4);
polygon.moveTo(2.0, 1.0);
polygon.lineTo(2.0, 5.0);
polygon.quadTo(1.25, 4.75, 1.0, 4.0);
polygon.lineTo(1.0, 2.0);
polygon.quadTo(1.75, 1.75, 2.0, 1.0);
polygon.closePath();
g.draw(polygon);

Also, check out this tutorial on GeneralPath

femtoRgon
  • 32,893
  • 7
  • 60
  • 87