Results 1 to 7 of 7
- 12-24-2010, 10:30 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
Trouble drawing image from file picked by JFileChooser
I'm not to certain where I went wrong, maybe I oberlooked something so I'll post the whole code. Its is meant to display an image you select in an extension of Frame. Also the bold portion was generated entirely by netBeans GUI Builder. The FileFilterExtension aslo seems fine. The red text are the lines mentioned in the error
Java Code:import java.awt.*; import java.awt.event.*; [COLOR="Red"]public class FileTest extends javax.swing.JFrame {[/COLOR] javax.swing.filechooser.FileFilter ff = new FileFilterExtension(); Graphics g; public FileTest() { setTitle("Pick a file"); this.setResizable(false); initComponents(); jFileChooser1.setFileFilter(ff); } [B] @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jFileChooser1 = new javax.swing.JFileChooser(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jFileChooser1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { [COLOR="Red"]jFileChooser1ActionPerformed(evt);[/COLOR] } }); 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(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> [/B] private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) { String filename = jFileChooser1.getSelectedFile().getName(); final ImageFrame iFrame = new ImageFrame(filename); iFrame.setSize(1024,768); iFrame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { iFrame.setVisible(false);}}); iFrame.setVisible(true); [COLOR="Red"] iFrame.showImage(g);[/COLOR] } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new FileTest().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JFileChooser jFileChooser1; // End of variables declaration }
Java Code:class ImageFrame extends Frame{ Image image; public ImageFrame(String name){ setTitle(name); image = Toolkit.getDefaultToolkit().getImage(name); } public void showImage(Graphics g){ [COLOR="Red"]g.drawImage(image,0,0,this);[/COLOR] } }
The error seems to come from the drawImage call yet I've never had this problem before. Is it somehow possible the image is null?
Moderator Edit: Code Tags AddedLast edited by Fubarable; 12-25-2010 at 12:30 AM. Reason: Moderator Edit: Code Tags Added
- 12-24-2010, 10:34 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
I did stop the errors for some by changing the drawImage to
g.drawImage(image,0,0,1024,768,this);
but it still shows nothing and I checked and the images don't seem to be null...
-
You should read the tutorials on how to show images in Swing as your code is all wrong. For one, you don't use AWT components such as Frame, for another if all you want to do is show the image, then put it in an ImageIcon and display it in a JLabel. If you want to show a background image in a GUI, then do it in a JPanel and draw in the JPanel's paintComponent method. Again the Swing tutorials will show you how to do this correctly.
- 12-25-2010, 03:06 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
Thank you, I realized that I should override the paint method and not call the showImage method from main. There are no more errors but still no output. The only puzzling thing is that JCreatorLE displayed images with similar code to this for example
While NetBeans only brings up the frame. The image is in the same directory as the code yet NetBeans won't display it. :confused:Java Code:public class Testpaint { public static void main(String args[]) { GenericFrame gfx = new GenericFrame(); gfx.setSize(1024,768); gfx.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); gfx.show(); } } class GenericFrame extends Frame { Image image; public void paint(Graphics g){ image = Toolkit.getDefaultToolkit().getImage("Beach.jpg"); g.drawImage(image, 0,0,1024,768,this); } }
- 12-25-2010, 03:13 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
Also I'm only using Frame because thats what my CS Class uses and for grading purposes its better if I do the same.
- 12-25-2010, 06:52 PM #6
Then you're beyond help. Why ask in a forum if you're unwilling to follow the perfectly valid advice offered?
Also, your code formatting is atrocious. Recommended reading: Code Conventions for the Java(TM) Programming Language: Contents
And finally, dump the NetBeans visual designer aka Matisse. It's not a beginner's tool.
db
- 12-26-2010, 03:54 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
I've taken your advice, Fubarable, and decided to use a label. And DB, how is my formatting atrocious? Please tell me which parts specifically so I know where to look in the code conventions. Are these some of the parts you are talking about?
Java Code:setTitle("Pick a file"); this.setResizable(false); initComponents(); jFileChooser1.setFileFilter(ff);But anyway the problems solved. Thanks for your help Fubarable.Java Code:String filename = jFileChooser1.getSelectedFile().getName(); final ImageFrame iFrame = new ImageFrame(filename); iFrame.setSize(1024,768); iFrame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { iFrame.setVisible(false);}}); iFrame.setVisible(true); iFrame.showImage(g);
Similar Threads
-
trouble with JFileChooser
By grcunning in forum AWT / SwingReplies: 3Last Post: 09-04-2010, 08:34 PM -
how to display image selected by JFileChooser
By khushi.cutegal in forum AWT / SwingReplies: 17Last Post: 07-24-2010, 06:03 AM -
How to display image in a panel with JFileChooser
By srisar in forum AWT / SwingReplies: 14Last Post: 10-11-2009, 06:06 PM -
Displaying a selected image with a JFileChooser into a textarea
By Jonte79 in forum AWT / SwingReplies: 2Last Post: 04-24-2009, 08:10 AM -
Problems drawing a section of an image onto another image.
By Cain in forum Java 2DReplies: 1Last Post: 04-17-2009, 12:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks