3

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.

This is how it looks:

Movable components

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. The connection point right and left are JButtons. The structure is that any outgoing connections startes from the right and incomes to the left JButton.

Any ideas how to set it up?

I can't post much of the source code, because the software is for a company.

mre
  • 43,520
  • 33
  • 120
  • 170
Kingalione
  • 4,237
  • 6
  • 49
  • 84
  • you can try using the `GlassPane`, as shown [here](http://stackoverflow.com/questions/6609888/drawing-between-2-images-in-1-jpanel/6610064#6610064). – mre Jan 11 '13 at 14:13
  • Why don't you use one of the open-source frameworks? like http://gef.tigris.org/ – lbalazscs Jan 11 '13 at 14:15
  • Do you know windowBuilder? Look here [http://www.eclipse.org/windowbuilder/] – erhun Jan 11 '13 at 14:17
  • erhun: i use the windowbuilder but the windowbuilder has no component with the functionality that i need. lbalazscs: my boss dont like it if we use 3rd party solutions. and the panels in the picture or not only panels with informations. they represent datasources and have own displaying methods. so i mean it is not only about the gui programming, cause there is a backend module for each different datasource types. thanks – Kingalione Jan 11 '13 at 14:25
  • Also consider `JHotDraw`, cited [here](http://stackoverflow.com/a/13997870/230513). – trashgod Jan 11 '13 at 23:08

1 Answers1

3

Did you convert Graphics object to Graphics2D and set the RenderHints? i.e

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.

See this example (press, drag and release mouse to create a line):

With g2d.setRenderingHint(..) and g2d.setStroke(..) within 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 above does not help, posting an SSCCE would enable us to test and see what could be at fault/make it better.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • i'll try it out and post than if its that what i looking for. thank you very much – Kingalione Jan 11 '13 at 15:00
  • Thank you David I solved the problem with your help. I referenced the other component as a output component and everytime I repaint the whole panel i look for output!=null if there is a connection between two components. If there is, I connect them your way. – Kingalione Jan 16 '13 at 11:34