Problema: Mostrar en un jdesktopPane una imagen seleccionada desde un jFilechooser.
Clase principal:
public class Principal extends javax.swing.JFrame {
/** Creates new form Principal */
public Principal() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jDesktopPane1 = new javax.swing.JDesktopPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItemAbrir = new javax.swing.JMenuItem();
jMenuItemSalir = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jMenu1.setText("Archivo");
jMenuItemAbrir.setText("Abrir");
jMenuItemAbrir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemAbrirActionPerformed(evt);
}
});
jMenu1.add(jMenuItemAbrir);
jMenuItemSalir.setText("Salir");
jMenuItemSalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemSalirActionPerformed(evt);
}
});
jMenu1.add(jMenuItemSalir);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jMenuItemSalirActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
this.setContentPane(jDesktopPane1);
}
private void jMenuItemAbrirActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
int x = fc.showOpenDialog(this);
File fileImagen = fc.getSelectedFile();
if(fileImagen!=null){
cargarImagen(jDesktopPane1, fileImagen);
}
}
/*este metodo recibe el jdeskopPane y el archivo imagen*/
public void cargarImagen(javax.swing.JDesktopPane jDeskp,File fileImagen)
{
try{
BufferedImage image = ImageIO.read(fileImagen);
jDeskp.setBorder( (Border) new PintaImagen(image));
}
catch (Exception e){
JOptionPane.showMessageDialog(this, "No se ha cargado la imagen");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItemAbrir;
private javax.swing.JMenuItem jMenuItemSalir;
// End of variables declaration
}
Clase PintaImagen:
class PintaImagen {
private final BufferedImage image;
public PintaImagen(BufferedImage image ) {
this.image=image;}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
int x0 = x+ (width-image.getWidth())/2;
int y0 = y+ (height-image.getHeight())/2;
g.drawImage(image,x0,y0,null); }
public Insets getBorderInsets(Component c) {
return new Insets(0,0,0,0);}
public boolean isBorderOpaque() {
return true; }
}
Lo que me ocurre es que al invocar el método cargarImagen() no puede hacer el try y se mete en el catch. Y no sé por qué...
*EDITO: ya he localizado la línea que falla:
jDeskp.setBorder( (Border) new PintaImagen(image));
Pero no se aún cómo solucionarlo... el ide me obliga a hacer un cast de image a Border, pero no se muy bien por qué