0

ImageIcon Array リストがあり、アイコンのサイズが多数あるため、特定の (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 に答える