我开发了一个软件解决方案来直观地连接 Java 组件,它工作正常。我现在唯一的问题是我的仪表板被 jscrollpane 包围,这很好。但是,如果我离开仪表板以使 jscrollpane 必须增长,则为连接组件而绘制的所有线都将从它们的位置移动。我已经搜索了获取 jscollpane 实际屏幕大小的方法,但一无所获。
如果需要,我会使用以下方法来增加 jscrollpane:
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);
拖动鼠标时。
所以我需要在 jscrollpane 中获取左上点的坐标来计算新坐标或将线条正确向后移动的东西。
要绘制我使用的线条:
@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);
}
}
谢谢你的帮助
这里是 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);
}
}