Search code examples
javascalagraphics2d

Fill an area inside two CubicCurve2D.Float s and a line


I'm drawing a tab in a user interface. I have the outline as I want it. How do I fill the area?

This is the code that draws the border of the tab:

val g2 = g.asInstanceOf[Graphics2D]

g2.translate(x, y)
val q = new CubicCurve2D.Float
q.setCurve(0, h, 8, h, 6, 0, 16, 0)
g2.setColor(Color.RED)
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON)
g2.draw(q)
val end = w - 8
g2.drawLine(17, 0, end, 0)
q.setCurve(end, 0, end+10, 0, w, h, w + 8, h)
g2.draw(q)

and this is the line it draws (the red one): image

I would like to be able to fill the inside of the red line.


Solution

  • I don't know Scala, but in Java 2D, the Graphics2D object can fill or draw the outline of any Shape object. For some arbitrary shape you would define it with GeneralPath object, such as:

    GeneralPath path = new GeneralPath();
    path.lineTo(10, 10);
    path.lineTo(0, 10);
    path.lineTo(0, 0);
    graphics.setColor(Color.RED);
    graphics.fill(path);
    

    The GeneralPath object also has methods for drawing bezier curves and quads, so you would draw the path and then choose to fill or draw the outline of it.

    Added new link to GeneralPath