Results 1 to 8 of 8
  1. #1
    Birkoff is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default How to display image ?

    Hello everyone,

    I just registered on these forums because it seems a decent place with people that know about stuff.

    Before anyone asks: Yes, I did use the search engine and found some stuff (which I couldn't fully understand) related to my problem. Maybe it's because I am not sure what to search for.

    Either way ... I am having some difficulties with a college project. In this project we're supposed to make a interface and use some image processing methods over it.
    I am having some problems to build up an interface. I have NetBeans-5.5.1 installed and got the basics running but I don't seem able to load and show an image on the screen.
    I am not even sure what component I should be using: JPanel, JFrame, JLabel, JInternalFrame, ... ? None of the above =S ?
    And I am not sure on how to load the image as well.

    Well, any help will be welcome. From tutorials, pdfs, plain text files, pretty much anything (even pointers in to what I should search). If it's gonna help me than I want it =P

    Thanks in advance.

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

    Default

    Some resource links:
    How to Use Icons
    Trail: 2D Graphics
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class LoadAndShow extends JPanel {
        BufferedImage image;
        Dimension size = new Dimension();
    
        public LoadAndShow(BufferedImage image) {
            this.image = image;
            size.setSize(image.getWidth(), image.getHeight());
        }
    
        /**
         * Drawing an image can allow for more
         * flexibility in processing/editing.
         */
        protected void paintComponent(Graphics g) {
            // Center image in this component.
            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 {
            String path = "images/hawk.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            LoadAndShow test = new LoadAndShow(image);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new JScrollPane(test));
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            //showIcon(image);
        }
    
        /**
         * Easy way to show an image: load it into a JLabel
         * and add the label to a container in your gui.
         */
        private static void showIcon(BufferedImage image) {
            ImageIcon icon = new ImageIcon(image);
            JLabel label = new JLabel(icon, JLabel.CENTER);
            JOptionPane.showMessageDialog(null, label, "icon", -1);
        }
    }

  3. #3
    Birkoff is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the reply.
    It took me some time to read all the stuff you posted but I am finally getting somewhere.

    I am having a problem using JScrollPane and would like to know if anyone around can help me with it.

    Here's how I am drawing the image:
    Java Code:
    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {                                       
            String path = "/home/birkoff/wallpapers/rei_forever.jpg";
            Graphics g = jScrollPane1.getGraphics();
            Graphics2D g2D = (Graphics2D) g;
            
            try {
                BufferedImage image = ImageIO.read(new File(path));
                ImageIcon icon = new ImageIcon(image);            
                g2D.drawImage(image, (jScrollPane1.getWidth() - image.getWidth())/2, (jScrollPane1.getHeight() - image.getHeight())/2, this);
                
                jScrollPane1.paintComponents(g2D);                        
            }
            catch ( Exception e ) {            
            }
            finally {        
            }
    I don't know why but this only draw the border of the image (if I set the jScrollPane1's border to no-boarder then no image is draw at all).

    So can anyone please point me to my error ?

    Also, if anyone knows how to resize the image so it fits window (or if you know how to enable the scrolls on JScrollPane) please gimme a hand.

    There were some code for this on one of the links the previous user posted but I couldn't understand them.

    Thanks in advance.

  4. #4
    Birkoff is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    I just managed to put some scrollbars on the jScrollPane1 but I still can't get it to show the image.

    Here's the new code:
    Java Code:
     private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {                                       
            String path = "/home/birkoff/wallpapers/rei_forever.jpg";
            Graphics g = jScrollPane1.getGraphics();
            Graphics2D g2D = (Graphics2D) g;
            
            try {
                BufferedImage image = ImageIO.read(new File(path));
                ImageIcon icon = new ImageIcon(image);                        
                Scrollbar columnView = new Scrollbar(Scrollbar.HORIZONTAL);            
                Scrollbar rowView = new Scrollbar(Scrollbar.VERTICAL);            
                jScrollPane1.setColumnHeaderView(columnView);
                jScrollPane1.setRowHeaderView(rowView);
                jScrollPane1.setBackground(Color.BLACK);
                
                g2D.drawImage(image, (jScrollPane1.getWidth() - image.getWidth())/2, (jScrollPane1.getHeight() - image.getHeight())/2, this);            
                g2D.setPaint(Color.BLACK);
                jScrollPane1.setOpaque(true);
                jScrollPane1.paintComponents(g2D);                        
            }
            catch ( Exception e ) {            
            }
            finally {        
            }

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

    Default

    Java Code:
        private void jButton1MouseReleased(MouseEvent evt) {                           
            String path = "/home/birkoff/wallpapers/rei_forever.jpg";
            // Using [i]getGraphics[/i] is not the way to draw.
            // Your graphics will not persist with this method.
            // Use a proper JPanel and override the [i]paintComponent[/i]
            // method as shown above.
            Graphics g = jScrollPane1.getGraphics();
            Graphics2D g2D = (Graphics2D) g;
            // It is unwise to load images like this. You should load
            // them at startup and save a reference to them in a variable
            // as shown above.
            try {
                BufferedImage image = ImageIO.read(new File(path));
                ImageIcon icon = new ImageIcon(image);
                // Scrollbar is an AWT component. It is best to not mix
                // AWT heavyweight components with Swing lightweight
                // components untill you've had more experience.
                Scrollbar columnView = new Scrollbar(Scrollbar.HORIZONTAL);
                Scrollbar rowView = new Scrollbar(Scrollbar.VERTICAL);
                // ColumnHeader and RowHeader are special component areas
                // in a JScrollPane. They are not designed to accept
                // scrollbars. See JScrollPane api for details and the
                // tutorial page shown below.
                jScrollPane1.setColumnHeaderView(columnView);
                jScrollPane1.setRowHeaderView(rowView);
                // The viewPort is a better choice for background colors.
                jScrollPane1.setBackground(Color.BLACK);
                g2D.drawImage(image, (jScrollPane1.getWidth() - image.getWidth())/2,
                     (jScrollPane1.getHeight() - image.getHeight())/2, this);
                g2D.setPaint(Color.BLACK);
                // JScrollPane is opaque by default.
                jScrollPane1.setOpaque(true);
                // Instead of calling this method we override it in
                // a JComponent such as JPanel. The example below
                // shows how to set up a graphic component that does
                // all the work for you.
                jScrollPane1.paintComponents(g2D);
            }
            catch ( Exception e ) {            
            }
            finally {        
            }
        }
    It's okay to show an image in event code such as in the mouseReleased method but try to avoid loading an image in event code. You only need to load an image one time. Loading is best done at construction or via a JFileChooser.
    For more about JScrollPane see How to Use Scroll Panes.
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class AnotherGo {
        private JScrollPane getContent() {
            // This image/graphics component knows
            // how to draw itself and how to report
            // its display size needs/requirements to
            // its parent scrollPane/container.
            AnImagePanel panel = new AnImagePanel();
            JScrollPane scrollPane = new JScrollPane(panel);
            // If you want to configure/customize this
            // scrollPane this is the place to do so.
            return scrollPane;
        }
    
        public static void main(String[] args) {
            AnotherGo test = new AnotherGo();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getContent());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class AnImagePanel  extends JPanel {
        BufferedImage image;
        Dimension size = new Dimension();
    
        public AnImagePanel() {
            image = loadImage();
            size.setSize(image.getWidth(), image.getHeight());
        }
    
        protected void paintComponent(Graphics g) {
            // Center image in this component.
            int x = (getWidth() - size.width)/2;
            int y = (getHeight() - size.height)/2;
            g.drawImage(image, x, y, this);
        }
    
        /**
         * This method handles the communication of
         * size requirements with the parent JScrollPane.
         */
        public Dimension getPreferredSize() {
            return size;
        }
    
        private BufferedImage loadImage() {
            String path = "images/hawk.jpg";
            BufferedImage image = null;
            try {
                image = ImageIO.read(new File(path));
            } catch(IOException e) {
                // May as well use what is given...
                System.out.println("read error:" + e.getMessage());
            }
            return image;
        }
    }

  6. #6
    bridleman4 is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default Help!!! Please...

    Hi everyone,

    I'm new to these forums and this thread is exactly what I have been needing help on! I'm trying to display .png image files on a JPanel. My images are stored in the same directory as the source files. They are images of playing cards and are labeled "1.png", "2.png", etc. I have tried using the ImageIO process used above, the ClassLoader.getResource() method, and the ImageIcon classes, and none of them are working for me. Here is my code:

    public void paintImage(Graphics g, Point p, String filename) {
    BufferedImage i = null;
    try{i = ImageIO.read(new File(filename);}
    catch(IOException e) {e.printStackTrace();}
    //g.drawString(filename, p.x, p.y + 10);
    if(i!=null) {
    g.drawImage(i, p.x, p.y, _gameBoard);
    }
    else System.out.println("i not loaded properly.");
    }

    It's the image-printing method for my panel, where _gameBoard is the JPanel I am trying to display the image on. Why is the program not reading the file correctly? Thanks.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you know that your filename is in fact correct, that it has all the correct path information? Or if not files, have you tried loading the images as class resources?

  8. #8
    bridleman4 is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the quick response! I looked into getting class resources. This fixed my problem. The code i used is:

    i = getToolkit().getImage(getClass().getResource("1.pn g"));

    inside my JApplet. Thanks again!

Similar Threads

  1. can display image in JFrame?
    By xCLARAx in forum AWT / Swing
    Replies: 14
    Last Post: 04-03-2009, 07:02 PM
  2. how to display image in jsp page(Struts)
    By rama6262 in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 07:50 AM
  3. How to display an image in JApplet
    By fred in forum Java Applets
    Replies: 1
    Last Post: 07-24-2007, 02:02 AM
  4. How to display a database-image in a page?
    By simon in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 11:56 PM
  5. Simplest way to read and display a jpeg image
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 03:42 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
  •