画一条线连接两个矩形

发布于 2024-10-11 03:04:52 字数 231 浏览 5 评论 0原文

我正在 Swing/AWT 中制作自己的类图应用程序,但我停在了这个功能上:

  • 我想在已选择的类矩形和目标类矩形之间画一条线,但线有一个功能,每当我移动一个线时在矩形中,连接它们的线随着移动的矩形以直线方式弯曲,我希望下图展示了我想要实现的目标: alt text

非常感谢一般指南或示例代码

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:
    alt text

A general guideline or a sample code is highly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒路情人 2024-10-18 03:04:52

我不懂 Java,但您可以遵循以下步骤:

  • 找到矩形每条线的中间(应该很容易,只需平均 x1+x2 和 y1+y2)
  • 使用以下命令确定彼此最接近的边缘根据您在上一步中得到的点得出的毕达哥拉斯公式。
  • 开始从 xa,ya(您在上面步骤中得到的第一个点)开始绘制一条线,并沿着远离矩形的方向绘制它。你应该知道这个方向,因为你可以知道这个点所在的线段。
  • 对 xb,yb (第二个矩形上的点)执行相同的操作。如果线条方向相反,则应将它们绘制到 xa-xb 或 ya-yb 的中间(取决于您是水平绘制还是垂直绘制)。如果它们是垂直的(这是正确的词吗?),您应该将它们绘制到它们交叉的点,因此您绘制从 xa,ya 到 xa,yb 或 xa,ya 到 xb, ya 的线,具体取决于您是否绘制水平或垂直线。
  • 应该进行一些额外的检查来查看矩形是否重叠。例如,您不应沿同一方向绘制线条。在您无法确定如何绘制这些直线的情况下,也许您只在两点之间绘制一条对角线就足够了。

对于实现,您可以构建一个线类,该线类使用观察者模式来侦听它所遵循的两个矩形,因此只要其中一个矩形移动或调整大小,它就可以自我更新。

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.

儭儭莪哋寶赑 2024-10-18 03:04:52

尝试使用 观察者 模式。所有与移动物体连接的线都应收到该物体的新位置通知并正确“弯曲”。当然,首先实现一些连接两个对象的逻辑。

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.

吻安 2024-10-18 03:04:52

尝试创建一个名为“ConnectingLine”或类似名称的类。这个类将有几个线段(这是 dia 中这些线部分的名称,这是目前我最喜欢的 uml 建模工具),这些线段将被一一计算。当然,您将有一个单独的类;)可能称为“LineSegment”。我认为这应该使您可以更轻松地执行执行此任务所需的数学计算。

这也可以使分段“自动路由或不自动路由”变得容易 d(^_^)b

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文