| 12
 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
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 
 |  
import javax.swing.*;
 
import java.awt.*;
import java.awt.event.*;
 
 
public class graphe extends JComponent implements MouseListener,ActionListener{
 
	static JFrame jf;
	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 static void main(String[] args) {
		new graphe();
	}
 
 
 
	public graphe(){
 
		jf=new JFrame();
 
		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);popupsomet.setOpaque(true);
		popupgraphe = new JPopupMenu("??");popupgraphe.setBackground(Color.gray);popupgraphe.setOpaque(true);
		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 ("..") );
 
	   jf.setJMenuBar(mbar);
	   jf.setVisible(true);jf.add(this);
 
	}
 
	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 paintComponent(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;
				}
			}
			} 
		repaint();
	}
 
	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 ());
		}
		repaint();
	}
 
	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,this,this);
				nb_sommets++;
				tab[i].indice=i;
			}
			else {
				tab[i] = new somet (xCurseur,yCurseur,this,this);
				tab[i].indice=i;
			}	
		}
 
		if (s.equals ("Renommer") ){
	    	String ans = JOptionPane.showInputDialog("nouveau nom?",compselect.getName());
	    	compselect.setName(ans);
	    }
		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;
	    	}
	    }
	   if (s.equals ("Blue"))
	      setBackground (Color.blue);
	    else
	      setBackground (Color.white);
repaint();
	}
 
 
 
	private void drawArrow(int x1, int y1, int x2, int y2, Graphics g) {
		g.drawPolygon(getArrow(x1, y1, x2, y2, 10, 5, 0.5));
	}
 
	private Polygon getArrow(int x1, int y1, int x2, int y2, int headsize,
			int difference, double factor) {
		int[] crosslinebase = getArrowHeadLine(x1, y1, x2, y2, headsize);
		int[] headbase = getArrowHeadLine(x1, y1, x2, y2, headsize - difference);
		int[] crossline = getArrowHeadCrossLine(crosslinebase[0],
				crosslinebase[1], x2, y2, factor);
 
		Polygon head = new Polygon();
 
		head.addPoint(headbase[0], headbase[1]);
		head.addPoint(crossline[0], crossline[1]);
		head.addPoint(x2, y2);
		head.addPoint(crossline[2], crossline[3]);
		head.addPoint(headbase[0], headbase[1]);
		head.addPoint(x1, y1);
 
		return head;
	}
 
	private int[] getArrowHeadLine(int xsource, int ysource, int xdest,
			int ydest, int distance) {
		int[] arrowhead = new int[2];
		int headsize = distance;
 
		double stretchfactor = 0;
		stretchfactor = 1 - (headsize / (Math
				.sqrt(((xdest - xsource) * (xdest - xsource))
						+ ((ydest - ysource) * (ydest - ysource)))));
 
		arrowhead[0] = (int) (stretchfactor * (xdest - xsource)) + xsource;
		arrowhead[1] = (int) (stretchfactor * (ydest - ysource)) + ysource;
 
		return arrowhead;
	}
 
	private int[] getArrowHeadCrossLine(int x1, int x2, int b1, int b2,
			double factor) {
		int[] crossline = new int[4];
 
		int x_dest = (int) (((b1 - x1) * factor) + x1);
		int y_dest = (int) (((b2 - x2) * factor) + x2);
 
		crossline[0] = (int) ((x1 + x2 - y_dest));
		crossline[1] = (int) ((x2 + x_dest - x1));
		crossline[2] = crossline[0] + (x1 - crossline[0]) * 2;
		crossline[3] = crossline[1] + (x2 - crossline[1]) * 2;
		return crossline;
	}
 
 
} | 
Partager