Bonjour,
j'ai une problème dans mon code en fait je n'arrive pas à écrire l'élément choisi a partir de mon jliste devant la flèche dessinée , donc je doit utiliser le y du flèche pas le y récupéré au niveau de mouse released ou mouse pressed et ça ce qu'il fait mon code ? J'ai essayé avec deux méthode que j'ai crée (arrow_test et savearrow_test) mais en vain , j'ai une autre problème : l'élément sélectionner se disparait à chaque nouvelle dessin du fleche :\
voici mon code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 *
 * @author W.S.I
 */import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
 
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 
public class Exemple extends JPanel  {
 JList liste = new JList();
 Exemple panel;
 
	private static final int MARGETOP = 50;
	private static final int WIDTH = 100;
	private static final int H = 60;
	private static final int HEIGHT = 3 * H;
	private static final int MARGIN = 5;
	private static final int ARROW = 5;
 
	private Rule[] rules = new Rule[5];
	private List<Arrow> arrows = new ArrayList<>();
	private List<Arrow_test> arrows_test = new ArrayList<>();
	private List<Map<String,Integer>> elements=new ArrayList<Map<String,Integer>>();
	private List <String> list_affich = new ArrayList();
 
	private int y = MARGETOP + H / 2;
	private int y_test = MARGETOP + H / 2;
	private Arrow currentArrow;
	private Arrow_test currentArrow_test;
	private String affichage = "";
 
	public Exemple() {
 //JButton jButton8 = new javax.swing.JButton();
  liste.setFont(new Font("Engravers MT", Font.PLAIN, 11));
     JScrollPane jScrollPane6 = new javax.swing.JScrollPane();
		String[] ruleNames = { "UE", "NodeB", "DRNC", "SRNC", "CN" };
		int x = 0;
		for (int i = 0; i < rules.length; i++, x += WIDTH) {
			rules[i] = new Rule(ruleNames[i], x, WIDTH);
		}
 
		setPreferredSize(new Dimension(860, 768));
 
                JButton jButton8 = new javax.swing.JButton();
		MouseAdapter mouseAdapter;
            mouseAdapter = new MouseAdapter() {
 
                @Override
                public void mousePressed(MouseEvent e) {
                    currentArrow = createArrow(e.getPoint());
                    repaint();
                }
 
                @Override
                public void mouseDragged(MouseEvent e) {
                    updateArrow(e.getPoint());
                    repaint();
                }
 
                @Override
                public void mouseReleased(MouseEvent e) {
 
                    updateArrow(e.getPoint());
                   int z=90;
                    saveArrow();
 
                        if(e.getX()>=45&&e.getX()<=455 )
 
                                                    try {
 
                                                    	dessiner(e.getY());
 
 
 
 
 
                    } catch (IOException ex) {
                        Logger.getLogger(Exemple.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (EncryptedDocumentException ex) {
                        Logger.getLogger(Exemple.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InvalidFormatException ex) {
                        Logger.getLogger(Exemple.class.getName()).log(Level.SEVERE, null, ex);
                    }
 
 
 
 
 
 
 
                }
 
            };
		addMouseListener(mouseAdapter);
		addMouseMotionListener(mouseAdapter);
 
 
	}
 
	public void dessiner(int y) throws IOException, FileNotFoundException, EncryptedDocumentException, InvalidFormatException{
 
    	int maxline =0;
 
 
	    JFrame parent = (JFrame) SwingUtilities.getWindowAncestor(Exemple.this); // permet de connaitre la fenêtre dans laquelle le composant se trouve (panel, c'est la référence du composant où se trouve le MouseListener original (celui qui produit mouseReleased())
	     jliste    b= new jliste(parent);
 
 
	        	  String selection = b.getSelection();
 
	        	  //System.out.println(selection);
	        	 // createArrow_test(selection);
	        	 // saveArrow_test();
	        	  //g.drawString(selection, 460, y) ;
	        	 // int taille=arrows_test.size();
	        	 //int y_vrai=y_test+taille*H;
	        	  Arrow_test element_dessiner = new Arrow_test(selection,y);
 
	        	  arrows_test.add(element_dessiner);
	        	  for(Arrow_test element: arrows_test){
	        		  Graphics g = getGraphics(); 
	        		  g.drawString(element.getSelection(), 460, saveArrow()) ;
 
	        	  }
 
 
 
 
	        	   }
	protected int saveArrow() {
		if (currentArrow != null && currentArrow.isValid()) {
			arrows.add(currentArrow);
			y += H;
 
		}
 
		currentArrow = null;
		return y;
	}
	protected void saveArrow_test() {
		if (currentArrow_test != null ) {
			arrows_test.add(currentArrow_test);
			y_test += H;
			for(Arrow_test arrow_test:arrows_test){
				Graphics g = getGraphics();
 
				//arrow_test.draw(g);
			}
 
 
		}
		currentArrow_test = null;
	}
	private Arrow createArrow(Point point) {
		for (Rule rule : rules) {
			if (rule.contains(point)) {
				return rule.createArrow(point, y);
			}
		}
		return null;
	}
	private Arrow_test createArrow_test(String selection) {
		for (Rule rule : rules) {
			Graphics g = getGraphics();
			Arrow_test arrow_test=	rule.createArrow_test(selection, y_test);
			arrow_test.draw(g);	
 
		}
		return null;
	}
 
 
	private void updateArrow(Point point) {
		if (currentArrow != null) {
			for (Rule rule : rules) {
				if (rule.contains(point)) {
					rule.updateArrow(currentArrow, point);
 
				}
			}
		}
	}
 
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		for (Rule rule : rules) {
			rule.draw(g);
		}
 
		g.setColor(Color.BLACK);
		for (Arrow arrow : arrows) {
			arrow.draw(g);
		}
 
		g.setColor(Color.BLACK);
		if (currentArrow != null) {
			currentArrow.draw(g);
		}
	}
 
	public static void main(String[] args) {
 
		JFrame frame = new JFrame();
 
		frame.add(new Exemple());
 
		frame.pack();
		frame.setLocationRelativeTo(null);
 
		frame.setVisible(true);
 
 
	}
	public static class Arrow_test {
 
		private String selection;
 
		private int y;
 
		public Arrow_test(String selection, int y) {
			this.selection = selection;
 
			this.y = y;
 
		}
 
		public void draw(Graphics g) {
 
 
                g.drawString(selection, 460, y) ;              
 
 
 
                }
		public String getSelection(){
			return this.selection;
		}
		public int getA(){
			return this.y;
		}
 
 
 
 
 
	}
 
	public static class Arrow {
 
		private int x;
		private int xend;
		private int y;
 
		public Arrow(int x, int y) {
			this.x = x;
			this.xend = x;
			this.y = y;
		}
 
		public void draw(Graphics g) {
			  if (isValid()) {
                Graphics2D g2d = (Graphics2D) g;
 
                                if(x<xend)
                                {g2d.drawLine(x, y, xend, y);
 
                g2d.drawLine(xend, y, xend - ARROW, y - ARROW);
                g2d.drawLine(xend, y, xend - ARROW, y + ARROW);}
                                else{
                                g2d.drawLine(x, y, xend, y);
 
                g2d.drawLine(xend, y, xend + ARROW, y + ARROW);
                g2d.drawLine(xend, y, xend + ARROW, y - ARROW);}
		}
                }
		public boolean isValid() {
			return xend != x;
		}
 
		public void setEnd(int end) {
                     if (x>=45 && x<=55 && end>300 && end <=350){xend=350;}
                    else if(x>=145 && x<=155 && end>=300&& end <=350) {xend=350;}
                    else if(x>=245 && x<= 255 && end>300&&end<=350){xend=350;}
                    else if(x>=345 && x<=355 && end>350 && end <=450){xend=450;}
 
                    else if (x>=345 && x<=355 && end<100 && end >=50){xend=50;}
                    else if(x>=345 && x<=355 && end<200&& end <=150) {xend=150;}
                    else if(x>=345 && x<= 355 && end<300 && end>=250){xend=250;}
                    else if(x>=445 && x<=455 && end<400 && end >=350){xend=350;}
 
		}
 
	}
 
	public static class Rule {
 
		public static final int N = 4;
 
		private final String name;
		private final int start;
		private final int width;
		private Rectangle2D textBounds;
 
		private Rectangle2D rectangle;
 
		public Rule(String name, int start, int width) {
			this.name = name;
			this.start = start;
			this.width = width;
			this.rectangle = new Rectangle2D.Double(start, MARGETOP, start + width, MARGETOP + N * HEIGHT);
		}
 
		public Arrow createArrow(Point point, int y) {
			if (point.getX()<55 && point.getX()>45){point=new Point(50, y);}
			if (point.getX()<155 && point.getX()>145){point=new Point(150, y);}
			if (point.getX()<255 && point.getX()>245){point=new Point(250, y);}
			if (point.getX()<355 && point.getX()>345){point=new Point(350, y);}
			if (point.getX()<455 && point.getX()>445){point=new Point(450, y);}
			//return new Arrow(start + width / 2, y);
                        return new Arrow((int) point.getX(), y);
 
		}
		public Arrow_test createArrow_test(String selection , int y) {
 
                        return new Arrow_test(selection, y);
 
		}
 
 
		public void updateArrow(Arrow currentArrow, Point point) {
			//currentArrow.setEnd(start + width / 2);
                        currentArrow.setEnd((int) point.getX());
                      //  String name = JOptionPane.showInputDialog("Label du Message");
		}
 
		public boolean contains(Point point) {
			return rectangle.contains(point);
		}
 
		public void draw(Graphics g) {
 
			g.setColor(Color.BLACK);
 
			int x = start + width / 2;
			int y = MARGETOP;
 
			if (textBounds == null) {
				Rectangle2D stringBounds = g.getFontMetrics().getStringBounds("MMMM", g);
				textBounds = new Rectangle2D.Double(x - stringBounds.getWidth() / 2 - MARGIN,
						y - stringBounds.getHeight() - MARGIN, MARGIN * 2 + stringBounds.getWidth(),
						MARGIN * 2 + stringBounds.getHeight());
			}
			Graphics2D g2d = ((Graphics2D) g);
			g2d.draw(textBounds);
			Point2D.Float p = getStringLocation(g2d, textBounds, name, SwingConstants.CENTER, SwingConstants.CENTER);
			g2d.drawString(name, (float) p.getX(), (float) p.getY());
 
			for (int i = 0; i < N; i++, y += HEIGHT) {
				g2d.drawLine(x, y + MARGIN, x, y + HEIGHT - MARGIN);
			}
 
		}
 
	}
 
 
	/**
         * méthode qui permet de placer un texte relativement Ã* un rectangle : elle
         * calcule la position Ã* utiliser avec la méthode Graphics.drawString()
         * 
         * @param g2d
         *            contexte graphique
         * @param bounds
         *            bounds du rectangle
         * @param string
         *            le texte
         * @param halign
         *            le type d'alignement horizontal parmi SwingContants.LEFT,
         *            SwingConstants.CENTER ou SwingConstants.RIGHT
         * @param valign
         *            le type d'alignement vertical parmi SwingContants.TOP,
         *            SwingConstants.CENTER ou SwingConstants.BOTTOM
         * @return
         */
	public static Point2D.Float getStringLocation(Graphics2D g2d, Rectangle2D bounds, String string, int halign,
			int valign) {
 
		final FontMetrics fontMetrics = g2d.getFontMetrics();
		final Rectangle2D textBounds = fontMetrics.getStringBounds(string, g2d);
		final double textWidth = textBounds.getWidth();
 
		double x, y;
 
		switch (halign) {
		case SwingConstants.LEFT:
			x = bounds.getX();
			break;
		case SwingConstants.RIGHT:
			x = bounds.getX() + bounds.getWidth() - textWidth;
			break;
		case SwingConstants.CENTER:
		default:
			x = bounds.getX() + (bounds.getWidth() - textWidth) / 2;
			break;
		}
		switch (valign) {
		case SwingConstants.TOP:
			y = bounds.getY() + fontMetrics.getAscent() - fontMetrics.getDescent();
			break;
		case SwingConstants.BOTTOM:
			y = bounds.getY() + bounds.getHeight() - fontMetrics.getDescent();
			break;
		case SwingConstants.CENTER:
		default:
			y = bounds.getY() + bounds.getHeight() / 2 - ((fontMetrics.getAscent() + fontMetrics.getDescent()) / 2)
					+ fontMetrics.getAscent();
			break;
		}
 
		return new Point2D.Float((float) x, (float) y);
	}
 
}