CubicCurve2D 连接两个 JInternalFrame 实例 [英] CubicCurve2D connecting two JInternalFrame instances

查看:24
本文介绍了CubicCurve2D 连接两个 JInternalFrame 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找到一种方法(在 Swing 中)将两个 JInternalFrameCubicCurve2D(也称为三次贝塞尔曲线)连接起来.我试图达到的整体效果是一个类似于 Yahoo!管道(曲线应该从一个内部框架的底部延伸到另一个内部框架的顶部).

I have been trying to find a way (in Swing) to connect two JInternalFrames with a CubicCurve2D (otherwise known as a cubic bezier curve). The overall effect I'm trying to achive is an interface similar to Yahoo! Pipes (the curve should go from the bottom of one internal frame to the top of the other).

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

Has anybody here done this before? The issue I am running into is that I cannot figure how to draw an updating curve in a way which is visible to the user. Drawing to and then repainting JDesktopPane.getGraphics doesn't seem to do anything.

如果可能,我想使用屏幕外缓冲区.

If possible, I would like to use an offscreen buffer.

推荐答案

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

Yes. Here's an example using drawLine(int x1, int y1, int x2, int y2), but invoking draw(Shape s) on your curve is a straightforward extension. You may have to expand the ComponentAdapter to handle resize events, too.

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();
            }
        });
    }
}

这篇关于CubicCurve2D 连接两个 JInternalFrame 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