Results 1 to 3 of 3
  1. #1
    shaungoater is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default Loading An Image Help Please!

    I am fairly new to programming and am having trouble loading an image. i have looked at many online tutorials but they are all different. If someone could please tell me the actual code needed to read a file into my program and store it as a buffered image. Thankyou very much to anyone who can help.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Supported image formats: You can get these with static methods in the ImageIO class. Generally it is reading gif, jpg, png and writing jpg and png up through j2se 1.4. Add support for reading and writing bmp and wbmp in j2se 1.5 and for writing gif in j2se 1.6
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class LoadImages {
        private JScrollPane getContent(BufferedImage image) {
            JPanel panel = new JPanel(new GridLayout(1,0));
            panel.add(new JLabel(new ImageIcon(image)));
            panel.add(new JLabel(new ImageIcon(getImage())));
            return new JScrollPane(panel);
        }
    
        private BufferedImage getImage() {
            String path = "images/bclynx.jpg";
            // ClassLoader looks up resource which must
            // be located/available in the classpath.
            URL url = getClass().getResource(path);
            BufferedImage image = null;
            try {
                image = ImageIO.read(url);
            } catch(IOException e) {
                System.out.println("Read error: " + e.getMessage());
            }
            return image;
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/mtngoat.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new LoadImages().getContent(image));
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    softdev is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Dynamic image loading in jsp
    By ramakrishna k m in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 10-10-2011, 06:08 AM
  2. Loading Image from local directory
    By nancyhung in forum Advanced Java
    Replies: 0
    Last Post: 01-29-2008, 03:46 PM
  3. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 09:33 AM
  4. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  5. problems when loading an image in servlet
    By oregon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 06:02 PM

Posting Permissions

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