Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. package projektjpwmiijedralskik1;
  2.  
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import java.io.File;
  6. import javax.swing.ImageIcon;
  7. import javax.swing.JFileChooser;
  8. import javax.swing.filechooser.FileNameExtensionFilter;
  9. import static java.awt.Image.SCALE_SMOOTH;
  10.  
  11. /**
  12.  *
  13.  * @author Konrad
  14.  */
  15. public class JFrame extends javax.swing.JFrame {
  16.  
  17.     private Image image;
  18.  
  19.     public JFrame() {
  20.         initComponents();
  21.  
  22.     }
  23.  
  24.     @SuppressWarnings("unchecked")
  25.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  26.     private void initComponents() {
  27.  
  28.         jLabel1 = new javax.swing.JLabel();
  29.         jButton1 = new javax.swing.JButton();
  30.  
  31.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  32.         setTitle("Konrad Jędralski");
  33.  
  34.         jLabel1.setText("Obraz...");
  35.         jLabel1.addComponentListener(new java.awt.event.ComponentAdapter() {
  36.             public void componentResized(java.awt.event.ComponentEvent evt) {
  37.                 jLabel1ComponentResized(evt);
  38.             }
  39.         });
  40.  
  41.         jButton1.setText("Wybierz obraz");
  42.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  43.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  44.                 jButton1ActionPerformed(evt);
  45.             }
  46.         });
  47.  
  48.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  49.         getContentPane().setLayout(layout);
  50.         layout.setHorizontalGroup(
  51.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  52.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  53.                 .addContainerGap()
  54.                 .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  55.                 .addContainerGap())
  56.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  57.                 .addContainerGap(338, Short.MAX_VALUE)
  58.                 .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
  59.                 .addContainerGap(320, Short.MAX_VALUE))
  60.         );
  61.         layout.setVerticalGroup(
  62.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  63.             .addGroup(layout.createSequentialGroup()
  64.                 .addContainerGap()
  65.                 .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
  66.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  67.                 .addComponent(jButton1)
  68.                 .addGap(17, 17, 17))
  69.         );
  70.  
  71.         pack();
  72.     }// </editor-fold>                        
  73.  
  74.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  75.  
  76.         JFrame frame = new JFrame();
  77.         frame.setLocation(200, 200);
  78.  
  79.         JFileChooser fileChooser = new JFileChooser();
  80.  
  81.         FileNameExtensionFilter filter = new FileNameExtensionFilter("Grafika JPG", "jpg");
  82.  
  83.         fileChooser.setFileFilter(filter);
  84.         fileChooser.showOpenDialog(frame);
  85.  
  86.         File file = fileChooser.getSelectedFile();
  87.  
  88.         Toolkit toolkit = Toolkit.getDefaultToolkit();
  89.         image = toolkit.getImage(file.toString());
  90.  
  91.         jLabel1.setText("");
  92.  
  93.         int labelWidth = jLabel1.getWidth();
  94.         int labelHeight = jLabel1.getHeight();
  95.  
  96.         Image imageTemp = image.getScaledInstance(labelWidth, labelHeight, Image.SCALE_SMOOTH);
  97.  
  98.         ImageIcon imageIcon = new ImageIcon(imageTemp);
  99.  
  100.         jLabel1.setIcon(imageIcon);
  101.  
  102.  
  103.     }                                        
  104.  
  105.     private void jLabel1ComponentResized(java.awt.event.ComponentEvent evt) {                                        
  106.  
  107.         if (image != null) {            
  108.             jLabel1.setSize(this.getWidth(), this.getHeight());
  109.             Image imageTemp = image.getScaledInstance(jLabel1.getWidth(), jLabel1.getHeight(), Image.SCALE_SMOOTH);
  110.             ImageIcon imageIcon = new ImageIcon(imageTemp);
  111.             jLabel1.setIcon(imageIcon);            
  112.         }        
  113.     }                                        
  114.  
  115.     public static void main(String args[]) {
  116.  
  117.         java.awt.EventQueue.invokeLater(new Runnable() {
  118.             public void run() {
  119.                 new JFrame().setVisible(true);
  120.             }
  121.         });
  122.     }
  123.  
  124.     // Variables declaration - do not modify                    
  125.     private javax.swing.JButton jButton1;
  126.     private javax.swing.JLabel jLabel1;
  127.     // End of variables declaration                  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement