5 Replies Latest reply: Apr 21, 2008 10:38 AM by 807591 RSS

    getting an image's url

    807591
      i'm making an image editor software using java... and i've created thumbnails using JLabel..
      then small images after scaling are displayed in jlabel in a panel... now i wish to retrieve the url of the image i click... that is... if i click on a thumbnail of some image... i wish to display it in a bigger panel... for that i need to send the string of the image's loaction on the system to the module which displays the image on the bigger panel... how do i get this string.. wen i click on the thumbnail???
      the getResource() method gives me the string.. but also some other data which i dont need... is there some other way to retrieve the url of the image??
      plz help me find out the solution ASAP!!
        • 1. Re: getting an image's url
          800382
          How did you get the image for the thumbnail in the first place?

          Also, is it really a thumbnail, or are you just scaling the actual image, so that really all you need to do is pass the image itself?
          getResource() returns a URL. Don't know what you mean by "data which i don't need". The URL is the URL.
          • 2. Re: getting an image's url
            807591
            bsampieri wrote:
            How did you get the image for the thumbnail in the first place?

            Also, is it really a thumbnail, or are you just scaling the actual image, so that really all you need to do is pass the image itself?
            getResource() returns a URL. Don't know what you mean by "data which i don't need". The URL is the URL.
            Plus, images don't necessarily have a URL associated with them. What is the following image's URL?
            Image image= new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED);
            PS. What does ImageIcon's toString method return?
            • 3. Re: getting an image's url
              807591
              by URL i mean the location of the image on the system.... so that it can be passed as a string to the other module...
              and wen i use getResource() i get info such as the size... and stuff... it gives me the location too.. but i dunno how to only get the location and not the rest of the info....
              • 4. Re: getting an image's url
                807591
                for thumbnails am using the following code...
                it is scaling.....


                import java.io.File;
                import javax.swing.Icon;
                import java.awt.*;
                import java.awt.event.*;
                import javax.swing.*;

                import java.awt.Image;
                import java.awt.Graphics2D;
                import java.awt.RenderingHints;
                import java.awt.geom.AffineTransform;
                import java.awt.image.BufferedImage;
                import javax.swing.ImageIcon;
                import java.io.*;
                import java.awt.Toolkit;
                import java.awt.Color;
                import java.awt.GridLayout;
                import javax.swing.border.BevelBorder;
                public class ImageLoad extends JPanel
                {
                private BufferedImage inImage,outImage;
                Graphics2D g2d;
                AffineTransform tx;
                Double scale;
                // JPanel panel=new JPanel();
                public ImageLoad(){GridLayout g1=new GridLayout(0,3,7,7);
                     setLayout(g1);}
                void thumbnailImage(File f)
                {
                // System.out.print(f.getAbsolutePath());
                //File f = new File("C:\\Documents and Settings\\harsimrat\\My Documents\\My Pictures\\Cools");

                File[] c = f.listFiles();
                for (int len = c.length, i = 0; i < len; i++)
                {
                String fname=c.getName().toLowerCase();
                if(fname.endsWith(".gif") || fname.endsWith(".jpg")||fname.endsWith(".bmp")|| fname.endsWith(".jpeg"))
                {   
                scale=1.0;
                Image inImage = Toolkit.getDefaultToolkit().getImage(c[i].getAbsolutePath());
                MediaTracker tracker = new MediaTracker(this);
                tracker.addImage(inImage, 0);
                try {
                tracker.waitForID(0);
                } catch (InterruptedException e) {
                }
                double w = inImage.getWidth(null);
                double h = inImage.getHeight(null);

                if(w>h)
                {   if(w >100) scale=100/w;}//::scaleW=1.0;
                else
                {   if(h>100) scale=100/h;}//::scaleH=1.0;

                if(scale<1.0)
                {
                w = (int)w*scale;
                     h = (int)h*scale;
                               outImage = new BufferedImage((int)w, (int)h, BufferedImage.TYPE_INT_RGB);

                               g2d = outImage.createGraphics();
                               // g2d.setBackground(Color.WHITE);
                               g2d.clearRect(0, 0, outImage.getWidth(), outImage.getHeight());
                               g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                               tx = new AffineTransform();
                               tx.scale(scale, scale);
                               g2d.drawImage(inImage, tx, null);
                               g2d.dispose();
                               inImage = new ImageIcon(outImage).getImage();
                }
                String str=fname;
                if(str.length()>12)
                str=str.substring(0,12)+ "..";
                Icon ico =new ImageIcon(inImage);
                JLabel label=new JLabel(str,ico,JLabel.CENTER);
                System.out.println(str +" " scale " "+ico.getIconWidth()+ " " +ico.getIconHeight());
                label.setVerticalTextPosition( SwingConstants.BOTTOM);
                label.setHorizontalTextPosition(SwingConstants.CENTER);
                label.setSize(100,100);//ico.getIconWidth(),ico.getIconHeight());
                label.setBackground(Color.DARK_GRAY);
                label.setBorder(new BevelBorder(1));
                MouseHandler handler=new MouseHandler();
                label.addMouseListener(handler);
                add(label);

                }           
                }
                }
                private class MouseHandler extends MouseAdapter
                {
                public void mouseClicked(MouseEvent event)
                {
                System.out.print(event.getSource());
                //File nm=new File("C:\\1234.jpg");


                }
                }


                }
                • 5. Re: getting an image's url
                  800472
                  You see where you do this?
                  File[] c = f.listFiles();
                  and then this?
                  Toolkit.getDefaultToolkit().getImage(c.getAbsolutePath());
                  You start with an array of files on the filesystem, then you're getting the path of the ones you consider images. Save that path. Your BufferedImage object represents an image that may only be resident in memory and not have a corresponding file on the file system. You already have the data you want to save, you just need to not throw it out.
                  
                   [http://java.sun.com/javase/6/docs/api/java/io/File.html|http://java.sun.com/javase/6/docs/api/java/io/File.html] 
                  
                  And PLEASE format your code using code tags.
                  
                  Edited by: nclow on Apr 21, 2008 8:50 AM