简体   繁体   中英

Swing paint program - How to handle selection?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM