Hi all,
I know this king of issue is quite common but I'm brand new in java so I found clues about my problem but didn't manage to make it works. Basically, I'm trying to build a small image processing program in which you load an image and then do simple stuff like rotating, changing colors, etc....
So far (which is very close for the moment...) I have a main frame which has a menu item saying "load image". When I click on it opens a file chooser, i select a picture. I would like to create a panel and then display it on the main frame.
So to do that, I retrieve the path of the picture from the file chooser in the ActionPerformed method but I wondering what to do then.
The thing is that I have to draw this picture on a panel then add it to the frame but after this I'll have to reuse it all the time when I'll do some modifications in the image. So I guess I shouldn't create the panel in the ActionPerformed method, should I ?
If not how can I return the string path of the picture from the ActionPerformed ? I tried to do it with setter/getter methods but probably didn't do it properly. Could you give me a bit of help regarding the right way to implement this kind of program ?
thanks
Here is my code for the main window, I'm using netBeans 6.9.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Main.java * * Created on 17 déc. 2010, 11:02:40 */ package JImage; import javax.swing.JFileChooser; import javax.swing.JMenuItem; /** * * @author Bill'o */ public class Main extends javax.swing.JFrame { private String rep = null; /** Creates new form Main */ public Main() { initComponents(); } public void setRep(String s) { rep = s; } public String getRep() { return rep; } /** 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() { jFileChooser1 = new javax.swing.JFileChooser(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); chargerImage = new javax.swing.JMenuItem(); fermer = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); jFileChooser1.setDialogTitle("Charger une image"); jFileChooser1.setFileFilter(new MyCustomFilter()); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setName("JImage"); // NOI18N jMenu1.setText("Fichier"); chargerImage.setText("Charger une image"); chargerImage.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { chargerImageActionPerformed(evt); } }); jMenu1.add(chargerImage); fermer.setText("Fermer"); fermer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fermerActionPerformed(evt); } }); jMenu1.add(fermer); jMenuBar1.add(jMenu1); jMenu2.setText("Edition"); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 722, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 469, Short.MAX_VALUE) ); pack(); }// </editor-fold> public JMenuItem getChargerImage() { return chargerImage; } private void chargerImageActionPerformed(java.awt.event.ActionEvent evt) { int returnVal = jFileChooser1.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { String repert = jFileChooser1.getSelectedFile().getAbsolutePath(); setRep(repert); //JImagePanel panel3 = ImageUtil.makePanel(rep); } else { System.out.println("Annulation par l'utilisateur"); } String imageRep = getRep(); } private void fermerActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JMenuItem chargerImage; private javax.swing.JMenuItem fermer; private javax.swing.JFileChooser jFileChooser1; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; // End of variables declaration }