bonjour

je suis sur un projet pour faire un jeu de plateau
et le souci c'est que j'arrive pas à mettre bien en oeuvre le drag and drop

mes images sont sur les JLabel et j'ai envi de transferer l'image sur d'autre JLabel
cela fait une semaine que je me casse la tete avec

help me please

voila le 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
 
import javax.swing.*;
 
import java.awt.*;
import java.awt.dnd.DropTarget;
import java.awt.event.*;
 
 
 
public class Plateau extends JFrame implements ActionListener , MouseMotionListener {
 
	public Plateau(){
		super();
 
		build();//On initialise notre fenêtre
	}
 
	private javax.swing.JLabel casec;
    private javax.swing.JLabel casee;
    private javax.swing.JLabel casen;
    private javax.swing.JLabel casene;
    private javax.swing.JLabel caseno;
    private javax.swing.JLabel caseo;
    private javax.swing.JLabel cases;
    private javax.swing.JLabel casese;
    private javax.swing.JLabel caseso;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane tabhisto;
    private javax.swing.JLabel jetonrestant1;
    private javax.swing.JLabel jetonrestant2;
    private javax.swing.JLabel njoueur1;
    public javax.swing.JLabel njoueur2;
    private javax.swing.JLabel pilej1;
    private javax.swing.JLabel pilej2;
 
	JLabel[] plateau={caseno,casen,casene,caseo,casec,casee,caseso,cases,casese};
 
 
 
	private void build(){
 
 
 
 
		setTitle("Ying & Yang"); //On donne un titre à l'application
		setSize(700,700); //On donne une taille à notre fenêtre
		setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
		setResizable(true); //On permet la redimensionnement de la fenêtre
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //On dit à l'application de se fermer lors du clic sur la croix
 
		this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
            	NotificationQuitter p = new NotificationQuitter(new javax.swing.JFrame(), true);
            	p.setVisible(true);
 
            }
        });
 
		JMenuBar menuBar = new JMenuBar();
		JMenu menu1 = new JMenu("Jeu");
		JMenu menu2 = new JMenu("Aide");
		JMenu menu3 = new JMenu("Affichage");
		JMenuItem configurer = new JMenuItem("Configurer Partie");
		JMenuItem charger = new JMenuItem("Charger");
		JMenuItem annuler = new JMenuItem("Annuler");
		JMenuItem apropos = new JMenuItem("A propos");
		JMenuItem meilleurcoup = new JMenuItem("Meilleur coup");
		JMenuItem changertheme = new JMenuItem("Changer le théme");
		JMenuItem changerplateau = new JMenuItem("Changer le plateau");
		JMenuItem quitter = new JMenuItem("quitter");
		JMenuItem sauvegarder = new JMenuItem("sauvegarder");
 
		menu1.add(configurer);
		menu1.add(charger);
		menu1.add(sauvegarder);
		menu1.add(annuler);
		menu1.add(quitter);
		menu2.add(apropos);
		menu2.add(meilleurcoup);
		menu3.add(changertheme);
		menu3.add(changerplateau);
 
		menuBar.add(menu1);
		menuBar.add(menu3);
		menuBar.add(menu2);
 
 
		setJMenuBar(menuBar);
		Configuration f = new Configuration(new javax.swing.JFrame(), true);
 
		tabhisto = new javax.swing.JTabbedPane();
 
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
 
        casen = new javax.swing.JLabel();
        caseno = new javax.swing.JLabel();
        caseo = new javax.swing.JLabel();
        casec = new javax.swing.JLabel();
        casene = new javax.swing.JLabel();
        casee = new javax.swing.JLabel();
        casese = new javax.swing.JLabel();
        cases = new javax.swing.JLabel();
        caseso = new javax.swing.JLabel();
 
        njoueur1 = new javax.swing.JLabel();
        njoueur2 = new javax.swing.JLabel();
 
 
 
        pilej2 = new javax.swing.JLabel();
        pilej1 = new javax.swing.JLabel();
 
        jetonrestant1 = new javax.swing.JLabel();
        jetonrestant2 = new javax.swing.JLabel();
 
 
 
 
 
 
         njoueur1.setText(f.getNomjoueur1());
 
         njoueur2.setText(f.getNomjoueur2());
 
        //jetonrestant1.setText(model.getNombrejeton1);
 
        //jetonrestant2.setText(model.getNombrejeton2);
 
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addGap(72, 72, 72)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(caseno, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(caseo, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(caseso, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(4, 4, 4)
                            .addComponent(casen, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(casec, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cases, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(casee, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(casese, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(casene, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jetonrestant1)
                        .addComponent(njoueur2)
                        .addComponent(pilej2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(njoueur1)
                        .addComponent(pilej1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jetonrestant2))
                    .addGap(67, 67, 67))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(27, 27, 27)
                            .addComponent(pilej1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(njoueur1)
                            .addGap(18, 18, 18)
                            .addComponent(jetonrestant1)
                            .addGap(46, 46, 46)
                            .addComponent(pilej2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(njoueur2)
                            .addGap(18, 18, 18)
                            .addComponent(jetonrestant2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(78, 78, 78)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(casene, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(casee, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(casese, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(casen, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(casec, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(cases, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(caseno, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(caseo, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(caseso, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addContainerGap(122, Short.MAX_VALUE))
            );
        tabhisto.addTab("plateau", jPanel1);
 
        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 559, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 476, Short.MAX_VALUE)
        );
 
        tabhisto.addTab("historique", jPanel2);
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabhisto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabhisto, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
        );
 
 
 
	        pack();
 
	        ImageIcon image = new ImageIcon(Toolkit.getDefaultToolkit().getImage("C:/Users/oussama/Desktop/ying-yang.jpg"));
 
	        caseno.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        caseno.setIcon(image);
 
	        casen.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        casen.setIcon(image);
 
	        casene.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        casene.setIcon(image);
 
	        casec.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        casec.setIcon(image);
 
	        casee.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        casee.setIcon(image);
 
	        caseo.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        caseo.setIcon(image);
 
	        caseso.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        caseso.setIcon(image);
 
	        casese.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        casese.setIcon(image);
 
	        cases.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        cases.setIcon(image);
 
	        ImageIcon image1 = new ImageIcon(Toolkit.getDefaultToolkit().getImage("C:/Users/oussama/Desktop/j1.png"));
	        ImageIcon image2 = new ImageIcon(Toolkit.getDefaultToolkit().getImage("C:/Users/oussama/Desktop/j2.jpg"));
 
	        pilej1.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        pilej1.setIcon(image1);
 
	        pilej2.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
	        pilej2.setIcon(image2);
	        String x = image1.toString();
 
 
	       try{  pilej1.setTransferHandler(new TransferHandler("text"));
 
	        pilej2.setTransferHandler(new TransferHandler(x));
	        MouseListener ml = new MouseAdapter(){
	          public void mousePressed(MouseEvent e){
	            JComponent jc = (JComponent)e.getSource();
	            TransferHandler th = jc.getTransferHandler();
	            th.exportAsDrag(jc, e, TransferHandler.COPY);
	          }
	        };
 
 
 
 
 
	        DropTarget dest0= new DropTarget();
	        DropTarget dest1= new DropTarget();
	        DropTarget dest2= new DropTarget();
	        DropTarget dest3= new DropTarget();
	        DropTarget dest4= new DropTarget();
	        DropTarget dest5= new DropTarget();
	        DropTarget dest6= new DropTarget();
	        DropTarget dest7= new DropTarget();
	        DropTarget dest8= new DropTarget();
 
	        caseno.setDropTarget(dest0);
	        casen.setDropTarget(dest1);
	        casene.setDropTarget(dest2);
	        caseo.setDropTarget(dest3);
	        casec.setDropTarget(dest4);
	        casee.setDropTarget(dest5);
	        caseso.setDropTarget(dest6);
	        cases.setDropTarget(dest7);
	        casese.setDropTarget(dest8);
 
 
	        pilej1.addMouseListener(ml);
	        pilej2.addMouseListener(ml);}
	       catch (Exception e){}
	    	//pilej1.setTransferHandler(new SimpleTransferHandler());
	    	//SimpleDragHandler handler = new SimpleDragHandler();
	    	//SimpleDragHandler handler = new SimpleDragHandler();
	        //pilej1.addMouseMotionListener(handler);
	       // pilej1.addMouseListener(handler);
	    //	pilej1.addMouseListener(new MouseAdapter() { 
	    	//	public void mousePressed(MouseEvent evt) { 
	    		//	JComponent comp = (JComponent)evt.getSource(); 
	    			//TransferHandler th = comp.getTransferHandler();
	    			//th.exportAsDrag(comp, evt, TransferHandler.COPY); 
	    //	} }); 
 
 
 
	/*    try{    
	        pilej1.addMouseListener(new java.awt.event.MouseAdapter() {
	            public void mousePressed(java.awt.event.MouseEvent evt) {
	                mousePressed(evt);
	            }
	        });
	        pilej1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
	            public void mouseDragged(java.awt.event.MouseEvent evt1) {
	                mouseDragged(evt1);
	            }
	        });
 
 
	        pilej2.addMouseListener(new java.awt.event.MouseAdapter() {
	            public void mouseReleased(java.awt.event.MouseEvent e) {
	                mouseReleased(e);
	            }
	        });
	    }catch (Exception a){}
 
/*
	    private void MousePressed(java.awt.event.MouseEvent evt) {
	        // TODO add your handling code here:
	    }
 
	    private void pilej11MouseDragged(java.awt.event.MouseEvent evt1) {
	        // TODO add your handling code here:
	    }
 
	    private void pilej2MouseReleased(java.awt.event.MouseEvent e) {
	        // TODO add your handling code here:
	    }
	     */   
 
		quitter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitterActionPerformed(evt);
            }
        });
		configurer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configurerActionPerformed(evt);
            }
        });
		charger.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chargerActionPerformed(evt);
            }
        });
 
		sauvegarder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sauvegarderActionPerformed(evt);
            }
        });
		annuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                annulerActionPerformed(evt);
            }
        });
		apropos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aproposActionPerformed(evt);
            }
        });
		meilleurcoup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                meilleurcoupActionPerformed(evt);
            }
        });
		changertheme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changerthemeActionPerformed(evt);
            }
        });
		changerplateau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changerplateauActionPerformed(evt);
            }
        });
 
 
 
 
 
 
 
 
	}
 
	public void configurerActionPerformed(ActionEvent e){
		// TODO Auto-generated method stub
 
		//Object source = e.getSource();
		Configuration f = new Configuration(new javax.swing.JFrame(), true);
 
		f.setVisible(true);
 
		f.pack();}
 
	public void quitterActionPerformed(ActionEvent e){
		NotificationQuitter n = new NotificationQuitter(new javax.swing.JFrame(), true);
		n.setVisible(true);
	}
 
 
 
	public void annulerActionPerformed(ActionEvent e){}
	public void meilleurcoupActionPerformed(ActionEvent e){}
	public void changerthemeActionPerformed(ActionEvent e){}
	public void changerplateauActionPerformed(ActionEvent e){}
	public void sauvegarderActionPerformed(ActionEvent e){}
	public void chargerActionPerformed(ActionEvent e){}
	public void aproposActionPerformed(ActionEvent e){}
 
 
 
	JLabel getJlabel(){
    	return pilej1;
    }
 
 
 
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
 
	}
	/*GhostGlassPane glassPane = new GhostGlassPane();
	setGlassPane(glassPane);
	JLabel label = new JLabel("Component adapted");
	label.addMouseListener(new GhostComponentAdapter(glassPane, "action_1"));
	label.addMouseMotionListener(new GhostMotionAdapter(glassPane));
	JButton button = new JButton("Picture adapted");
	label.addMouseListener(new GhostPictureAdapter(glassPane, "action_2", "image.png"));
	label.addMouseMotionListener(new GhostMotionAdapter(glassPane));
	/*ImageIcon image = new ImageIcon(Toolkit.getDefaultToolkit().getImage("c:/test.jpg"));
		label.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
		label.setIcon(image);
*/
 
	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
 
	}
}