1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
public class graphe extends JApplet implements MouseListener,ActionListener{
static somet[] tab;
static int nb_sommets=0;
static int sometselect=-1;
static boolean creationarc,supprimerarc=false;
static int dep,arr;//pour creer ou supprimer des arcs
Component compselect=null;
JMenuBar mbar;
JPopupMenu popupsomet;
JPopupMenu popupgraphe;
protected JTextField textfield;
protected int xCurseur;
protected int yCurseur;
public void init(){
tab=new somet[100];
setBackground(Color.white);
setName("fond");
setLayout(null);
setSize(600,600);
setVisible(true);
addMouseListener(this);
mbar = new JMenuBar();
popupsomet = new JPopupMenu("?");popupsomet.setBorderPainted(true);popupsomet.setBackground(Color.red);
popupgraphe = new JPopupMenu("??");popupgraphe.setBackground(Color.gray);
textfield = new JTextField(30);
//JScrollPane jsPane = new JScrollPane(textfield);
mbar.add (makeMenuItem ("Creer nouveau sommet?") );
mbar.add (makeMenuItem ("effacer tout?") );
mbar.add(textfield);
popupsomet.add (makeMenuItem ("Renommer") );
popupsomet.add (makeMenuItem ("creer un arc partant de ce somet?") );
popupsomet.add (makeMenuItem ("Supprimer ce sommet") );
popupsomet.add (makeMenuItem ("Supprimer un arc partant de ce sommet") );
popupsomet.add (makeMenuItem ("..") );
popupsomet.add (makeMenuItem (".") );
popupgraphe.add(makeMenuItem ("Creer nouveau sommet?") );
popupgraphe.add (makeMenuItem ("..") );
setJMenuBar(mbar);
}
private JMenuItem makeMenuItem (String s) {
JMenuItem item = new JMenuItem (s);
item.addActionListener ( this );
return item;
}
public void saisie_suc(int dep, int arr, int cout) {
int i = 0;
//int k = this.rech_indice(dep);
while (tab[dep].liste_succ[i][0] >=0) {
i = i + 1;
}
tab[dep].liste_succ[i][0] = arr;
tab[dep].liste_succ[i][1] = cout;
}
public void paint(Graphics g){
for (int i = 0; i < nb_sommets; i++) {
if (tab[i]!= null) {
int j = 0;
while (tab[i].liste_succ[j][0] >=0) {
//String s = tab[i].acces_liste_succ()[j][0];
//int k = this.rech_indice(s);
this.drawArrow(tab[i].getX() +25, tab[i].getY() +25,
tab[tab[i].liste_succ[j][0]].getX()+25 , tab[tab[i].liste_succ[j][0]].getY()+25 , g);
j = j + 1;
}
}
}
}
public void update(Graphics g)
{
try {
Thread.sleep(10);
repaint();
} catch (InterruptedException err) {
// TODO Auto-generated catch block
err.printStackTrace();
}
paint(g);
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e){
xCurseur=e.getX();
yCurseur=e.getY();
if(e.getButton()==MouseEvent.BUTTON3 ){
popupgraphe.setVisible(true);
popupgraphe.show (e.getComponent(), e.getX (), e.getY ());
}
}
public void mousePressed2(MouseEvent e) {
compselect=e.getComponent();
if(e.getComponent().getClass().getName().equals("somet")){
sometselect=((somet) e.getComponent()).indice;
System.out.println(((somet)e.getComponent()).indice);
}
System.out.println(e.getComponent().getName());
if(e.getButton()==MouseEvent.BUTTON3 ){
popupsomet.setVisible(true);
popupsomet.show (e.getComponent(), e.getX (), e.getY ());
}
if (creationarc){
String cout = JOptionPane.showInputDialog("quel coût?","0");
int cc=0;
try{
cc=Integer.parseInt(cout);
}catch( NumberFormatException nfe){
textfield.setText("entrez un coût entier svp..");
}
saisie_suc(dep,sometselect,cc);
creationarc=false;
}
if (supprimerarc){
tab[dep].liste_succ[sometselect][0]=-1;
tab[dep].liste_succ[sometselect][1]=-1;
supprimerarc=false;
}
repaint();
}
public void mouseReleased(MouseEvent e) {
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand ();
if(s.equals("Creer nouveau sommet?")){
int i=0;
while (tab[i] != null) {
i++;
}
System.out.println(i +".."+nb_sommets);
if (i>=nb_sommets){
tab[i]=new somet (xCurseur,yCurseur,getContentPane(),this);
nb_sommets++;
tab[i].indice=i;
}
else {
tab[i] = new somet (xCurseur,yCurseur,getContentPane(),this);
tab[i].indice=i;
}
repaint();
}
if (s.equals ("Renommer") ){
String ans = JOptionPane.showInputDialog("nouveau nom?",compselect.getName());
compselect.setName(ans);
repaint();
}
if (s.equals ("creer un arc partant de ce somet?") ){
dep=sometselect;creationarc=!creationarc;
}
if (s.equals ("Supprimer un arc partant de ce sommet") ){
dep=sometselect;supprimerarc=!supprimerarc;
}
if (s.equals ("Supprimer ce sommet") ){
//Visible=false;
if (compselect.getClass().getName().equals("somet") ){
remove(compselect);//compselect=null;
repaint();
}
}
if (s.equals ("Blue"))
setBackground (Color.blue);
else
setBackground (Color.white);
} |
Partager