我有两个 JPanel,我想单击第一个面板,然后单击第二个面板,并在两个面板之间绘制一条直线。此外,放置后,该线必须保留在两个面板之间。有人可以告诉我从哪里开始吗?这两个面板放置在第三个面板上,它们是下图中的蓝色矩形。先感谢您。
父 JPanel
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
class WorkflowPanel extends JPanel {
private volatile int screenX = 0;
private volatile int screenY = 0;
private static final int RADIUS = 35;
private int radius = RADIUS;
private ArrayList<ModelView> relationship;
WorkflowPanel() {
relationship = new ArrayList<>();
relationship.add(new ModelView());
relationship.add(new ModelView());
add(relationship.get(0));
add(relationship.get(1));
setLayout(null);
setVisible(true);
}
@Override
protected void paintChildren(Graphics g) {
for (int i = 0; i < relationship.size(); i += 2) {
ModelView one = relationship.get(i);
ModelView two = relationship.get(i + 1);
Point p1 = new Point(one.getLocation().x + one.getWidth() / 2, one.getLocation().y + one.getHeight() / 2);
Point p2 = new Point(two.getLocation().x + two.getWidth() / 2, two.getLocation().y + two.getHeight() / 2);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
this.repaint();
}
super.paintChildren(g);
}
public ArrayList<ModelView> getRelationship() {
return relationship;
}
}
POPMUISE
相关分类