0

I am developing an application which draws various shapes on a canvas and fills them with color; simply a painting application using java. I have implemented rectangular selection and filling the shapes. What I want more is, to draw an irregular shape i.e. shape with some curved surfaces, and to fill it with color.

The basic flow for drawing a shape is; first, an outline is drawn using mouse, and once the path is closed, the shapes can be resized by dragging end points. Then, it can be filled with a default color (GRAY) upon clicking a 'Done' button.

Regarding the curve surfaces, I succeeded on drawing outline with curved surfaces, though some bugs are there. Now once I click the 'Done' button, undesired shape(s) gets drawn. Actually, I wanted to fill the inner part of the outline with color, no matter how the outline looks.

Example image here

Code for drawing curve with the help of control points.

public static Path2D.Double createPathWithCurve(ArrayList<EndPointRect> endPoints, ArrayList<MidPointRect> midPoints, MidPointRect midPoint, Point curPoint) {
    Path2D.Double path = new Path2D.Double();

    Point start = midPoint.getStart();
    Point end = midPoint.getEnd();
    int x[] = Utility.fetchArrayOfXFromPoint(endPoints);
    int y[] = Utility.fetchArrayOfYFromPoint(endPoints);

    for(int i = 0; i < x.length; i++) {
        MidPointRect mp = hasCurve(new Point(x[i], y[i]), midPoints);

        if(i == 0) {
            path.moveTo(x[i], y[i]); // Moves drawer to first point
            if(x[i] == start.getX() && y[i] == start.getY()) {
                path.curveTo(start.getX(), start.getY(),
                        curPoint.getX(), curPoint.getY(),
                        end.getX(), end.getY());
                midPoint.setXy(curPoint);
                midPoint.setControlPoint(curPoint);
            }
            else if(mp != null)
                path.curveTo(mp.getStart().getX(), mp.getStart().getY(),
                        mp.getControlPoint().getX(), mp.getControlPoint().getY(),
                        mp.getEnd().getX(), mp.getEnd().getY());
        }
        else if(x[i] == start.getX() && y[i] == start.getY()) {
            path.lineTo(x[i], y[i]);
            path.moveTo(start.getX(), start.getY());
            path.curveTo(start.getX(), start.getY(),
                    curPoint.getX(), curPoint.getY(),
                    end.getX(), end.getY());
            path.moveTo(end.getX(), end.getY());
            midPoint.setXy(curPoint);
            midPoint.setControlPoint(curPoint);
        }
        else {
            if(mp != null) {
                path.lineTo(x[i], y[i]);
                path.moveTo(mp.getStart().getX(), mp.getStart().getY());
                path.curveTo(mp.getStart().getX(), mp.getStart().getY(),
                        mp.getControlPoint().getX(), mp.getControlPoint().getY(),
                        mp.getEnd().getX(), mp.getEnd().getY());
                path.moveTo(mp.getEnd().getX(), mp.getEnd().getY());
            }
            else
                path.lineTo(x[i], y[i]);
        }

        if((i+1) == x.length) {
            path.lineTo(x[0], y[0]);  // Draws line from last point to first point
            //path.closePath();
        }
    }

    return path;
}

'Done' button click method and custom curve class:

public void doneButtonCalled() {
if (!tempRectList.isEmpty()) {
    CustomCurve area = createCurveFromPoints(getCurrentShape(), getTempRectList());
    repaint();
} }

public CustomCurve createCurveFromPoints(Path2D.Double shape, ArrayList<EndPointRect> points) {
int x[] = Utility.fetchArrayOfXFromPoint(points);
int y[] = Utility.fetchArrayOfYFromPoint(points);

return new CustomCurve(shape, x, y, x.length); }

