試す:
public class ImageComponent extends JComponent {
private final BufferedImage img;
public ImageComponent(URL url) throws IOException {
img = ImageIO.read(url);
setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
}
public static void main(String[] args) throws Exception {
final URL kitten = new URL("https://placekitten.com/g/200/300");
final ImageComponent image = new ImageComponent(kitten);
JFrame frame = new JFrame("Test");
frame.add(new JScrollPane(image));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}