0

我有 ImageIcon 数组列表,它有很多大小的图标,所以我需要将它们设置为特定的 (85*100) 大小。那么我该怎么做呢?我的代码如下:

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");
4

1 回答 1

1

要调整图像大小,您可以使用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));
 }
}

或者您可以使用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));
 }
}
于 2013-08-26T06:22:03.280 回答