Java - Eliminar JLabel

 
Vista:
Imágen de perfil de Diego
Val: 41
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Diego (16 intervenciones) el 06/05/2021 02:52:54
Saludos me gustaría saber si es posible borrar un JLabel de un panel después de ejecutar el programa. Me explico estoy intentado crear una aplicación que me permita cargar una imagen como tablero y que de igual forma pueda agregar unos JLabels que simulen fichas o tokens. Todo eso he logrado hacerlo pero también me gustaría que luego de crear la ficha pueda borrarla por ejemplo haciendo click derecho en un JLabel determinado y eliminarlo. Si esto es posible agradecería que me facilitaran información al respecto.
Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Kabuto (1381 intervenciones) el 06/05/2021 11:28:19
Se puede.
Todo depende un poco de como tengas estructurado el código, pero en principio añadiendo un MouseListener a las etiquetas, puedes detectar cuando se le hace click derecho y eliminarlas.

Te dejo un programita de ejemplo:

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
package eliminaJLabel;
 
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
 
public class EliminaLabel extends JFrame{
 
	private JLabel etiqueta1;
	private JLabel etiqueta2;
	private JPanel panel;
 
	public EliminaLabel() {
 
		etiqueta1 = new JLabel("Elimíname");
		etiqueta1.setToolTipText("Click derecho para eliminar");
		etiqueta1.addMouseListener(new RatonListener());
		etiqueta2 = new JLabel("Destruyeme");
		etiqueta2.setToolTipText("Click derecho para eliminar");
		etiqueta2.addMouseListener(new RatonListener());
 
		panel = new JPanel();
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 30));
		panel.add(etiqueta1);
		panel.add(etiqueta2);
 
		setContentPane(panel);
 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 200);
		setLocationRelativeTo(null);
		setVisible(true);
	}
 
	private class RatonListener implements MouseListener {
		@Override
		public void mouseClicked(MouseEvent e) {
			if (e.getButton() == 3) { //3 suele ser el boton derecho
				JLabel etiq = (JLabel)e.getComponent(); //Referenciamos la etiqueta clickada
				panel.remove(etiq); //Pedimos al panel que la elimine
				panel.repaint(); //Repintamos el panel, ahora ya sin la etiqueta clickada
			}
		}
 
		@Override
		public void mousePressed(MouseEvent e) { }
 
		@Override
		public void mouseReleased(MouseEvent e) { }
 
		@Override
		public void mouseEntered(MouseEvent e) {
			JLabel etiq = (JLabel)e.getComponent();
			etiq.setForeground(Color.RED);
		}
 
		@Override
		public void mouseExited(MouseEvent e) {
			JLabel etiq = (JLabel)e.getComponent();
			etiq.setForeground(Color.BLACK);
		}
	}
 
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
 
			@Override
			public void run() {
				new EliminaLabel();
			}
		});
	}
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de Diego
Val: 41
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Diego (16 intervenciones) el 06/05/2021 16:38:22
Ok. Cuando pueda reviso y luego comento cómo me fué. Gracias.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Diego
Val: 41
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Diego (16 intervenciones) el 11/05/2021 15:30:44
Saludos he estado "cacharreando" pero no he podido lograr lo que deseo podré aquí el código que tengo para ver si puedes guiarme. este
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
package presentacion;
 
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
 
 
public class Principal extends JFrame {
    private PanMapa panMapa;
    //private PanControles panControles;
 
    public Principal(){
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setTitle("Gestión de mapas");
        setLayout(null);
        setBackground(Color.yellow);
 
        {
            panMapa = new PanMapa();
            panMapa.setLocation(WIDTH, WIDTH);
            panMapa.setOpaque(true);
            this.add(panMapa);
        }
 /*       {
            panControles = new PanControles();
            panControles.setLocation(1025, 0);
            this.add(panControles);
        } */
        pack();
        setSize(1350, 700);
        setLocationRelativeTo(null);
    } // fin de constructor
    public static void main(String[] args)
    {
        Principal frame = new Principal();
        frame.setVisible(true);
    }
} // fin de la clase Principal
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Diego
Val: 41
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Diego (16 intervenciones) el 11/05/2021 15:33:21
Otra clase. Tener en cuenta que mi conocimiento en programación es limitado y por ende muchas cosas podrían no ser lógicas.
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
package presentacion;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import logica.GeneraFicha;
 
