我尝试在JGraphX中移动图形。我的第一次尝试是使用setGeometry,但这也会移动整个坐标系及其原点。所以这对我来说不是一个选择,因为移动几个图表时有点棘手。
我的第二次尝试
Object[] ver = graph.getChildVertices(graph.getDefaultParent());
graph.moveCells(ver, 100, 100, false);
移动所有细胞。到目前为止一切顺利,但边缘起点和终点改变了它的位置。
移动细胞后:
在移动单元格之前,边缘的位置正确。
那么如何将边缘的位置设置回正常的起点和终点? 或者,我感谢将细胞移动到给定位置的任何其他方法。
编辑:如果我之前添加了一个布局,似乎只会发生此行为。如果没有布局,移动单元格将按预期工作。
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
30);
Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
80, 30);
graph.insertEdge(parent, null, "Edge", v1, v2);
}
finally
{
graph.getModel().endUpdate();
}
new mxHierarchicalLayout(graph).execute(graph.getDefaultParent());
graph.getModel().beginUpdate();
try
{
Object[] ver = graph.getChildVertices(graph.getDefaultParent());
graph.moveCells(ver, 100, 100, false);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
答案 0 :(得分:1)
我通读了这个JGraphx的api文档,并从xGraph类找到了有趣的属性:
<强> mxGraph.prototype.resetEdgesOnMove:强>
指定在移动连接的单元格后是否应重置边缘控制点。默认为false。
我会尝试切换它,看看它是否可能在这种情况下有所帮助。至少这会影响移动条件下的边缘行为。
答案 1 :(得分:0)
使用 graph.getChildCells()移动顶点和边:
Object[] ver = graph.getChildCells(graph.getDefaultParent());
graph.moveCells(ver, 100, 100, false);
答案 2 :(得分:0)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class GUI {
public GUI() {
JFrame frame = new JFrame(); // making the JFrame
frame.setTitle("Donate Today!"); // sets JFrame title
// Make the content pane with a set layout
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout(5,5)); // makes a layout for aesthetics/organization
// make right-hand panel where the donation form will appear
JPanel right = new JPanel(new GridLayout(3,2,5,5));
right.setPreferredSize(new Dimension(400,160));
JLabel lblamount = new JLabel("Donation");
JLabel lblgender = new JLabel("Gender");
JTextField amount = new JTextField();
JRadioButton radioButton = new JRadioButton("Female");
JRadioButton radioButton_1 = new JRadioButton("Male");
JRadioButton radioButton_2 = new JRadioButton("Other");
ButtonGroup b1 = new ButtonGroup();
b1.add(radioButton);
b1.add(radioButton_1);
b1.add(radioButton_2);
right.add(lblamount);
right.add(amount);
right.add(lblgender);
JPanel rButtons = new JPanel();
rButtons.add(radioButton);
rButtons.add(radioButton_1);
rButtons.add(radioButton_2);
right.add(rButtons);
// makes the clear button
JButton btnClear = new JButton("Clear");
btnClear.setPreferredSize(new Dimension(10,10));
// makes the submit button
JButton btnSubmit = new JButton("Submit");
btnSubmit.setPreferredSize(new Dimension(10,10));
// adds both buttons to the form JPanel
right.add(btnClear);
right.add(btnSubmit);
// make left-hand panel where the button menu selections will appear
JPanel left = new JPanel(new GridLayout(3,1,5,5)); // specifies a grid layout for theh buttons
left.setPreferredSize(new Dimension(195,225));
String arr[] = new String[] {"Budgeting", "About Us", "View Graph"};
for (int i=0; i<arr.length; i++) { // loops through the above array
left.add(new JButton(arr[i])); //add button to lower pane
}
// adds everything to the JFrame
contentPane.add(left, BorderLayout.WEST); //adding panel 1 to the top of the frame
contentPane.add(right, BorderLayout.EAST); //adding panel 2 to the center of the frame
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
只是移动所说的单元格,而是尝试做:
graph.moveCells(<params>);
这应该移动所述单元格及其子元素和边缘