Search code examples
javaswingimageicon

How to ImageIcon Array List resize in following code


I have ImageIcon Array list and it has many sizes of icons, So I need to set them to a specific (85*100) size. So how I do this? my code as follow:

File file = new File("C://Users/ks/Desktop/DB");

            if (file != null) {
                File[] files = file.listFiles(new FilenameFilter() {

                    public boolean accept(File file, String fileName) {

                        if (fileName.endsWith(".png")) {
                            return true;
                        } else {
                            System.out.println("No jpg files");
                            return false;

                        }

                    }
                });

                System.out.println("Current dir : " + file.getCanonicalPath());

                List<ImageIcon> images = new ArrayList<ImageIcon>();// Array list

                for (int fileInList = 0; fileInList < files.length; fileInList++) {
                    System.out.print(files[fileInList].toString() + "  ");
                    System.out.println("this is forloop" + fileInList);
                    if (files[fileInList].isFile()) {

 images.add(0, new ImageIcon(ImageIO.read(files[fileInList])));// add all imagaes

                    }
              }                }

              FlowLayout f = new FlowLayout();

                          JPanel p = new JPanel(f);


            f.setAlignment(FlowLayout.LEFT);

                p.setSize(700, 100);

            for (int x = 0; x < images.size(); x++) {

                 p.add(new JLabel(images.get(x)));

                }

                jPanel1.setPreferredSize(p.getSize());
                jPanel1.add(p);
                jPanel1.revalidate();
                jPanel1.repaint();


            } else {
                System.out.println("No file Found"); // Array index out of bound display insted of this
            }

        } catch (IOException e) {

            e.printStackTrace();
        }
        System.out.println("Success");

Solution

  • To resize an Image you can use Image.getScaledInstance()

    List<ImageIcon> images = new ArrayList<ImageIcon>();// Array list
    
    for (int fileInList = 0; fileInList < files.length; fileInList++) {
     if (files[fileInList].isFile()) {
       Image baseImage = ImageIO.read(files[fileInList]);
       Image scaledImage = baseImage.getScaledInstance(85, 100,  java.awt.Image.SCALE_SMOOTH);   
       images.add(0, new ImageIcon(scaledImage));
     }
    }
    

    Or you could use the Graphics.drawImage().

    List<ImageIcon> images = new ArrayList<ImageIcon>();// Array list
    
    for (int fileInList = 0; fileInList < files.length; fileInList++) {
     if (files[fileInList].isFile()) {
       Image baseImage = ImageIO.read(files[fileInList]);
       BufferedImage scaledImage = new BufferedImage(85, 100, BufferedImage.TYPE_INT_RGB);
       Graphics2D g2d = (Graphics2D)scaledImage.createGraphics();
       g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,
                                RenderingHints.VALUE_RENDER_QUALITY));
       g2d.drawImage(baseImage, 0, 0, 85, 100, null);
       images.add(0, new ImageIcon(scaledImage));
     }
    }