Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 45
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.
    Java Code:
     content.add(ImageDisplay(_fileNameTF));
    If this is the statement with the error, it says to call the ImageDisplay method and pass it a JTextField reference.
    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.

  2. #22
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    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)

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Print out the name of the file being opened to see if that is ok.

  4. #24
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    its not working either. Hmm.

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Strange that a println wouldn't work.

  6. #26
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Ohh i thought you meant something else. println worked.

  7. #27
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    ------removed------
    Last edited by petur170; 06-23-2011 at 06:10 AM.

  8. #28
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    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 06:28 AM.

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I get the following exception when I execute your code:
    Exception in thread "main" java.lang.NullPointerException
    at java.io.File.<init>(File.java:222)
    at ImageBrowser2.main(ImageBrowser2.java:77)
    The value of path is not set when line 77 is executed.

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Cross posted at Displaying an Image Problem

  11. #31
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    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);
        }

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    What value is it set to?
    Add a println to show its value just before you try to use it.

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  14. #34
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    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 08:53 PM.

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    BufferedImage image = ImageIO.read(new File(path));
    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?
    What printed out?

  16. #36
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    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)

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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,....

  18. #38
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    its in the same folder, it does exist, and its not locked. Tried someother things, still hasnt worked.

  19. #39
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  20. #40
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    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)

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2011, 02:54 PM
  2. Replies: 8
    Last Post: 07-30-2010, 06:47 PM
  3. Offline browsing issues
    By stthepan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-16-2010, 08:50 AM
  4. web browsing
    By mihir786 in forum Advanced Java
    Replies: 2
    Last Post: 11-10-2009, 04:43 AM
  5. Replies: 3
    Last Post: 01-23-2009, 01:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •