开发者

Drawing a line connecting two rectangles

开发者 https://www.devze.com 2023-02-03 05:22 出处:网络
I am making my own class diagram app in Swing/AWT but i stopped at this functionality: I want to draw a line between the Class rectangle that already selected and to the target Class rectangle, but

I am making my own class diagram app in Swing/AWT but i stopped at this functionality:

  • I want to draw a line between the Class rectangle that already selected and to the target Class rectangle, but line has a feature which is whenever i move one of the rectangles the line that join them get bend in a straight fashion following the moving rectangle , i hope the following picture demonstrate what i want to achieve:

    Drawing a line connecting two rectangles

A general guideline or a sample code开发者_如何转开发 is highly appreciated


I don't know Java, but the steps you could follow are these:

  • Find the middle of each line of the rectangles (should be easy, just avarage x1+x2 and y1+y2)
  • Determine the edges that are closest to each other using Pythagoras formula on the points you got in the previous step.
  • Start drawing a line starting at xa,ya (first point you got in the step above), and draw it in the direction away from the rectangle. You should know this direction, because you can know the line segment this point is on.
  • Do the same for xb,yb (point on the second rectangle). If the lines are in opposite directions, you should draw them to halfway xa-xb or ya-yb (depending on if you're drawing horizontally or vertically). If they are perpendicular (is that the right word?) you should draw them to the point where they cross, so you draw the line from xa,ya to xa,yb or xa,ya to xb, ya, depending if you draw the horizontal or vertical line.
  • There should be some additional check to see if the rectangles overlap. You should not draw lines in the same direction for example. Maybe it would suffice for you to draw just a diagonal line between the two points in those cases where you cannot determine how to draw these straight lines.

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


http://java-sl.com/connector.html Hope this helps.


Try with observer pattern. All lines that are connected with a moving object should be notified with new position of the object and 'bent' properly. Of course, first implement some logic that will connect 2 objects.


try creating a class named "ConnectingLine" or similar. this class will then have several segments (that's the name of these line parts in dia, which is currently my favorite uml modeling tool) which will be calculated one by one. you'll have a sepaparate class for this of course ;) called maybe "LineSegment". i think this should make it easier for you to perform the mathematical calculations required to perform this task.

this could also enable making segments "auto routed or not" easy d(^_^)b

0

精彩评论

暂无评论...
验证码 换一张
取 消