Java – draw a line connecting two rectangles

I created my own class diagram application in swing / AWT, but I stopped this function:

>I want to draw a line between the selected class Rectangle and the target class rectangle, but line has a feature that when I move one of the rectangles, the line connecting them bends in a straight line after the moving rectangle. I hope the following picture shows the goal I want to achieve:

We would appreciate a general guide or sample code

Solution

I don't understand java, but the steps you can follow are as follows:

>Find the middle of each row of the rectangle (it should be simple, just avarage X1 x2 and Y1 Y2) > use the Pythagorean formula on the points obtained in the previous step to determine the closest edges to each other. > Draw a line from Xa, ya (the first point obtained in the above step), and then draw a line away from the rectangle You should know this direction because you can know the line segment where this point is located. > Do the same for XB, Yb (the point on the second rectangle) If the lines are in the opposite direction, they should be drawn to the middle of XA XB or ya Yb (depending on whether you draw horizontally or vertically) If they are vertical (are they the correct words?) You should draw them to the point where they cross, so you draw lines from Xa, ya to Xa, Yb or Xa, ya to XB, ya, depending on whether you draw horizontal or vertical lines. > Additional checks should be made to determine whether the rectangles overlap For example, you should not draw lines in the same direction Perhaps it is enough to draw diagonals between two points when you are not sure how to draw these lines

For implementation, you can build a line class that uses observer mode to listen to the two rectangles it follows, so as long as one of them moves or resizes, it can update itself

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>