Results 1 to 3 of 3
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,064
    Rep Power
    8

    Default image download from server

    I am aware of the Sun article page "How to Use Icons", but I do not understand it fully. The advanced Sun texts are informative but not instructive to me.
    So here is what I did for the moment, in order to test what is really going on. I divided the Image loading part form the image displaying part, by triggering the download and the image paint under two different buttons (first button downloads the file and second button paints the image).
    I found out that it takes very long to get the image (of an average size of 40 kb) from the server (takes several seconds) and after downloading it, it seams still not ready to paint because I get erros according the image size… (can not get size, see try and catch).
    I am just trying to understand how things work. From the first button on the image should be stored under the var image or....
    So ones image points to the photo I should be able to get the size through image.getWidth(null); ......
    Why is that still not working?
    here is part of the code



    private class EventHandler implements ActionListener {

    public void actionPerformed(ActionEvent evt) {
    // images are download before display
    if (evt.getSource() == firstButton) {
    count++;
    if (count==maxcount)
    count=0;
    image = downloadImage(img[count].name); // the download
    }

    if (evt.getSource() == nextButton) // display image
    try {
    imgwidth = image.getWidth(null);
    }
    catch (Exception e) {
    System.out.println("can not get image width " + imgwidth);
    //e.printStackTrace();
    }
    try {
    imgheight = image.getHeight(null);
    }
    catch (Exception e) {
    System.out.println("can not get image height " + imgheight);
    //e.printStackTrace();
    }
    x = (int)(framew - imgwidth)/2; // centre the image on the window
    y = (int)(frameh - imgheight)/2;

    displayPanel.repaint();
    }

    }
    }



    private Image downloadImage(String filename) { // the image file reader
    Image images=null;
    URL url;
    url = getClass().getResource("Imagestore/" + filename);
    try {
    images = ImageIO.read(url);
    } catch (IOException ex) {
    System.out.println("can not read the file: " + filename);
    ex.printStackTrace();
    }

    return images;
    }





    private class ImageDisplay extends JPanel { // Defines the display panel.
    public void paintComponent(Graphics g) { // for painting the images
    this.setBackground(Color.WHITE);
    super.paintComponent(g);
    g.drawImage(image,x, y, imgwidth, imgheight,this);
    // the image infotext
    textLabel.setBounds(img[count].textx,img[count].texty, 300 , 50);
    // color, position of the tet
    settextColor(img[count].textcolor);
    textLabel.setText(img[count].info);
    }
    }

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

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class DownloadingImages {
        ImageDisplay displayPanel;
        String[] paths = {
            "http://antwrp.gsfc.nasa.gov/apod/image/0712/m74_hst_800c.jpg",
            "http://antwrp.gsfc.nasa.gov/apod/image/0711/pleiades_fs.jpg",
            "http://antwrp.gsfc.nasa.gov/apod/image/0711/crescent_bugnet.jpg",
            "http://antwrp.gsfc.nasa.gov/apod/image/0712/" +
                    "HindsVariable_goldman800.jpg"
        };
    
        private class EventHandler implements ActionListener {
            BufferedImage image;
            int count = 0;
    
            public void actionPerformed(ActionEvent evt) {
                String ac = evt.getActionCommand();
                // images are download before display
                if (ac.equals("load image")) {
                    count++;
                    if (count == paths.length)
                        count=0;
                    long start = System.currentTimeMillis();
                    image = downloadImage(paths[count]);
                    String name = getName(paths[count]);
                    long end = System.currentTimeMillis();
                    double time = (end - start)/1000.0;
                    System.out.printf("load time for %s was: %.1f seconds%n",
                                       name, time);
                }
    
                if (ac.equals("show image")) {
                    int imgwidth = 0, imgheight = 0;
                    try {
                        imgwidth = image.getWidth();
                    } catch (Exception e) {
                        System.out.println("can not get image width " + imgwidth);
                        System.out.println(e.getMessage());
                    }
                    try {
                        imgheight = image.getHeight();
                    } catch (Exception e) {
                        System.out.println("can not get image height " + imgheight);
                        System.out.println(e.getMessage());
                    }
    
                    displayPanel.setImage(image); 
                }
    
                if(ac.equals("load all"))
                    loadAllImages();
            }
    
            private String getName(String s) {
                int lastSlash = s.lastIndexOf("/");
                if(lastSlash > -1 && lastSlash < s.length()-1)
                    return s.substring(lastSlash+1);
                return null;
            }
        }
    
        private BufferedImage downloadImage(String path) { 
            BufferedImage image = null;
            try {
                URL url = new URL(path);
                image = ImageIO.read(url);
            } catch (IOException ex) {
                System.out.println("can not read : " + path);
                System.out.println(ex.getMessage());
            } 
            return image;
        }
    
        private class ImageDisplay extends JPanel {
            BufferedImage image;
    
            public ImageDisplay() {
                // this belongs here
                this.setBackground(Color.WHITE);
            }
    
            protected void paintComponent(Graphics g) {
                // This next line will fill component background
                // with background color specified in constructor
                // above.
                super.paintComponent(g);
                if(image != null) {
                    int x = (getWidth() - image.getWidth())/2;
                    int y = (getHeight() - image.getHeight())/2;
                    g.drawImage(image, x, y, this);  
                }
            }
    
            public void setImage(BufferedImage image) {
                this.image = image;
                Dimension d = new Dimension(image.getWidth(),
                                            image.getHeight());
                setPreferredSize(d);
                revalidate();
                repaint();
            }
        }
    
        private void loadAllImages() {
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    long start = System.currentTimeMillis();
                    BufferedImage[] images =
                        new BufferedImage[paths.length];
                    for(int i = 0; i < images.length; i++) {
                        try {
                            URL url = new URL(paths[i]);
                            images[i] = ImageIO.read(url);
                        } catch(IOException e) {
                            System.out.println("read error for: " +
                                                paths[i] + ": " +
                                                e.getMessage());
                        }
                    }
                    long end = System.currentTimeMillis();
                    double time = (end - start)/1000.0;
                    String s = "Images loaded in " + time + " seconds";
                    JOptionPane.showMessageDialog(null, s,
                                                  "images loaded", -1);
    /*
                    JPanel panel = new JPanel(new GridLayout(2,0));
                    JScrollPane scrollPane = new JScrollPane(panel);
                    scrollPane.setPreferredSize(new Dimension(500,500));
                    for(int i = 0; i < images.length; i++) {
                        panel.add(new JLabel(new ImageIcon(images[i])));
                    }
                    JOptionPane.showMessageDialog(null, scrollPane,
                                                  "images", -1);
    */
                }
            });
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        private JScrollPane getImagePanel() {
            displayPanel = new ImageDisplay();
            return new JScrollPane(displayPanel);
        }
    
        private JPanel getButtonPanel() {
            String[] ids = { "load image", "show image", "load all" };
            EventHandler handler = new EventHandler();
            JPanel panel = new JPanel();
            for(int j = 0; j < ids.length; j++) {
                JButton button = new JButton(ids[j]);
                button.setActionCommand(ids[j]);
                button.addActionListener(handler);
                panel.add(button);
            }
            return panel;
        }
    
        public static void main(String[] args) {
            DownloadingImages test = new DownloadingImages();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getImagePanel());
            f.add(test.getButtonPanel(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,064
    Rep Power
    8

    Default

    Thanks hardwired

    impressive (your code), I pulled it all ready through the compiler and it works! So now I need some time to study it all (I might ask some questions?) and to digest it very carefully.

    thanks again this might be the missing link to my applet
    willemjav

Similar Threads

  1. Server socket - send image to client
    By Hinty in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 07:39 AM
  2. Download JDK 1.5
    By Nick15 in forum New To Java
    Replies: 4
    Last Post: 01-01-2009, 03:10 AM
  3. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  4. I could download JDK 1.5
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-13-2007, 03:36 PM
  5. How to download an image from a HTTP URL
    By Valeriano in forum Networking
    Replies: 1
    Last Post: 05-20-2007, 08:35 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
  •