如何將影像縮圖新增至網格中的佈局?
這個問題著重於 SpringLayout 網格,以在 JScrollPane 中保存一系列影像縮圖。具體來說,當照片清單的大小可能很大時如何處理佈局。
所以基本上,您需要某種位於滾動窗格中的容器(通常稱為視圖)。您應該為此添加圖像。
在以下 Java 程式碼範例中,照片位於 JPanel 上,並且位於 JScrollPane 中:
import java.awt.BorderLayout;
導入 java.awt.Component;
導入java.awt.Dimension;
導入 java.awt.EventQueue;
導入 java.awt.FlowLayout;
導入 java.awt.Graphics;
導入🎜>導入java.awt.Graphics2D;
導入java.awt.Image;
導入java.awt.Insets;
導入java.awt.event.ActionEvent;
導入java.awt.event.ActionListener;
導入java.awt.geom.AffineTransform;
導入java.awt.image.BufferedImage;
導入java. io.File;
導入java.io.FileFilter;
導入java.io.IOException;
導入javax.imageio.ImageIO;
導入javax.swing.JButton;
導入javax.swing.JFrame;
導入 javax.swing.JPanel;
導入 javax.swing.JScrollPane;
導入javax.swing.SwingUtilities;
導入javax.swing.UIManager;
public static void main(String[] args) { new ImageGrid(); } public ImageGrid() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private JPanel imagesPane; public TestPane() { setLayout(new BorderLayout()); imagesPane = new JPanel(new WrapLayout()); add(new JScrollPane(imagesPane)); JButton scan = new JButton("Scan"); scan.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String path = "C:\Users\shane\Dropbox\Ponies"; File[] files = new File(path).listFiles(new FileFilter() { @Override public boolean accept(File pathname) { String name = pathname.getName().toLowerCase(); return pathname.isFile() && (name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".gif")); } }); imagesPane.removeAll(); for (File file : files) { try { ImagePane pane = new ImagePane(file); imagesPane.add(pane); } catch (Exception exp) { exp.printStackTrace(); } } imagesPane.revalidate(); imagesPane.repaint(); } }); add(scan, BorderLayout.SOUTH); } } public class ImagePane extends JPanel { private Image img; public ImagePane(File source) throws IOException { img = ImageIO.read(source); if (img.getWidth(this) > 200 || img.getHeight(this) > 200) { int width = img.getWidth(this); int height = img.getWidth(this); float scaleWidth = 200f / width; float scaleHeight = 200f / height; if (scaleWidth > scaleHeight) { width = -1; height = (int)(height * scaleHeight); } else { width = (int)(width * scaleWidth); height = -1; } img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); } } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); if (img != null) {
/ / int height = img.m.
// 浮點數 = 1f;
// AffineTransform at = new AffineTransform();
// at.translate(
// (getWidth() / 2) - ((img.getWidth() * scale) / 2) ,
// (getHeight() / 2) - ((img.getHeight() *比例) / 2));
// at.scale(scale, scale);
// g2d.setTransform(at);
以上是如何在 JScrollPane 內的 Java 網格佈局中高效顯示大量圖像縮圖?的詳細內容。更多資訊請關注PHP中文網其他相關文章!