0

I'm trying to make a paint program using swing. I have several shapes that the user can draw, including ellipses, lines, and rectangles. The shapes can drawn (using graphics2d) with various strokes (line thicknesses) and can be filled or unfilled.

I'm trying to implement a feature such that the user can click a shape with the right mouse button and drag it to move it.

My current strategy is to poll the array of shapes is reverse order (meaning in case of multiple shapes the most recently added one is selected).

Each shape implementation has a method called isSelected(int x, int y). I need to figure out how to determine whether a point falls on the shape. One challenge is that an unfilled shape such as an oval should not be selected when clicked inside, but should be selected if it is filled. Also, selection should respond to the thickness of a stroke. That is, a line should be selectable by clicking out the outer area if it uses a thicker stroke, not just the exact center of the line.

How on earth can I go about implementing this?

Bassinator
  • 1,682
  • 3
  • 23
  • 50
  • The question depends on "how" you are storing the shapes. Java's graphics 2d API supports a concept of `Shape`, with a number of predefined shapes. These contain the ability to determine if a given point falls within their bounds – MadProgrammer Nov 04 '17 at 02:10
  • I am storing an array of a custom type which implements a 2d shape from this API – Bassinator Nov 04 '17 at 02:11

1 Answers1

1

I am storing an array of a custom type which implements a 2d shape from this API

Then you could use:

Depending on the information you have and what you want to check

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • This works, but doesn't handle the logic for filled/unfilled shapes. – Bassinator Nov 04 '17 at 02:18
  • How do you mean? Since I've got no context into how you've actually implemented anything, it's difficult to make further suggestions – MadProgrammer Nov 04 '17 at 02:46
  • @Airhead What was suggested by MadProgrammer should be able to handle filled and unfilled shapes. Have you tested it? – user3437460 Nov 04 '17 at 08:07
  • Yes, I have tested it. I don't want unfilled shapes to be selectable from the inside, which they currently are. – Bassinator Nov 04 '17 at 14:18
  • @Airhead Well, in that case, you just want to test if the mouse point is along the bounds of the shape. This assumes that you your happy to deal with the rectangle bounds of the shape and not the shape itself. If you want to know if they've clicked on the edge of circle (for example) it becomes much more complex, then you need to perform some intersection calculations. I'd calculate the angle from the centre of the shape to the mouse point, then I'd calculate the point in the circle at which this angle would represent and then determine if the mouse point falls within a specified range of it – MadProgrammer Nov 04 '17 at 20:54
  • @Airhead Luckily, I've already done something similar to this [for example](https://stackoverflow.com/questions/26476645/resizing-path2d-circle-by-clicking-and-dragging-its-outer-edge/26476909#26476909) – MadProgrammer Nov 04 '17 at 20:55
  • A thought - what if I were to create a second (unseen) ellipse for a moment, slightly larger or smaller to account for the line thickness. As long as the ellipse is positioned appropriately on top of the other, I could then check that the point is inside one ellipse, but not the other. Do you think this would work? – Bassinator Nov 05 '17 at 00:28
  • @Airhead You'd have to try and see if that worked for your needs – MadProgrammer Nov 05 '17 at 00:28