Imagen en jPanel
Publicado por Alejandro (21 intervenciones) el 30/08/2018 22:06:35
Necesito colocar una imagen en un jPanel, pero que esta imagen se adapte al tamaño de diferentes monitores. Se agradece desde ya!!
Valora esta pregunta


0
package capaNegocis;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Imagen extends javax.swing.JPanel{
String ruta;
public Imagen(int x, int y, String ruta){
this.setSize(x, y);
this.ruta = ruta;
}
public void paint(Graphics g){
Dimension height = getSize();
//ImageIcon img = new ImageIcon(getClass().getResource(ruta));
Image imgExterna = new ImageIcon(ruta).getImage();
g.drawImage(imgExterna, 0, 0, height.width, height.height, null);
setOpaque(false);
super.paintComponent(g);
}
}
private void btnCargarImagen1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int seleccion = fc.showOpenDialog(this);
if (seleccion == JFileChooser.APPROVE_OPTION) {
File fichero = fc.getSelectedFile();
String ruta = fichero.getAbsolutePath();
int x = jpImagen.getWidth();
int y = jpImagen.getHeight();
Imagen img = new Imagen(x, y, ruta);
jpImagen.add(img);
jpImagen.repaint();
}
}