import javax.swing.*; import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class graphe extends Applet implements MouseListener,ActionListener{ /** * */ private static final long serialVersionUID = 1L; static somet[] tab; static int nb_sommets=0; static int sometselect=-1; boolean creationarc; boolean supprimerarc=false; static int dep,arr;//pour creer ou supprimer des arcs Component compselect=null; MenuBar mbar; JPopupMenu popupsomet; JPopupMenu popupgraphe; JComboBox box1,box2; Panel panel; protected TextField textfield; protected int xCurseur; protected int yCurseur; public graphe(){ tab=new somet[100]; setBackground(Color.white); setName("fond"); setLayout(null); setSize(600,600); setVisible(true); addMouseListener(this); panel=new Panel(); panel.setSize(getWidth(),30);panel.setBackground(Color.lightGray); mbar = new MenuBar(); popupsomet = new JPopupMenu("?"); popupgraphe = new JPopupMenu("??"); textfield = new TextField("<=0) { i = i + 1; } tab[dep].liste_succ[i][0] = arr; tab[dep].liste_succ[i][1] = cout; } public void paint(Graphics g){ super.paint(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; } public static void main(String[] args) { Frame f=new Frame("test"); graphe g=new graphe(); f.add("Center",g); f.setVisible(true); } }