/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * BackgroundDialog.java * * Created on Sep 6, 2013, 10:29:14 PM */ package com.arretados.leveleditor; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFileChooser; /** * * @author yurifariasg */ public class BackgroundDialog extends javax.swing.JDialog { private ImageListener listener; /** Creates new form BackgroundDialog */ public BackgroundDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); 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">//GEN-BEGIN:initComponents private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; jLabel1 = new javax.swing.JLabel(); pathTextField = new javax.swing.JTextField(); openButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setName("Form"); // NOI18N getContentPane().setLayout(new java.awt.GridBagLayout()); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.arretados.leveleditor.LevelEditorApp.class).getContext().getResourceMap(BackgroundDialog.class); jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N jLabel1.setName("jLabel1"); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 100.0; gridBagConstraints.weighty = 100.0; getContentPane().add(jLabel1, gridBagConstraints); pathTextField.setText(resourceMap.getString("pathTextField.text")); // NOI18N pathTextField.setName("pathTextField"); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 95.0; getContentPane().add(pathTextField, gridBagConstraints); openButton.setText(resourceMap.getString("openButton.text")); // NOI18N openButton.setName("openButton"); // NOI18N openButton.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { openButtonMouseClicked(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 5.0; getContentPane().add(openButton, gridBagConstraints); pack(); }// </editor-fold>//GEN-END:initComponents private final JFileChooser fc = new JFileChooser(); private void openButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openButtonMouseClicked //In response to a button click: int returnVal = fc.showOpenDialog(this); System.out.println("Return: " + returnVal); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); try { setImageWithPath(file.getAbsolutePath()); } catch (Exception e) { jLabel1.setText("Failed to load image"); } } }//GEN-LAST:event_openButtonMouseClicked private void setImageWithPath(String absolutePath) throws Exception { Image img = ImageIO.read(new File(absolutePath)); ImageIcon imgIcon = new ImageIcon(img); if (imgIcon.getIconHeight() == 0 || imgIcon.getIconWidth() == 0) throw new Exception(); if (listener != null) listener.onImageChanged(img); jLabel1.setIcon(imgIcon); jLabel1.setText(""); pathTextField.setText(absolutePath); } public void setImageListener(ImageListener listener) { this.listener = listener; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(BackgroundDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(BackgroundDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(BackgroundDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(BackgroundDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the dialog */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { BackgroundDialog dialog = new BackgroundDialog(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JButton openButton; private javax.swing.JTextField pathTextField; // End of variables declaration//GEN-END:variables }