public class PanMapa extends JPanel {
 
    private JScrollPane scroll;
    public JLabel lblMapa;
    File fichero;
    JFileChooser jfchCargarMapa = new JFileChooser();
    JFileChooser jfchCargarFicha = new JFileChooser();
    private JButton btnAgregarMap;
    private JButton btnCrearFicha;
    public JComboBox combo;
    private JLabel lbl1;
    GeneraFicha lblFicha = new GeneraFicha() ;
 
    public PanMapa() {
 
        iniComponentes();
 
    } // fin del constructor
 
    private void iniComponentes() {
        setVisible(true);
        setLayout(null);
        setBackground(new Color(205, 190, 112));
        //Componentes
 
        {
            scroll = new JScrollPane();
            scroll.setBounds(0, 0, 1016, 672);
            this.add(scroll);
            {
                lblMapa = new JLabel();
                scroll.setViewportView(lblMapa);
            }
        }
        {
            btnAgregarMap = new JButton("Agregar mapa");
            btnAgregarMap.setBounds(1050, 30, 250, 40);
            btnAgregarMap.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    btnAgregarMapActionPerformed(evt);
                }
            });
            this.add(btnAgregarMap);
 
        }
        {
            btnCrearFicha = new JButton("Crear Ficha");
            btnCrearFicha.setBounds(1080, 210, 100, 40);
            btnCrearFicha.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    btnAgregarFichaActionPerformed(evt);
                }
            });
            this.add(btnCrearFicha);
 
        }
        {
            lbl1 = new JLabel("Tamaño de ficha");
            lbl1.setBounds(1050, 120, 200, 30);
            lbl1.setFont(new Font("Arial", Font.ITALIC, 20));
            this.add(lbl1);
        }
        {
            combo = new JComboBox();
            ComboBoxModel comboModel = new DefaultComboBoxModel(new String[]{
                "Seleccione", "Minúsculo", "Diminuto", "Menudo", "Pequeño", "Mediano",
                "Grande", "Enorme", "Gargantuesco", "Colosal"
            });
            combo.setBounds(1040, 160, 200, 30);
            combo.setModel(comboModel);
            combo.setFont(new Font("Arial", Font.ITALIC, 20));
            this.add(combo);
        }
 
        // fin de componentess
        setSize(1350, 700);
    } // fin del método iniComponentes
 
    private void btnAgregarMapActionPerformed(ActionEvent evt) {
 
        int seleccion;
        FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
        jfchCargarMapa.setFileFilter(filtro);
        seleccion = jfchCargarMapa.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == seleccion) {
 
            fichero = jfchCargarMapa.getSelectedFile();
            try {
                ImageIcon icon = new ImageIcon(fichero.toString());
                lblMapa.setIcon(icon);
 
            } catch (Exception ex) {
 
                JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
            }
        }
    } // fin del método btnAgregarMapActionPerformed
 
    private void btnAgregarFichaActionPerformed(ActionEvent evt) {
 
        if (combo.getSelectedItem().equals("Minúsculo")) {
            GeneraFicha lblFicha = new GeneraFicha();
            lblMapa.add(lblFicha);
            lblFicha.setOpaque(true);
            lblFicha.setBounds(30, 90, 10, 10);
 
            int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarMapa.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
 
                fichero = jfchCargarFicha.getSelectedFile();
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
                    Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    lblFicha.setIcon(iconoEscalado);
 
                } catch (Exception ex) {
 
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
                }
            }
 