public class CustomCurve extends Area implements ShapeModel, Serializable {
private Color color;
private int[] xPoints;
private int[] yPoints;
private boolean visible;

public CustomCurve(Path2D.Double shape, int[] xPoints, int[] yPoints, int nPoints) {
    super(shape);
    this.xPoints = xPoints;
    this.yPoints = yPoints;
    this.color = Color.darkGray;
    visible = true;
}

/* setters and getters */  }
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). But having said that, I'm not convinced the image is actually needed in order to demonstrate the problem, which could be reproduced by creating the shape in a panel. – Andrew Thompson May 31 '18 at 12:53
  • Thank you so much @AndrewThompson, I should have studied for that standard beforehand. This piece of thing is really bugging me up. If it is still not upto the standard, please do suggest me. – ɐʂʂɐ ʂʂɪȠo Jun 01 '18 at 06:37
  • *"If it is still not upto the standard, please do suggest me."* It's less Complete or Self Contained than ever! I wasn't suggesting to remove images and explanation outside the code. Did you even read the two documents linked? – Andrew Thompson Jun 01 '18 at 10:11
  • Yes, I studied the links you provided, however, I could not understand the second link about hot links, properly. – ɐʂʂɐ ʂʂɪȠo Jun 01 '18 at 10:43
  • I was referring to the two links in the first comment, rather than the link to the page about images in the second. And given the uncompilable code snippets remaining above are neither an MCVE nor SSCCE (which are basically the same thing described in different words) I can only conclude that you have completely misunderstood them! I wrote one, and the first draft of the other, so if there is anything in the documents that you don't understand, now would be a good time to ask. But please be specific about what you don't understand. – Andrew Thompson Jun 01 '18 at 10:48
  • Thanks Andrew for your kindness. I got a basic grasp of MCVE, it might take a while to get into the depth. Since my application is quite huge, I just got lost for what to include and not to in code. Plus, giving the majority of code would not be the point of MCVE. How exactly post my code here? – ɐʂʂɐ ʂʂɪȠo Jun 01 '18 at 11:18
  • *"Plus, giving the majority of code would not be the point of MCVE"* No, it wouldn't, but there's more to MCVE than just the M (for minimal). Plus the point here is not about your entire app., it is about *'creating a logical `Shape` from a series of `Point` objects'*. Create a new (**small**) app. that focuses on that. When you get that working as expected, transferring the technique to the larger app. should be possible. The MCVE / SSCCE should be a self contained code we can (compile & run) to see the existing problem with the `Shape`. It can be a few hundred lines long & still be Minimal. – Andrew Thompson Jun 01 '18 at 11:26
  • Thank you so much Andrew for your clarification. I will be posting the minimal but self sufficient code here soon. In meantime, I am still not able to get the gist of the hot links you mentioned in your first comment. Thanks again for guiding a novice person like me. – ɐʂʂɐ ʂʂɪȠo Jun 01 '18 at 11:32
  • *"I am still not able to get the gist of the hot links you mentioned"* Is the image back drop seen in the original screen shots **actually necessary** to show the problem? AFAIU it only needs a custom painted panel (to draw the dots and the shape). – Andrew Thompson Jun 01 '18 at 11:41
  • Aww, so you mean to say, instead of attaching the actual snapshots, same can be demonstrated from already existing samples or manually drawn references. Thanks. – ɐʂʂɐ ʂʂɪȠo Jun 01 '18 at 11:48
  • *"Aww, so you mean to say,.."* No. I meant to **get an answer** to the question I asked. I don't ask rhetorical questions on this site, so when I ask a question, **provide an answer.** – Andrew Thompson Jun 01 '18 at 12:19
  • Yeah, I thought the actual screenshots might demonstrate the scenario well, no other intentions apart from that. – ɐʂʂɐ ʂʂɪȠo Jun 01 '18 at 13:34
  • No, no. I was ***never*** talking about the screen shots! The screen shots indicated an image was **used in the application.** It was *that* image I meant to replace (if it was actually necessary to demonstrate the problem, which it seems it isn't. And by 'hot link' I meant like in .. (give me a moment to find an example) [this answer](https://stackoverflow.com/a/6296381/418556), which loads the image of a cat directly from the net (hot links to it) in order to make the final effect. It is a good way to get around the user needing to *supply an image* in order to see the example code work! .. – Andrew Thompson Jun 01 '18 at 13:50
  • .. so add the original screen shots back into the question, but **hot link** to a (damn) image if it's needed in order to demonstrate the effect. BTW - I'm really getting the impression that our communications are .. deeply flawed. Unless you can understand me better, my patience for continuing this is waning quickly. I really don't have the time (or patience) for explaining something 10 different ways. – Andrew Thompson Jun 01 '18 at 13:52
  • Thank you so much for your patience over my stupidity. – ɐʂʂɐ ʂʂɪȠo Jun 01 '18 at 15:33

0 Answers0