Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  2.         if (evt.getSource() == jButton1) {
  3.             int returnVal = jFileChooser.showOpenDialog(MainJFrame.this);
  4.             if (returnVal == JFileChooser.APPROVE_OPTION) {
  5.                 try {
  6.                     BufferedImage bufferedImage = ImageIO.read(Files.newInputStream(Paths.get(jFileChooser.getSelectedFile().getAbsolutePath())));
  7.                     this.image = bufferedImage;
  8.                     this.aspectRatio = (double) image.getWidth(rootPane) / (double) image.getHeight(rootPane);
  9.                     jLabel1.setIcon(new ImageIcon(image.getScaledInstance(jLabel1.getWidth(), (int) (jLabel1.getWidth() / this.aspectRatio), Image.SCALE_SMOOTH)));
  10.                 } catch (IOException ex) {
  11.                     JOptionPane.showMessageDialog(this, "Can't load file.");
  12.                 }
  13.             }
  14.         }
  15.     }                                        
  16.  
  17.     private void formComponentResized(java.awt.event.ComponentEvent evt) {                                      
  18.         if (image != null) {
  19.             jLabel1.setSize(this.getWidth(), (int) (this.getWidth() / this.aspectRatio));
  20.             jLabel1.setIcon(new ImageIcon(image.getScaledInstance(jLabel1.getWidth(), (int) (jLabel1.getWidth() / this.aspectRatio), Image.SCALE_SMOOTH)));
  21.         }
  22.     }                                    
  23.    
  24.     public static void main(String args[]) {
  25.         java.awt.EventQueue.invokeLater(new Runnable() {
  26.             public void run() {
  27.                 new MainJFrame().setVisible(true);
  28.             }
  29.         });
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement