我开发了一个软件解决方案来可视化地连接Java组件,它工作得很好。我现在唯一的问题是,我的仪表板是由一个jscrollpane包围,这很好。但是,如果我离开仪表板,使jscrollpane必须增长,所有绘制的连接组件的线都从它们的位置移动。我搜索了一些方法来获得jscollpane的真实屏幕大小,但一无所获。
如果需要,可以使用:
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);
}
}
覆盖panel
的paintComponent()
而不是JFrame
的paint()
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
public class LineDraw extends JFrame {
private JPanel contentPane;
JScrollPane scrollPane = new JScrollPane();
JPanel panel = new JPanel(){
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(btnNewButton.getLocation().x,btnNewButton.getLocation().y, btnNewButton_1.getLocation().x,btnNewButton_1.getLocation().y);
}
};
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);
validate();
repaint();
}
});
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());
validate();
repaint();
}
});
}
}
并完全删除paint()
方法