SwingとImageIOを使ってファイル選択ダイアログで指定した画像を読み込み、その画像を画面内に4つ表示するプログラムを作ってみました。印刷機能も付けてみました。コメントが一切ありませんが…!
そういえばImageIO#readって対応してないファイルに対して例外がでるんじゃなくてnullが返ってくるのね…。そこらへんも考えてプログラムを作っていかねば
import javax.swing.JFrame; import javax.swing.JComponent; import javax.swing.JOptionPane; import javax.swing.JFileChooser; import javax.swing.SwingUtilities; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JMenuBar; import java.awt.Color; import java.awt.Graphics; import java.awt.Dimension; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterJob; import java.awt.print.PrinterException; import java.util.Optional; import java.io.File; import java.io.IOException; public class Test extends JComponent{ private Optional<BufferedImage> editImage = Optional.empty(); public static void main(String... args){ SwingUtilities.invokeLater(Test::start); } private static void start(){ var test = new Test(); var frame = new JFrame(); test.setPreferredSize(new Dimension(600, 500)); frame.setJMenuBar(Test.createMenuBar(test)); frame.add(test); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.pack(); } @Override public void paintComponent(Graphics g){ super.paintComponent(g); drawImage4(g, 0, 0, getWidth(), getHeight()); } private void draw(Graphics g, int x, int y, int width, int height){ g.setColor(Color.WHITE); g.fillRect(x, y, width, height); editImage.ifPresent(image -> { double ratio = Test.getRatio(width, height, image); int newWidth = (int)(image.getWidth() * ratio); int newHeight = (int)(image.getHeight() * ratio); g.drawImage(image, x, y, newWidth, newHeight, this); }); } private void drawImage4(Graphics g, int x, int y, int width, int height){ int halfWidth = width / 2; int halfHeight = height / 2; draw(g, x, y, halfWidth, halfHeight); draw(g, x + halfWidth, y, halfWidth, halfHeight); draw(g, x, y + halfHeight, halfWidth, halfHeight); draw(g, x + halfWidth, y + halfHeight, halfWidth, halfHeight); } private static JMenuBar createMenuBar(Test test){ var menubar = new JMenuBar(); var fileMenu = new JMenu("File"); var openItem = new JMenuItem("Open"); openItem.setMnemonic('O'); openItem.addActionListener(e -> { Test.openLoadDialog(test); }); var printItem = new JMenuItem("Print"); printItem.setMnemonic('P'); printItem.addActionListener(e -> { Test.printImage(test); }); fileMenu.setMnemonic('F'); fileMenu.add(openItem); fileMenu.add(printItem); menubar.add(fileMenu); return menubar; } private static void openLoadDialog(Test test){ var chooser = new JFileChooser(); chooser.setCurrentDirectory(new File("./")); chooser.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "gif", "png")); if(chooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION){ var file = chooser.getSelectedFile(); test.load(file); test.repaint(); } } private static void printImage(Test test){ var job = PrinterJob.getPrinterJob(); job.setPrintable((g, format, index) -> { if(index > 0){ return Printable.NO_SUCH_PAGE; } test.drawImage4(g, 0, 0, (int)format.getImageableWidth(), (int)format.getImageableHeight()); return Printable.PAGE_EXISTS; }); if(job.printDialog()){ try{ job.print(); }catch(PrinterException e){ JOptionPane.showMessageDialog(test, "印刷に失敗しました: " + e.getMessage()); } } } private void load(File file){ try{ editImage = Optional.ofNullable(ImageIO.read(file)); }catch(IOException e){ JOptionPane.showMessageDialog(this, "ファイルの読み込みに失敗しました"); } } private static double getRatio(int width, int height, BufferedImage image){ double widthRatio = (double)width / image.getWidth(); double heightRatio = (double)height / image.getHeight(); return Math.min(widthRatio, heightRatio); } }