Native Java components visually connected for every drag and drop

I am working on a software solution for a small workflow editor. To do this, I created my own JPanel with some features, such as deleting myself or editing basic information.

Here's what it looks like:

Movable components

The fact is that I need a dynamic connector, such as an arrow or something like that. I tried this with drawline, but it is not dynamic and does not look very good. I mean, if I move one of the boxes so that the drawn line also changes its position.

The boxes in the large JPanel are movable and resizable. The connection point on the right and left is JButton s. The structure is that any outgoing connections start on the right and the revenues on the left are JButton .

Any ideas how to set it up?

I can’t publish most of the source code because it is software for the company.

+3
source share
1 answer

Have you converted the Graphics object to Graphics2D and set RenderHints ? iee

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

This will add some nice anti aliasing effects and may make the line more straight.

In addition, increasing the stroke width through Graphics2D#setStroke will cause the edges of the gears to disappear, as it will now become thicker.

See this example (click, drag and release the mouse to create a row):

With g2d.setRenderingHint(..) and g2d.setStroke(..) in paintComponent(..) commented out:

enter image description here

With g2d.setRenderingHint(..) and g2d.setStroke(..) uncommented:

enter image description here

 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 the above does not help, publishing SSCCE will allow us to check and see what could be a mistake / make it better.

+3
source

Source: https://habr.com/ru/post/1488780/


All Articles