我正在Netbeans中开发一个Java程序,以绘制一个图形树,以创建其节点和边缘并将其绘制到JPanel中。
我只能拖拽节点和边缘,因为图像没有更新-它突然消失了一切,我需要添加节点或边缘以再次观看树。
我发现该错误一定是在Graph.paint()函数中或有关重绘,重新验证...函数的某个东西。
我一直在这里寻找解决方案,但找不到任何真正能解决问题的方法。
GraphPanel 是管理Graph的JPanel。
图是存储所有节点和边的结构
节点和边缘分别是节点和边缘的结构。
我的 GraphPanelMouseClicked 代码是: 我发布了这段代码以向我展示如何绘制节点,等等[这可以正常工作
private void GraphPanelMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.out.println("Click X: " + evt.getX() + " , y: " + evt.getY());
System.out.println("Click On Screen X: " + evt.getXOnScreen() + " , y: " + evt.getYOnScreen());
switch(this.ButtonSelection){
case OriHelperGUI.Select:
break;
case OriHelperGUI.AddNode:
String name = JOptionPane.showInputDialog("Node name: ");
String weight = JOptionPane.showInputDialog("Node weight: ");
Node n = new Node(new Point(evt.getX(), evt.getY()), Integer.parseInt(weight), name);
g.addNode(n);
System.out.println("Nodo " + name + " añadido");
break;
case OriHelperGUI.AddEdge:
for(Node node: g.getNodes()){
if(new Rectangle((int)(node.getPoint().getX() - node.getWeight()/2),(int)(node.getPoint().getY() - node.getWeight()/2), (int)node.getWeight(), (int)node.getWeight()).contains(new Point(evt.getX(), evt.getY()))){
if(this.AddEdgeN1==null){
AddEdgeN1 = node;
System.out.println(node.getName());
}
else{
if(AddEdgeN2==null){
AddEdgeN2 = node;
System.out.println("Uniendo " + AddEdgeN1.getName() + " a " + AddEdgeN2.getName());
g.addEdge(new Edge(AddEdgeN1, AddEdgeN2));
AddEdgeN1.addNeighbour(AddEdgeN2);
AddEdgeN2.addNeighbour(AddEdgeN1);
g.checkEdges();
AddEdgeN1 = null;
AddEdgeN2 = null;
}
}
}
}
break;
default:
break;
}
g.paint(GraphPanel.getGraphics());
GraphPanel.revalidate();
}
我的 GraphPanelMouseDragged 代码是: 我认为这是失败的地方...
private void GraphPanelMouseDragged(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if(this.SelectedNode>=0){
Node old = this.g.getNodes().get(this.SelectedNode);
Node n = new Node(new Point(evt.getX(),evt.getY()),old.getWeight(),old.getName());
g.getNodes().set(this.SelectedNode, n);
g.paint(GraphPanel.getGraphics());
GraphPanel.revalidate();
}
}
我的 Graph paint(g)和paintComponent(g)方法是[不确定更新图像时哪个方法可以工作]:
@Override
public void paint(Graphics g){
g.setColor(Color.black);
super.paint(g);
for(Node n:nodes){
n.paintNode(g);
}
for(Edge e:edges){
e.paintEdge(g);
}
}
public void paintComponent(Graphics g){
g.setColor(Color.black);
super.paint(g);
for(Node n:nodes){
n.paintNode(g);
}
for(Edge e:edges){
e.paintEdge(g);
}
}
节点绘制(g)方法:
public void paintNode(Graphics g) {
super.paint(g); // don't forget to call the super's method
g.drawOval((int)(point.x -(weight/2)), (int)(point.y-(weight/2)), (int)weight, (int)weight);
g.drawString(name, point.x, point.y);
}
@Override
public void paintComponent(Graphics g) {
super.paint(g); // don't forget to call the super's method
g.drawOval((int)(point.x -(weight/2)), (int)(point.y-(weight/2)), (int)weight, (int)weight);
g.drawString(name, point.x, point.y);
repaint();
}
最后,我的 Edge 绘画方法是:
public void paintEdge(Graphics g) {
super.paint(g); // don't forget to call the super's method
g.drawLine((int)n1.getPoint().getX(), (int)n1.getPoint().getY(), (int)n2.getPoint().getX(), (int)n2.getPoint().getY());
}
@Override
public void paintComponent(Graphics g) {
super.paint(g); // don't forget to call the super's method
g.drawLine((int)n1.getPoint().getX(), (int)n1.getPoint().getY(), (int)n2.getPoint().getX(), (int)n2.getPoint().getY());
repaint();
}
我还添加了当前程序的GIF,以更好地理解我的问题。
非常感谢您