连接两个 JInternalFrame 实例的 CubicCurve2D

2023-12-15

我一直在尝试找到一种方法(在 Swing 中)连接两个JInternalFrame与一个CubicCurve2D(也称为三次贝塞尔曲线)。我想要实现的总体效果是一个类似于雅虎!管道(曲线应从一个内部框架的底部延伸到另一个内部框架的顶部)。

这里有人以前做过这个吗?我遇到的问题是我无法弄清楚如何以用户可见的方式绘制更新曲线。绘制然后重新绘制JDesktopPane.getGraphics似乎什么也没做。

如果可能的话,我想使用屏幕外缓冲区。


是的。这是一个使用的示例drawLine(int x1, int y1, int x2, int y2),但调用draw(Shape s)在你的曲线上是一个简单的延伸。您可能需要扩展ComponentAdapter也可以处理调整大小事件。

alt text

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/** @see http://stackoverflow.com/questions/3951383 */
public class JDPTest extends JDesktopPane {

    private static final Stroke s = new BasicStroke(4.0f);
    private MyFrame one = new MyFrame("One", 100, 100);
    private MyFrame two = new MyFrame("Two", 400, 240);

    public JDPTest() {
        this.setPreferredSize(new Dimension(640, 480));
        this.add(one);
        this.add(two);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.lightGray);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.setColor(Color.blue);
        g2d.setStroke(s);
        int x1 = one.getX() + one.getWidth() / 2;
        int y1 = one.getY() + one.getHeight() / 2;
        int x2 = two.getX() + two.getWidth() / 2;
        int y2 = two.getY() + two.getHeight() / 2;
        g2d.drawLine(x1, y1, x2, y2);
    }

    private final class MyFrame extends JInternalFrame {

        MyFrame(String name, int x, int y) {
            super(name);
            this.setSize(160, 100);
            this.setLocation(x, y);
            this.setVisible(true);
            this.addComponentListener(new ComponentAdapter() {

                @Override
                public void componentMoved(ComponentEvent e) {
                    JDPTest.this.repaint();
                }
            });
        }
    }

    private void display() {
        JFrame f = new JFrame("JDPTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JDPTest().display();
            }
        });
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

连接两个 JInternalFrame 实例的 CubicCurve2D 的相关文章

随机推荐