Results 21 to 40 of 45
Thread: Browsing .png files?
- 06-23-2011, 12:04 AM #21
What do you know about OOP programming. Your question implies that you need to do a bit of studying before trying to write a program.
If this is the statement with the error, it says to call the ImageDisplay method and pass it a JTextField reference.Java Code:content.add(ImageDisplay(_fileNameTF));
The method is to return a component that can be added to the content panel.
There is a class named ImageDisplay but there is NOT a method named ImageDisplay.
- 06-23-2011, 12:30 AM #22
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
OK so i fixed it (sorta) Now i can actually run it without an error, but when i load the image, it has this error:
Java Code:javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read(Unknown Source) at DesaturateApplet.ImageDisplay.ImageDisplay(ImageDisplay.java:23) at DesaturateApplet.ImageBrowser$OpenAction.actionPerformed(ImageBrowser.java:90) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.AbstractButton.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
- 06-23-2011, 12:32 AM #23
Print out the name of the file being opened to see if that is ok.
- 06-23-2011, 12:39 AM #24
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
its not working either. Hmm.
- 06-23-2011, 12:40 AM #25
Strange that a println wouldn't work.
- 06-23-2011, 12:48 AM #26
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Ohh i thought you meant something else. println worked.
- 06-23-2011, 04:10 AM #27
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
------removed------
Last edited by petur170; 06-23-2011 at 05:10 AM.
- 06-23-2011, 05:10 AM #28
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Ok, so i combined it all too one class and fixed all the errors, now i can load a image without any errors, but it doesnt display anything. My full code:
Java Code:package DesaturateApplet; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class ImageBrowser extends JFrame{ //====================================================== fields JTextField _fileNameTF = new JTextField(15); JTextField _wordCountTF = new JTextField(4); JFileChooser _fileChooser = new JFileChooser(); //================================================= constructor ImageBrowser() { String newline = System.getProperty("line.separator"); _fileNameTF.setEditable(false); _wordCountTF.setEditable(false); JPanel content = new JPanel(); content.setLayout(new FlowLayout()); content.add(new JLabel("File Name:" + newline)); content.add(_fileNameTF); content.add(new JLabel(newline + "Preview:")); JMenuBar menubar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenuItem openItem = new JMenuItem("Open..."); openItem.addActionListener(new OpenAction()); menubar.add(fileMenu); fileMenu.add(openItem); this.setJMenuBar(menubar); this.setContentPane(content); this.setTitle("Select a Image File"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); // Layout components. this.setLocationRelativeTo(null); // Center window. } public static String fileimage; ///////////////////////////////////////////////////// OpenAction class OpenAction implements ActionListener { public void actionPerformed(ActionEvent ae) { int retval = _fileChooser.showOpenDialog(ImageBrowser.this); if (retval == JFileChooser.APPROVE_OPTION) { File file = _fileChooser.getSelectedFile(); _fileNameTF.setText(file.getName()); File filetemp = _fileChooser.getSelectedFile(); String fileimage = filetemp.getName(); } } } //========================================================= main static String path = fileimage; public static void main(String[] args) throws IOException{ JFrame window = new ImageBrowser(); window.setVisible(true); BufferedImage image = ImageIO.read(new File(path)); ImageBrowser test = new ImageBrowser(image); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.add(new JScrollPane(test)); window.setSize(400,400); window.setLocation(200,200); window.setVisible(true); } private static void showIcon(BufferedImage image) { ImageIcon icon = new ImageIcon(image); JLabel label = new JLabel(icon, JLabel.CENTER); JOptionPane.showMessageDialog(null, label, "icon", -1); } BufferedImage image; Dimension size = new Dimension(); public ImageBrowser(BufferedImage image) { this.image = image; size.setSize(image.getWidth(), image.getHeight()); } static String imgfile = fileimage; protected void paintComponent(Graphics g) { int x = (getWidth() - size.width)/2; int y = (getHeight() - size.height)/2; g.drawImage(image, x, y, this); } public Dimension getPreferredSize() { return size; } }Last edited by petur170; 06-23-2011 at 05:28 AM.
- 06-23-2011, 01:14 PM #29
I get the following exception when I execute your code:
The value of path is not set when line 77 is executed.Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(File.java:222)
at ImageBrowser2.main(ImageBrowser2.java:77)
- 06-23-2011, 01:33 PM #30
Cross posted at Displaying an Image Problem
- 06-23-2011, 06:56 PM #31
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
It is set...
Java Code:static String path = fileimage; BufferedImage image; Dimension size = new Dimension(); public ImageBrowser(BufferedImage image) { this.image = image; size.setSize(image.getWidth(), image.getHeight()); } static String imgfile = fileimage; protected void paintComponent(Graphics g) { int x = (getWidth() - size.width)/2; int y = (getHeight() - size.height)/2; g.drawImage(image, x, y, this); } public Dimension getPreferredSize() { return size; } public static void main(String[] args) throws IOException{ JFrame window = new ImageBrowser(); window.setVisible(true); BufferedImage image = ImageIO.read(new File(path)); ImageBrowser test = new ImageBrowser(image); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.add(new JScrollPane(test)); window.setSize(400,400); window.setLocation(200,200); window.setVisible(true); } private static void showIcon(BufferedImage image) { ImageIcon icon = new ImageIcon(image); JLabel label = new JLabel(icon, JLabel.CENTER); JOptionPane.showMessageDialog(null, label, "icon", -1); }
- 06-23-2011, 07:00 PM #32
What value is it set to?
Add a println to show its value just before you try to use it.
- 06-23-2011, 07:01 PM #33
What value is it set to?
Add a println to show its value just before you try to use it.
Do you having any programming experience in any language.
There are things about how you have put this program together that makes me wonder if you know how computer programs execute.
- 06-23-2011, 07:51 PM #34
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Yes im a noob. (im 10 so i dont know much about computers :( )
It says the name of the file and the file extension, but there is no file directory (that matter's right?)Last edited by petur170; 06-23-2011 at 07:53 PM.
- 06-23-2011, 07:54 PM #35
Are you sure the variable path has a value when the above line executes? Did you print its value out just before the above line was executed?BufferedImage image = ImageIO.read(new File(path));
What printed out?
- 06-23-2011, 08:03 PM #36
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
oooh it says null until i load a file. I made a default file for it to use before i load an image. But it doesnt display...
This is the new error (with the default file name printed)Java Code:default.png Exception in thread "main" javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read(Unknown Source) at DesaturateApplet.ImageBrowser.main(ImageBrowser.java:97)
- 06-23-2011, 08:06 PM #37
The problem is that The program can not read the file.
Lots of reasons for that. File doesn't exist, it's in another folder, the OS has a lock on it,....
- 06-23-2011, 08:18 PM #38
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
its in the same folder, it does exist, and its not locked. Tried someother things, still hasnt worked.
- 06-23-2011, 08:31 PM #39
Sorry, I can't see how your files are arranged or how you are executing the code.
Try this:
Remove the package statement from the source and recompile the program
Open a command prompt, change directory to the folder with the image and the java .class file
Enter the command: dir
to show all the files.
Execute the java program: java ImageBrowser
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
- 06-23-2011, 08:58 PM #40
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Java Code:Exception in thread "main" java.lang.NoClassDefFoundError: ImageBrowser (wrong n ame: DesaturateApplet/ImageBrowser) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Similar Threads
-
Java webstart having problem with multiple jar files and resource files
By rjalori in forum AWT / SwingReplies: 2Last Post: 06-11-2011, 01:54 PM -
Convert avi, mpeg, wmv media files to .flv files in java code
By vinay1497 in forum New To JavaReplies: 8Last Post: 07-30-2010, 05:47 PM -
Offline browsing issues
By stthepan in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-16-2010, 07:50 AM -
web browsing
By mihir786 in forum Advanced JavaReplies: 2Last Post: 11-10-2009, 03:43 AM -
BROWSING and ATTACHING MORE THAN ONE FILES IN JAVA
By nehaa in forum New To JavaReplies: 3Last Post: 01-23-2009, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks