我开发了一个可视化连接Java组件的软件解决方案,它运行正常。我现在唯一的问题是我的仪表板被jscrollpane包围,这很好。但是如果我离开仪表板以便jscrollpane必须增长,那么绘制连接组件的所有线都会从它们的位置移动。我已经搜索了获取jscollpane真实屏幕大小的方法,但一无所获。
如果需要,我可以使用:
来增长jscrollpanePoint 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);
}
}
答案 0 :(得分:4)
paintComponent()
panel
的{{1}},而不是paint()
的{{1}}
JFrame
完全删除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();
}
});
}
}
方法。