すべての画像を縮小して RAM の使用量を減らすことができます。たとえば、このコードはすべての画像を 200x200 に縮小します。そうすれば、100MB に 1000 個の画像を収めることができます。
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
public class Scroll extends JPanel {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JPanel panel = new Scroll();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int i = 0; i < 10; i++) {
JPanel buttonPanel = new JPanel();
JRadioButton b1 = new JRadioButton("button 1");
JRadioButton b2 = new JRadioButton("button 2");
JRadioButton b3 = new JRadioButton("button 3");
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
buttonPanel.add(b1);
buttonPanel.add(b2);
buttonPanel.add(b3);
BufferedImage buffer = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffer.createGraphics();
BufferedImage image = ImageIO.read(new File("image.jpg"));
g.scale(buffer.getWidth()*1.0/image.getWidth(),
buffer.getHeight()*1.0/image.getHeight());
g.drawImage(image, 0, 0, null);
g.dispose();
JLabel imageLabel = new JLabel(new ImageIcon(buffer));
JSplitPane splitPane = new JSplitPane();
splitPane.add(imageLabel, JSplitPane.LEFT);
splitPane.add(buttonPanel, JSplitPane.RIGHT);
panel.add(splitPane);
}
JScrollPane spane = new JScrollPane(panel);
frame.add(spane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,600);
frame.setVisible(true);
}
}
画像が表示されるときに動的にロードする場合は、ImageIcons の代わりに各画像に空の JPanel を使用し、それらの JPanel のペイント メソッドをオーバーライドする必要があります。Paint メソッドは、JPanel が表示されている場合にのみ呼び出されます。したがって、最も簡単な解決策は、ペイント メソッドで常にディスクからイメージをロードし、それを画面に描画することです。
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
public class Scroll extends JPanel {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JPanel panel = new Scroll();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int i = 0; i < 10; i++) {
JPanel buttonPanel = new JPanel();
JRadioButton b1 = new JRadioButton("button 1");
JRadioButton b2 = new JRadioButton("button 2");
JRadioButton b3 = new JRadioButton("button 3");
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
buttonPanel.add(b1);
buttonPanel.add(b2);
buttonPanel.add(b3);
JPanel imagePanel = new JPanel() {
{
BufferedImage image = ImageIO.read(new File("image.jpg"));
setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
image.flush();
}
@Override
public void paint(Graphics g) {
try {
BufferedImage image = ImageIO.read(new File("image.jpg"));
g.drawImage(image, 0, 0, null);
image.flush();
} catch(Exception e) {
}
}
};
JSplitPane splitPane = new JSplitPane();
splitPane.add(imagePanel, JSplitPane.LEFT);
splitPane.add(buttonPanel, JSplitPane.RIGHT);
panel.add(splitPane);
}
JScrollPane spane = new JScrollPane(panel);
frame.add(spane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,600);
frame.setVisible(true);
}
}