/*            ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
            Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(),
                    lblFicha.getHeight(), Image.SCALE_SMOOTH);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            lblFicha.setIcon(iconoEscalado);
   */
        }
        if (combo.getSelectedItem().equals("Diminuto")) {
            GeneraFicha lblFicha = new GeneraFicha();
            lblMapa.add(lblFicha);
            lblFicha.setOpaque(true);
            lblFicha.setBounds(30, 90, 20, 20);
 
            int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarMapa.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
 
                fichero = jfchCargarFicha.getSelectedFile();
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
                    Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    lblFicha.setIcon(iconoEscalado);
 
                } catch (Exception ex) {
 
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
                }
            }
 
 /*           ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
            Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(),
                    lblFicha.getHeight(), Image.SCALE_SMOOTH);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            lblFicha.setIcon(iconoEscalado);
   */
        }
 
        if (combo.getSelectedItem().equals("Menudo")) {
            GeneraFicha lblFicha = new GeneraFicha();
            lblMapa.add(lblFicha);
            lblFicha.setOpaque(true);
            lblFicha.setBounds(30, 90, 35, 35);
            int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarMapa.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
 
                fichero = jfchCargarFicha.getSelectedFile();
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
                    Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    lblFicha.setIcon(iconoEscalado);
 
                } catch (Exception ex) {
 
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
                }
            }
 
 /*           ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
            Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(),
                    lblFicha.getHeight(), Image.SCALE_SMOOTH);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            lblFicha.setIcon(iconoEscalado);
*/
        }
        if (combo.getSelectedItem().equals("Pequeño")) {
           // GeneraFicha lblFicha = new GeneraFicha();
            lblMapa.add(lblFicha);
            lblFicha.setOpaque(true);
            lblFicha.setBounds(30, 90, 60, 60);
            generarIcono();
 
/*             int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarMapa.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
                fichero = jfchCargarFicha.getSelectedFile();
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
                    Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    lblFicha.setIcon(iconoEscalado);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
                }
            }
          */
  /*          ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
            Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(),
                    lblFicha.getHeight(), Image.SCALE_SMOOTH);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            lblFicha.setIcon(iconoEscalado);
*/
        }
        if (combo.getSelectedItem().equals("Mediano")) {
            GeneraFicha lblFicha = new GeneraFicha();
            lblMapa.add(lblFicha);
            lblFicha.setBounds(30, 90, 70, 70);
 
            int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarMapa.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
 
                fichero = jfchCargarFicha.getSelectedFile();
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
                    Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    lblFicha.setIcon(iconoEscalado);
 
                } catch (Exception ex) {
 
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
                }
            }
            /*         ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
            Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
            //lblFicha.setOpaque(true);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            lblFicha.setIcon(iconoEscalado);
             */
        }
        if (combo.getSelectedItem().equals("Grande")) {
            GeneraFicha lblFicha = new GeneraFicha();
 
            lblMapa.add(lblFicha);
            lblFicha.setBounds(30, 90, 140, 140);
 
            int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarMapa.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
 
                fichero = jfchCargarFicha.getSelectedFile();
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
                    Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    lblFicha.setIcon(iconoEscalado);
 
                } catch (Exception ex) {
 
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
                }
            }
 
/*            ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
            Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
            //lblFicha.setOpaque(true);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            lblFicha.setIcon(iconoEscalado);
*/
        }
        if (combo.getSelectedItem().equals("Enorme")) {
            GeneraFicha lblFicha = new GeneraFicha();
 
            lblMapa.add(lblFicha);
            lblFicha.setBounds(30, 90, 210, 210);
 
             int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarMapa.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
 
                fichero = jfchCargarFicha.getSelectedFile();
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
                    Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    lblFicha.setIcon(iconoEscalado);
 
                } catch (Exception ex) {
 
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
                }
            }
 
 /*           ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
            Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
            //lblFicha.setOpaque(true);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            lblFicha.setIcon(iconoEscalado);
*/
        }
        if (combo.getSelectedItem().equals("Gargantuesco")) {
            GeneraFicha lblFicha = new GeneraFicha();
 
            lblMapa.add(lblFicha);
            lblFicha.setBounds(30, 90, 280, 280);
 
 
/*            ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
            Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
            //lblFicha.setOpaque(true);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            lblFicha.setIcon(iconoEscalado);
*/
        }
        if (combo.getSelectedItem().equals("Colosal")) {
            GeneraFicha lblFicha = new GeneraFicha();
 
            lblMapa.add(lblFicha);
            lblFicha.setBounds(30, 90, 420, 420);
            int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarMapa.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
 
                fichero = jfchCargarFicha.getSelectedFile();
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
                    Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    lblFicha.setIcon(iconoEscalado);
 
                } catch (Exception ex) {
 
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
                }
            }
 
