I developed a software solution to connect Java components visually and it works fine. My only problem now is that my dashboard is surrounded by a jscrollpane and thats good. But if i get out of the dashboard so that the jscrollpane have to grow, all the lines drawed to connect the components are moving from they position. I have search for methods to get the real screen size of the jscollpane but found nothing.
To grow the jscrollpane if needed i use:
Point p = (mainPanel.getMousePosition());
if(mainPanel.getWidth() - (p.x) < tmp.getWidth() && mainPanel.getHeight() - (p.y) < tmp.getHeight())
{
dim = new Dimension(dim.width+(mainPanel.getWidth()-p.x),dim.height+mainPanel.getHeight() - (p.y));
}
else if(mainPanel.getHeight() - (p.y) < tmp.getHeight())
{
dim = new Dimension(dim.width,dim.height+mainPanel.getHeight() - (p.y));
}
else if(mainPanel.getWidth() - (p.x) < tmp.getWidth())
{
dim = new Dimension(dim.width+(mainPanel.getWidth()-p.x),dim.height);
}
mainPanel.setPreferredSize(dim);
While the mouse is dragged.
So i need to get the coordinates of the left top point in the jscrollpane to calculate the new coordinates or something that shifts the lines correct back.
To draw the lines i use:
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// test
Line2D tmp_line;
for (int i = 0; i < abuttons.size(); i++) {
// System.out.println(abuttons.get(i).out);
if (abuttons.get(i).out != null) {
int x1 = abuttons.get(i).getLocation().x
+ abuttons.get(i).connectBtn_right.getLocation().x
+ 12;
int y1 = abuttons.get(i).getLocation().y
+ abuttons.get(i).connectBtn_right.getLocation().y
+ 9 + 75;
int x2 = abuttons.get(i).out.getLocation().x
+ abuttons.get(i).out.connectBtn_left.getLocation().x
+ 12;
int y2 = abuttons.get(i).out.getLocation().y
+ abuttons.get(i).out.connectBtn_left.getLocation().y
+ 9 + 75;
tmp_line = new Line2D.Double(x1, y1, x2, y2);
g2d.setPaint(Color.BLACK);
g2d.setStroke(new BasicStroke(1.5f));
g2d.draw(tmp_line);
}
}
if (point1 != null && point2 != null) {
line2d = new Line2D.Double(point1, point2);
g2d.setPaint(Color.RED);
g2d.setStroke(new BasicStroke(1.5f));// set stroke size
g2d.draw(line2d);
}
}
Thanks for any help
Here the SSCCE:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JButton;
public class LineDraw extends JFrame {
private JPanel contentPane;
JScrollPane scrollPane = new JScrollPane();
JPanel panel = new JPanel();
JButton btnNewButton = new JButton("New button");
JButton btnNewButton_1 = new JButton("New button");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LineDraw frame = new LineDraw();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LineDraw() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
scrollPane.setBounds(0, 0, 424, 251);
contentPane.add(scrollPane);
scrollPane.setViewportView(panel);
panel.setLayout(null);
btnNewButton.setBounds(27, 98, 89, 23);
panel.add(btnNewButton);
btnNewButton_1.setBounds(213, 137, 89, 23);
panel.add(btnNewButton_1);
initListener();
}
private void initListener()
{
btnNewButton.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent me) {
super.mouseDragged(me);
Point pos = panel.getMousePosition();
System.out.println(pos);
if(panel.getWidth()-pos.x<50 && panel.getHeight()-pos.y<50)
{
panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()+50));
}
else if(panel.getHeight()-pos.y<50)
{
panel.setPreferredSize(new Dimension(panel.getWidth(),panel.getHeight()+50));
}
else if(panel.getWidth()-pos.x<50)
{
panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()));
}
btnNewButton.setLocation(pos);
repaint();
validate();
}
});
btnNewButton_1.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent me) {
super.mouseDragged(me);
Point pos = panel.getMousePosition();
if(panel.getWidth()-pos.x<50 && panel.getHeight()-pos.y<50)
{
panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()+50));
}
else if(panel.getHeight()-pos.y<50)
{
panel.setPreferredSize(new Dimension(panel.getWidth(),panel.getHeight()+50));
}
else if(panel.getWidth()-pos.x<50)
{
panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()));
}
btnNewButton_1.setLocation(panel.getMousePosition());
repaint();
validate();
}
});
}
@Override
public void paint(Graphics g)
{
super.paint(g);
g.drawLine(btnNewButton.getLocation().x+10,btnNewButton.getLocation().y+32, btnNewButton_1.getLocation().x+10,btnNewButton_1.getLocation().y+32);
}
}