Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Displaying an Image Problem

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Displaying an Image Problem

    Cross posted at: Browsing .png files?

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Displaying an Image Problem

    I have read your crossposted thread and found your updated code:

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

    Compiling it gives the following error:

    Exception in thread "main" java.lang.NullPointerException
    at java.io.File.<init>(Unknown Source)
    at ImageBrowser.main(ImageBrowser.java:71)


    Looks like its to do with:

    BufferedImage image = ImageIO.read(new File(path));

    Because this is in your main method, when compiled, the program is looking for an image that doesn't exist.

    static String path = fileimage;

    fileimage is not set.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Image Drawing Problem
    By Matta in forum AWT / Java Swing
    Replies: 8
    Last Post: June 11th, 2011, 06:06 AM
  2. Problem in reading image file
    By ramhanuman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 02:46 PM
  3. Problem on displaying Celcius
    By Superteo1987 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 31st, 2011, 10:23 AM
  4. Java Gif image problem in JFrame
    By Zachary Wins in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 23rd, 2010, 08:51 PM
  5. GUI problem with image in java
    By Koâk in forum AWT / Java Swing
    Replies: 6
    Last Post: May 17th, 2009, 04:17 AM

Tags for this Thread