简体   繁体   English

Java 自己的组件通过拖放进行可视化连接

[英]Java own components visually connecting per drag and drop

I'm working on a software solution for a small workflow editor.我正在为小型工作流编辑器开发软件解决方案。 For this I created an own JPanel with some functionality like deleting itself or editing the main information.为此,我创建了一个自己的JPanel具有一些功能,例如删除自身或编辑主要信息。

This is how it looks:这是它的外观:

可移动组件

The point is, that i need a dynamic connector like a arrow or something like that.关键是,我需要一个像箭头之类的动态连接器。 I tried it with drawline but its not dynamic and looks not well.我用画线试过,但它不是动态的,看起来不太好。 I mean if I move one of the boxes so the drawed line have to change its position too.我的意思是如果我移动其中一个框,那么绘制的线也必须改变它的位置。

The boxes in the big JPanel are movable and resizable.JPanel中的框是可移动和可调整大小的。 The connection point right and left are JButton s.左右连接点是JButton The structure is that any outgoing connections startes from the right and incomes to the left JButton .结构是任何传出连接从右边开始,从左边开始JButton

Any ideas how to set it up?任何想法如何设置它?

I can't post much of the source code, because the software is for a company.我不能发布很多源代码,因为该软件是为一家公司提供的。

Did you convert Graphics object to Graphics2D and set the RenderHints ?您是否将Graphics对象转换为Graphics2D并设置了RenderHints ie IE

Graphics2D g2d=(Graphics2D)g;   
g2d.setRenderingHint(RenderHints.KEY_ANTIALIASING,RenderHints.VALUE_ANTIALIASING _ON);

This will add some nice anti aliasing effects and might make the line appearance straighter.这将添加一些不错的抗锯齿效果,并可能使线条看起来更直。

Also increasing the stroke width via Graphics2D#setStroke will make the jaggeder edges disappear as its now thicker.同样通过Graphics2D#setStroke增加笔触宽度将使锯齿状边缘消失,因为它现在更厚。

See this example (press, drag and release mouse to create a line):请参阅此示例(按下、拖动并释放鼠标以创建一条线):

With g2d.setRenderingHint(..) and g2d.setStroke(..) within paintComponent(..) commented out:g2d.setRenderingHint(..)g2d.setStroke(..)paintComponent(..)注释掉:

在此处输入图片说明

With g2d.setRenderingHint(..) and g2d.setStroke(..) uncommented:取消注释g2d.setRenderingHint(..)g2d.setStroke(..)

在此处输入图片说明

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Test gui = new Test();
            }
        });
    }

    public Test() {
        initComponents();
    }

    private void initComponents() {
        JFrame frame = new JFrame("Line Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new MyPanel());

        frame.pack();
        frame.setVisible(true);
    }
}

class MyPanel extends JPanel {

    Point point1;
    Point point2;
    Line2D line2d;

    public MyPanel() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                super.mousePressed(me);
                point1 = me.getPoint();
            }
        });
        addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent me) {
                super.mouseDragged(me);
                point2 = me.getPoint();
                line2d = new Line2D.Double(point1, point2);
                repaint();
            }
        });
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        //Set  anti-alias!
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        if (point1 != null && point2 != null) {
            g2d.setPaint(Color.RED);
            g2d.setStroke(new BasicStroke(1.5f));//set stroke size
            g2d.draw(line2d);
        }
    }
}

If above does not help, posting an SSCCE would enable us to test and see what could be at fault/make it better.如果以上没有帮助,发布 SSCCE 将使我们能够测试并查看可能有什么问题/使其更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM