السلام عليكم و رحمة الله و بركاته
أنا بصدد إنجاز مشروع يتمثل في "GRAPH EDITOR" باستخدام لغة الجافا و المكتبة الخارجية "JGraph" و المطلوب هو إنجاز نافذة تؤدي عدة وظائف من ضمنها زرين الأول يدخل عقدا في" الGRAPH" و الثاني يدخل سهما يربط بين عقدتين من العقد التي تم إدخالها سابقا
المشكل يتمثل في كون الزر الثاني لا يعمل أنجزت من أجل التجربة برنامجا بسيطا يدخل في "الGRAPH " عقدتين بواسطة الزر الأول "add a node " و يدخل سهما بينهما في نفس "الGRAPH " بواسطة الزر الثاني و لكن نفس المشكل الزر الثاني لا يعمل و كأن "الGRAPH " لم يقرأ التجديد أو الزيادة
هاهو البرنامج كاملا
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
public class Principal extends JFrame{
private JGraph graph ;
public Principal(){
//the frame construction
super("بسم الله الرحمن الرحيم");
setVisible(true);
setSize(700,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//the pannel
JPanel pannel = new JPanel();
getContentPane().add(pannel,BorderLayout.SOUTH);
//the buttons
JToggleButton b1,b2,b3 ;
b1 = new JToggleButton ("add an node");
b2 = new JToggleButton("add an edge");
b3 = new JToggleButton("afficher le graph ");
pannel.add(b1);
pannel.add(b2);
pannel.add(b3);
//the graph
GraphModel model = new DefaultGraphModel();
GraphLayoutCache view = new GraphLayoutCache(model,new DefaultCellViewFactory());
this.graph = new JGraph (model,view);
//tableau de cellules
final DefaultGraphCell[] cells = new DefaultGraphCell[2];
//l'action des boutons
//b1
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// 1er cellule
cells[0] = new DefaultGraphCell(new String("Hello"));
GraphConstants.setBounds(cells[0].getAttributes(), new Rectangle2D.Double(20,20,40,20));
GraphConstants.setGradientColor(cells[0].getAttributes(),Color.orange);
GraphConstants.setOpaque(cells[0].getAttributes(), true);
DefaultPort port0 = new DefaultPort();
cells[0].add(port0);
cells[1] = new DefaultGraphCell(new String("World"));
GraphConstants.setBounds(cells[1].getAttributes(), new Rectangle2D.Double(140,140,40,20));
GraphConstants.setGradientColor( cells[1].getAttributes(), Color.red);
GraphConstants.setOpaque(cells[1].getAttributes(), true);
DefaultPort port1 = new DefaultPort();
cells[1].add(port1);
graph.getGraphLayoutCache().insert(cells);
getContentPane().add(new JScrollPane(graph));
pack();
setVisible(true);
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
DefaultEdge edge = new DefaultEdge();
edge.setSource(cells[0].getChildAt(0));
edge.setTarget(cells[1].getChildAt(0));
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
graph.getGraphLayoutCache().insertEdge(cells[0],cells[1],edge);
/*getContentPane().add(new JScrollPane(graph));
pack();
setVisible(true);*/
}
});
/* b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
graph.getGraphLayoutCache().insert(cells);
getContentPane().add(new JScrollPane(graph));
pack();
setVisible(true);
}
});*/
}
public static void main(String[] args) {
Principal P = new Principal();
}
}و هاهو الزر الذي لم يعمل
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
DefaultEdge edge = new DefaultEdge();
edge.setSource(cells[0].getChildAt(0));
edge.setTarget(cells[1].getChildAt(0));
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
graph.getGraphLayoutCache().insertEdge(cells[0],cells[1],edge);
/*getContentPane().add(new JScrollPane(graph));
pack();
setVisible(true);*/
}
});