Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void displayImage(final Image image, String message) throws InterruptedException {
- if(image==null)
- throw new IllegalArgumentException("No image to draw. Given image is null.");
- //The window
- JFrame frame = new JFrame();
- //Topmost component of the window
- Container main = frame.getContentPane();
- //Turns out this is probably the simplest way to render image on screen
- //with guaranteed 1:1 aspect ratio
- JPanel panel = new JPanel() {
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(image, 0, 0, null);
- }
- };
- panel.setSize(image.getWidth(null), image.getHeight(null));
- //Put the image drawer in the topmost window component
- main.add(panel);
- //System.out.println(image.getWidth(null)+", "+image.getHeight(null));
- //frame.pack();
- frame.setTitle(message+" ["+image.getWidth(null)+" x "+image.getHeight(null)+"]");
- //Set window size to the image size plus some padding dimensions
- frame.pack();
- frame.setVisible(true);
- final Thread t = Thread.currentThread();
- frame.addWindowListener(new WindowListener() {
- @Override
- public void windowOpened(WindowEvent e) {}
- @Override
- public void windowClosing(WindowEvent e) {
- synchronized(t) {t.notify();}
- //System.out.println("Closing");
- frame.dispose();
- }
- @Override
- public void windowClosed(WindowEvent e) {
- //System.out.println("Closed");
- //synchronized(t) {
- //t.notify();
- //}
- }
- @Override
- public void windowIconified(WindowEvent e) {}
- @Override
- public void windowDeiconified(WindowEvent e) {}
- @Override
- public void windowActivated(WindowEvent e) {}
- @Override
- public void windowDeactivated(WindowEvent e) {}
- });
- synchronized(t) {
- t.wait();
- //System.out.println("Wait over.");
- }
- //JOptionPane.showMessageDialog(null, scrollPane, message, javax.swing.JOptionPane.INFORMATION_MESSAGE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement