Page 1 of 3 123 LastLast
Results 1 to 20 of 45
  1. #1
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Cool Displaying an Image problem

    Hi, i'm trying to make a little applet where you can load a image (via File Browsing), edit it to your liking (with a preview) and save it again. But for some reason the preview is not working. Here's most of my code (file browsing and preview image):
    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:31 AM. Reason: change title/ updated code/ make thread better

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Can you define what you mean by "browsing a png file"?
    press a button, and the browser will open up, you click a .png, edit it,
    In what environment are you operating?
    Where is the button? How is the HTML fed to the browser display a png image. How is that image linked to an editor?

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

    Default

    -removed- _ _

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

    Default

    I use windows 7.
    By browsing for .png files i mean file browsing like this:
    And i think you know what .png files are :)

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Sorry I'm C# illiterate.
    Yes I'm familiar with png files.

    Not sure what "browsing" an image means? Is it showing the pixel values at an x,y location?
    And how a browser comes into it? Can you explain that?

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

    Default

    I meant to say file browsing >.< sorry

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    There is the JFileChooser class for selecting a file.

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

    Default

    Okay so i got his code
    [spoiler]
    Java Code:
    package DesaturateApplet;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    
    public class ImageBrowser extends JFrame {
    
        JTextField   _fileNameTF  = new JTextField(15);
        JTextField   _wordCountTF = new JTextField(4);
        JFileChooser _fileChooser = new JFileChooser();
        //ImageDisplay png = new ImageDisplay(_fileNameTF);
     
        ImageBrowser() {
       
            _fileNameTF.setEditable(false);
            _wordCountTF.setEditable(false);
    
    
    
      
            JPanel content = new JPanel();
            content.setLayout(new FlowLayout());
            content.add(new JLabel("File Name:"));
            content.add(_fileNameTF);
            content.add(new JLabel("Preview:"));
            content.add(ImageDisplay(_fileNameTF));
    
            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();                   
            this.setLocationRelativeTo(null); 
        }
    
        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());
                }
            }
        }
    
        //========================================================= main
        public static void main(String[] args) {
            JFrame window = new ImageBrowser();
            window.setVisible(true);
        }
    	
    }
    [/spoiler]
    And it works, but i cant get it to display the image. Here's the small part that's supposed to display an image:
    Java Code:
            JPanel content = new JPanel();
            content.setLayout(new FlowLayout());
            content.add(new JLabel("File Name:"));
            content.add(_fileNameTF);
            content.add(new JLabel("Preview:"));
            content.add(ImageDisplay(_fileNameTF));
    Thanks
    Last edited by petur170; 06-22-2011 at 11:19 PM.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    What is ImageDisplay? It appears to be a method.

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

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import javax.imageio.*;
    import java.io.*;
    
    public class ImageDisplay extends JPanel
    {
    
    	private String path;
     
    
    	private Image img;
     
    	public ImageDisplay(String path)	 throws IOException
    	{
    
    		this.path = path;	
     
    	
    		img = ImageIO.read(new File(path));
     
    	}
     
    
    	public void paint(Graphics g)
    	{
    
    		if( img != null)
    			g.drawImage(img,0,0, this);
    	}
     
    }
    :)

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    it works, but
    The code you posted does NOT compile.
    What do you mean by "it works"?

    There are lots of examples of how to display images on the forum. For example Search for ImageIO.read for some.

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

    Default

    Well, i need it to display the image i loaded from the file browser

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    The code as posted does not compile! It won't execute and display anything if it doesn't first compile.

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

    Default

    I'm using eclipse (you can test in eclipse)

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    That's fine. What problem are you having? Do you get errors or what happens with your code.
    The code you post will NOT compile for me!!!

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

    Default

    Here's the error i get:
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	The method ImageDisplay(JTextField) is undefined for the type ImageBrowser
    
    	at DesaturateApplet.ImageBrowser.<init>(ImageBrowser.java:33)
    	at DesaturateApplet.ImageBrowser.main(ImageBrowser.java:96)

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    The method ImageDisplay(JTextField) is undefined for the type ImageBrowser
    The syntax for that statement, as I mentioned in post #9, is how you call a method.
    What is the statement where the error occured supposed to do?

    How long have you been trying to write a Java program? Is this your first one?

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

    Default

    This is my second, My first was a simple bank account thing.

    The line with the error is line 6 on the bottom code in post #10.

    It is supposed to display an image right below the text "Preview:"

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Look at the syntax of the statement. It is a method call, but there is no method with that name.
    What did you expect that statement to do?

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

    Default

    oohh i see. The method is in a different class. Its supposed to draw an image from a directory (string). Is there a way i can call the method from the class (ImageDisplay)

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  2. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  3. Offline browsing issues
    By stthepan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-16-2010, 07:50 AM
  4. web browsing
    By mihir786 in forum Advanced Java
    Replies: 2
    Last Post: 11-10-2009, 03:43 AM
  5. Replies: 3
    Last Post: 01-23-2009, 12: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
  •