/*            ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
            Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
            //lblFicha.setOpaque(true);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            lblFicha.setIcon(iconoEscalado);
*/
        }
 
 
    } // fin del método btnAgregarFichaActionPerformed
    public void generarIcono(){
 
      //GeneraFicha lblFicha = new GeneraFicha();
  int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarFicha.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
 
                fichero = jfchCargarFicha.getSelectedFile();
 
 
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
 
                    Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    lblFicha.setIcon(iconoEscalado);
 
                } catch (Exception ex) {
 
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
                }
            }
    }
 
} // fin del clase PanMapa
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Diego
Val: 41
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Diego (16 intervenciones) el 11/05/2021 15:34:37
Otra clase.
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
package logica;
 
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Path2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.border.AbstractBorder;
 
/**
 *
 * @author Usuario
 */
public class BordeJLabel extends AbstractBorder {
    public BordeJLabel(){
 
    }
    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
 
        Graphics2D g2d = (Graphics2D) g;
 
        Color oldColor = c.getParent().getBackground();
        g2d.setColor(oldColor);
 
        Shape outer;
        Shape inner;
 
        int offs = 1;
        int size = offs + offs;
 
        float arc = .2f * offs;
        outer = new RoundRectangle2D.Float(x / 2, y / 2, width, height, offs * width , offs * height);
        inner = new RoundRectangle2D.Float(x + offs - 2, y + offs - 2, width - size + 4, height - size  + 4, arc, arc);
 
        Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
        g2d.addRenderingHints(antialiasing);
        path.append(outer, false);
        path.append(inner, false);
        g2d.fill(path);
    }
    private final RenderingHints antialiasing= new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Diego
Val: 41
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Diego (16 intervenciones) el 11/05/2021 15:36:37
Espero me puedas guiar.
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
package logica;
 
 
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import presentacion.PanMapa;
 
 
 
public class GeneraFicha extends JLabel implements MouseMotionListener{
    private final BordeJLabel borde = new BordeJLabel();
    JFileChooser jfchCargarFicha = new JFileChooser();
    File fichero;
 
    public GeneraFicha(){
 
        addMouseMotionListener(this);
        setBorder(borde);
 
 
    }
 
    public void mouseDragged(MouseEvent mme) {
    setLocation(
        this.getX() + mme.getX() - this.getWidth() / 2,
        this.getY() + mme.getY() - this.getHeight() / 2
    );
  }
  public void mouseMoved(MouseEvent mme) {}
 
 
  public void generarIcono(){
 
 
  int seleccion;
            FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
            jfchCargarFicha.setFileFilter(filtro);
            seleccion = jfchCargarFicha.showOpenDialog(this);
            if (JFileChooser.APPROVE_OPTION == seleccion) {
 
                fichero = jfchCargarFicha.getSelectedFile();
 
 
                try {
                    ImageIcon icon = new ImageIcon(fichero.toString());
 
                    Image imgEscalada = icon.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_SMOOTH);
                    Icon iconoEscalado = new ImageIcon(imgEscalada);
                    this.setIcon(iconoEscalado);
 
                } catch (Exception ex) {
 
                    JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
                }
            }
  }
 
 
} // fin de clase GeneraFicha
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Kabuto (1381 intervenciones) el 11/05/2021 21:11:20
Woww..
Muy interesante tu programa, aún no he entendido bien el 100% de toda su lógica..., pero al menos sí lo suficiente para conseguir lo que quieres.

Dentro de la clase PanMapa, escribimos la siguiente "subclase interna", que implementa un MouseListener.
Es prácticamente idéntica a la que puse de ejemplo anteriormente.
Fíjate en la línea marcada en negrita:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private class EliminarFicha implements MouseListener {
 
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getButton() == 3) {
            JLabel etiq = (JLabel)e.getComponent();
            lblMapa.remove(etiq);
            repaint();
        }
    }
 
    @Override
    public void mousePressed(MouseEvent e) { }
 
    @Override
    public void mouseReleased(MouseEvent e) { }
 
    @Override
    public void mouseEntered(MouseEvent e) { }
 
    @Override
    public void mouseExited(MouseEvent e) {	}
 
}

La idea es detectar que ficha/JLabel ha sido la clickada con botón derecho y eliminarla de lblMapa (que es otro JLabel)
Y repintamos el panel PanMapa para refleja este cambio.

Bien, este MouseListener se lo vamos a añadir a cada nueva ficha que se crea.

Esto lo hacemos dentro del método btnAgregarFichaActionPerformed()

Me he fijado que para cada if, pones la instrucción para crear un ficha
1
GeneraFicha lblFicha = new GeneraFicha();

Es decir, repites esa instrucción 9 veces. Y la instrucción que necesitamos para agregarle el MouseListener, también habría que ponerla 9 veces.
Así que para evitar esto lo he cambiado y solo pongo estas instrucciones una sola vez al principio del método.
Ya luego dentro de cada if, se agregan a lblMapa con las dimensiones correspondientes.
Las líneas que tú repetías la he dejado comentadas, pero vamos, se pueden borrar.

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
private void btnAgregarFichaActionPerformed(ActionEvent evt) {
 
    GeneraFicha lblFicha = new GeneraFicha();
    lblFicha.addMouseListener(new EliminarFicha());
 
    if (combo.getSelectedItem().equals("Minúsculo")) {
        //GeneraFicha lblFicha = new GeneraFicha();
        lblMapa.add(lblFicha);
        lblFicha.setOpaque(true);
        lblFicha.setBounds(30, 90, 10, 10);
 
        int seleccion;
        FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
        jfchCargarMapa.setFileFilter(filtro);
        seleccion = jfchCargarFicha.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == seleccion) {
 
            fichero = jfchCargarFicha.getSelectedFile();
            try {
                ImageIcon icon = new ImageIcon(fichero.toString());
                Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                Icon iconoEscalado = new ImageIcon(imgEscalada);
                lblFicha.setIcon(iconoEscalado);
 
            } catch (Exception ex) {
 
                JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
            }
        }
 
        /*            ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
        Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(),
                lblFicha.getHeight(), Image.SCALE_SMOOTH);
        Icon iconoEscalado = new ImageIcon(imgEscalada);
        lblFicha.setIcon(iconoEscalado);
         */
    }
    if (combo.getSelectedItem().equals("Diminuto")) {
        //GeneraFicha lblFicha = new GeneraFicha();
        lblMapa.add(lblFicha);
        lblFicha.setOpaque(true);
        lblFicha.setBounds(30, 90, 20, 20);
 
        int seleccion;
        FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
        jfchCargarMapa.setFileFilter(filtro);
        seleccion = jfchCargarFicha.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == seleccion) {
 
            fichero = jfchCargarFicha.getSelectedFile();
            try {
                ImageIcon icon = new ImageIcon(fichero.toString());
                Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                Icon iconoEscalado = new ImageIcon(imgEscalada);
                lblFicha.setIcon(iconoEscalado);
 
            } catch (Exception ex) {
 
                JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
            }
        }
 
        /*           ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
        Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(),
                lblFicha.getHeight(), Image.SCALE_SMOOTH);
        Icon iconoEscalado = new ImageIcon(imgEscalada);
        lblFicha.setIcon(iconoEscalado);
         */
    }
 
    if (combo.getSelectedItem().equals("Menudo")) {
        //GeneraFicha lblFicha = new GeneraFicha();
        lblMapa.add(lblFicha);
        lblFicha.setOpaque(true);
        lblFicha.setBounds(30, 90, 35, 35);
        int seleccion;
        FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
        jfchCargarMapa.setFileFilter(filtro);
        seleccion = jfchCargarFicha.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == seleccion) {
 
            fichero = jfchCargarFicha.getSelectedFile();
            try {
                ImageIcon icon = new ImageIcon(fichero.toString());
                Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                Icon iconoEscalado = new ImageIcon(imgEscalada);
                lblFicha.setIcon(iconoEscalado);
 
            } catch (Exception ex) {
 
                JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
            }
        }
 
        /*           ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
        Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(),
                lblFicha.getHeight(), Image.SCALE_SMOOTH);
        Icon iconoEscalado = new ImageIcon(imgEscalada);
        lblFicha.setIcon(iconoEscalado);
         */
    }
    if (combo.getSelectedItem().equals("Pequeño")) {
        // GeneraFicha lblFicha = new GeneraFicha();
        lblMapa.add(lblFicha);
        lblFicha.setOpaque(true);
        lblFicha.setBounds(30, 90, 60, 60);
        generarIcono();
 
        /*             int seleccion;
        FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
        jfchCargarMapa.setFileFilter(filtro);
        seleccion = jfchCargarFicha.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == seleccion) {
            fichero = jfchCargarFicha.getSelectedFile();
            try {
                ImageIcon icon = new ImageIcon(fichero.toString());
                Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                Icon iconoEscalado = new ImageIcon(imgEscalada);
                lblFicha.setIcon(iconoEscalado);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
            }
        }
         */
        /*          ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
        Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(),
                lblFicha.getHeight(), Image.SCALE_SMOOTH);
        Icon iconoEscalado = new ImageIcon(imgEscalada);
        lblFicha.setIcon(iconoEscalado);
         */
    }
    if (combo.getSelectedItem().equals("Mediano")) {
        //GeneraFicha lblFicha = new GeneraFicha();
        lblMapa.add(lblFicha);
        lblFicha.setBounds(30, 90, 70, 70);
 
        int seleccion;
        FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
        jfchCargarMapa.setFileFilter(filtro);
        seleccion = jfchCargarFicha.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == seleccion) {
 
            fichero = jfchCargarFicha.getSelectedFile();
            try {
                ImageIcon icon = new ImageIcon(fichero.toString());
                Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                Icon iconoEscalado = new ImageIcon(imgEscalada);
                lblFicha.setIcon(iconoEscalado);
 
            } catch (Exception ex) {
 
                JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
            }
        }
        /*         ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
        Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
        //lblFicha.setOpaque(true);
        Icon iconoEscalado = new ImageIcon(imgEscalada);
        lblFicha.setIcon(iconoEscalado);
         */
    }
    if (combo.getSelectedItem().equals("Grande")) {
        //GeneraFicha lblFicha = new GeneraFicha();
 
        lblMapa.add(lblFicha);
        lblFicha.setBounds(30, 90, 140, 140);
 
        int seleccion;
        FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
        jfchCargarMapa.setFileFilter(filtro);
        seleccion = jfchCargarFicha.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == seleccion) {
 
            fichero = jfchCargarFicha.getSelectedFile();
            try {
                ImageIcon icon = new ImageIcon(fichero.toString());
                Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                Icon iconoEscalado = new ImageIcon(imgEscalada);
                lblFicha.setIcon(iconoEscalado);
 
            } catch (Exception ex) {
 
                JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
            }
        }
 
        /*            ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
        Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
        //lblFicha.setOpaque(true);
        Icon iconoEscalado = new ImageIcon(imgEscalada);
        lblFicha.setIcon(iconoEscalado);
         */
    }
    if (combo.getSelectedItem().equals("Enorme")) {
        //GeneraFicha lblFicha = new GeneraFicha();
 
        lblMapa.add(lblFicha);
        lblFicha.setBounds(30, 90, 210, 210);
 
        int seleccion;
        FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
        jfchCargarMapa.setFileFilter(filtro);
        seleccion = jfchCargarFicha.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == seleccion) {
 
            fichero = jfchCargarFicha.getSelectedFile();
            try {
                ImageIcon icon = new ImageIcon(fichero.toString());
                Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                Icon iconoEscalado = new ImageIcon(imgEscalada);
                lblFicha.setIcon(iconoEscalado);
 
            } catch (Exception ex) {
 
                JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
            }
        }
 
        /*           ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
        Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
        //lblFicha.setOpaque(true);
        Icon iconoEscalado = new ImageIcon(imgEscalada);
        lblFicha.setIcon(iconoEscalado);
         */
    }
    if (combo.getSelectedItem().equals("Gargantuesco")) {
        //GeneraFicha lblFicha = new GeneraFicha();
 
        lblMapa.add(lblFicha);
        lblFicha.setBounds(30, 90, 280, 280);
 
 
        /*            ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
        Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
        //lblFicha.setOpaque(true);
        Icon iconoEscalado = new ImageIcon(imgEscalada);
        lblFicha.setIcon(iconoEscalado);
         */
    }
    if (combo.getSelectedItem().equals("Colosal")) {
        //GeneraFicha lblFicha = new GeneraFicha();
 
        lblMapa.add(lblFicha);
        lblFicha.setBounds(30, 90, 420, 420);
        int seleccion;
        FileNameExtensionFilter filtro = new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
        jfchCargarMapa.setFileFilter(filtro);
        seleccion = jfchCargarFicha.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == seleccion) {
 
            fichero = jfchCargarFicha.getSelectedFile();
            try {
                ImageIcon icon = new ImageIcon(fichero.toString());
                Image imgEscalada = icon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
                Icon iconoEscalado = new ImageIcon(imgEscalada);
                lblFicha.setIcon(iconoEscalado);
 
            } catch (Exception ex) {
 
                JOptionPane.showMessageDialog(null, "Error abriendo la imagen " + ex);
 
            }
        }
 
        /*            ImageIcon imgIcon = new ImageIcon(getClass().getResource("/res/prueba.jpg"));
        Image imgEscalada = imgIcon.getImage().getScaledInstance(lblFicha.getWidth(), lblFicha.getHeight(), Image.SCALE_SMOOTH);
        //lblFicha.setOpaque(true);
        Icon iconoEscalado = new ImageIcon(imgEscalada);
        lblFicha.setIcon(iconoEscalado);
         */
    }
 
 
}


Y ya está. Ahora cada ficha puede ser eliminada haciendo click derecho sobre ella.
Solo ha sido necesario escribir un MouseListener y agregarselo a cada ficha que se genere.

Muchas gracias por compartir tu código, insisto de nuevo en que es muy interesante.
Un saludo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de Diego
Val: 41
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Diego (16 intervenciones) el 12/05/2021 03:06:03
Muchas gracias. Ha funcionado bien, faltan más cosas que me gustaría mejorar por ejemplo que al mover las fichas estas no superen el tamaño del mapa (lblMapa) y que al mover el mapa y necesite agregar más fichas estas se pongan en un sitio visible y no en el que está predeterminado pues algunas veces es posible que la escena esté al final del mapa y se necesiten agregar nuevas fichas estás no queden en un lugar visible. No se si me explico. Pero de momento todo va como quiero. Gracias nuevamente.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Diego
Val: 41
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

Eliminar JLabel

Publicado por Diego (16 intervenciones) el 26/05/2021 01:50:07
Está fue la solución que le di a la cuestión de los limites de la ficha en el mapa,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void mouseDragged(MouseEvent mme) {
         x = this.getX()+ mme.getX() - this.getWidth() / 2;
         y = this.getY()+ mme.getY() - this.getHeight() / 2;
    setLocation(
        this.getX() + mme.getX() - this.getWidth() / 2,
        this.getY() + mme.getY() - this.getHeight() / 2
    );
    // sirve para limitar la ficha y que no se salga del mapa
    if(x<20)
        this.setLocation(20, this.y);
    if(x>4138)
         this.setLocation(4138, this.y);
     if(y<20)
        this.setLocation(this.x, 20);
    if(y>4200)
         this.setLocation(this.x, 4138);
    System.out.println(x+" "+ y);
    }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar