Add image thumbnails to a layout in a grid?

前端 未结 2 937
傲寒
傲寒 2020-11-22 12:25

I have a list of images. I need to add as small thumbnails to a frame. I currently have frame with SpringLayout. How can add thumbnails in some grid like fashio

2条回答
  •  一个人的身影
    2020-11-22 12:59

    This recently deleted question on the same topic prompted this example that uses randomly-sized, synthetic images to illustrate the effect of scaling to a particular SIZE in the largest dimension.

    image

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import javax.swing.*;
    
    /** @see https://stackoverflow.com/a/15982915/230513 */
    public class Thumbnails {
    
        private static final int SIZE = 128;
        private static final Random r = new Random();
    
        // Get a randomly sized image
        static private Image getImage() {
            int w = r.nextInt(SIZE) + SIZE / 2;
            int h = r.nextInt(SIZE) + SIZE / 2;
            BufferedImage bi = new BufferedImage(
                w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = bi.createGraphics();
            g2d.setPaint(Color.lightGray);
            g2d.fillRect(0, 0, w, h);
            g2d.setColor(Color.black);
            String s = w + "\u00D7" + h;
            int x = (w - g2d.getFontMetrics().stringWidth(s)) / 2;
            g2d.drawString(s, x, 24);
            g2d.dispose();
            return bi;
        }
    
        // Get a panel with an image scaled to SIZE in the largest dimension
        // https://stackoverflow.com/a/15961424/230513
        private static JPanel getPanel() {
            Image original = getImage();
            int w = original.getWidth(null);
            int h = original.getHeight(null);
            float scaleW = (float) SIZE / w;
            float scaleH = (float) SIZE / h;
            if (scaleW > scaleH) {
                w = -1;
                h = (int) (h * scaleH);
            } else {
                w = (int) (w * scaleW);
                h = -1;
            }
            Image scaled = original.getScaledInstance(w, h, Image.SCALE_SMOOTH);
            JPanel p = new JPanel(new GridLayout()){
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(SIZE, SIZE);
                }
            };
            p.add(new JLabel(new ImageIcon(scaled)));
            p.setBorder(BorderFactory.createLineBorder(Color.red));
            return p;
        }
    
        private static void createAndShowGUI() {
            JFrame f = new JFrame("PhotoAlbum55");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel(new GridLayout(6, 6));
            for (int i = 0; i < 6 * 6; i++) {
                panel.add(getPanel());
            }
            f.add(new JScrollPane(panel));
            f.pack();
            f.setSize(4 * SIZE, 4 * SIZE);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    

提交回复
热议问题