Advertisement
Guest User

Obrazek

a guest
Apr 1st, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1. package otwieraniezdjec;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import javax.imageio.ImageIO;
  10. import javax.swing.JFileChooser;
  11.  
  12.  
  13. /**
  14.  *
  15.  * @author P42
  16.  */
  17. public class Obrazki extends javax.swing.JFrame {
  18.  
  19.     /**
  20.      * Creates new form NewJFrame
  21.      */
  22.     public Obrazki() {
  23.         initComponents();
  24.     }
  25.  
  26.     /**
  27.      * This method is called from within the constructor to initialize the form.
  28.      * WARNING: Do NOT modify this code. The content of this method is always
  29.      * regenerated by the Form Editor.
  30.      */
  31.     @SuppressWarnings("unchecked")
  32.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  33.     private void initComponents() {
  34.  
  35.         otworz = new javax.swing.JButton();
  36.         nazwaPliku = new javax.swing.JLabel();
  37.         tablica = new javax.swing.JPanel();
  38.  
  39.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  40.  
  41.         otworz.setText("Otwórz");
  42.         otworz.addActionListener(new java.awt.event.ActionListener() {
  43.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  44.                 otworzActionPerformed(evt);
  45.             }
  46.         });
  47.  
  48.         nazwaPliku.setText("jLabel1");
  49.  
  50.         tablica.setBackground(new java.awt.Color(0, 0, 0));
  51.  
  52.         javax.swing.GroupLayout tablicaLayout = new javax.swing.GroupLayout(tablica);
  53.         tablica.setLayout(tablicaLayout);
  54.         tablicaLayout.setHorizontalGroup(
  55.             tablicaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  56.             .addGap(0, 0, Short.MAX_VALUE)
  57.         );
  58.         tablicaLayout.setVerticalGroup(
  59.             tablicaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  60.             .addGap(0, 260, Short.MAX_VALUE)
  61.         );
  62.  
  63.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  64.         getContentPane().setLayout(layout);
  65.         layout.setHorizontalGroup(
  66.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  67.             .addGroup(layout.createSequentialGroup()
  68.                 .addComponent(otworz)
  69.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  70.                 .addComponent(nazwaPliku)
  71.                 .addGap(0, 289, Short.MAX_VALUE))
  72.             .addGroup(layout.createSequentialGroup()
  73.                 .addContainerGap()
  74.                 .addComponent(tablica, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  75.                 .addContainerGap())
  76.         );
  77.         layout.setVerticalGroup(
  78.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  79.             .addGroup(layout.createSequentialGroup()
  80.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  81.                     .addComponent(otworz)
  82.                     .addComponent(nazwaPliku))
  83.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  84.                 .addComponent(tablica, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  85.                 .addContainerGap())
  86.         );
  87.  
  88.         pack();
  89.     }// </editor-fold>                        
  90.     Image obrazek=null;
  91.     @Override
  92.     public void paint(Graphics g) {
  93.         super.paint(g);
  94.         if(obrazek==null) {
  95.             return;
  96.        
  97.          
  98.         }
  99.         scaleImageIfNeeded();
  100.         Graphics tablicaG = tablica.getGraphics();
  101.         tablicaG.drawImage(obrazek, 0, 0, this);
  102.     }
  103.     private void scaleImageIfNeeded() {
  104.                 if ((obrazek == null) || (obrazek.getWidth(null) != getWidth()) || (obrazek.getHeight(null) != getHeight())) {
  105.                         obrazek = obrazek.getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT);
  106.                 }
  107.         }
  108.     private void otworzActionPerformed(java.awt.event.ActionEvent evt) {                                      
  109.        
  110.                    JFileChooser oknoOtwierania = new JFileChooser();
  111.                    int sposobZamkniecia = oknoOtwierania.showOpenDialog(this);
  112.                    if (sposobZamkniecia != JFileChooser.APPROVE_OPTION) {
  113.                        return;
  114.                    }
  115.                    
  116.                    File wybranyPlik = oknoOtwierania.getSelectedFile();
  117.                    nazwaPliku.setText( wybranyPlik.getName());
  118.         try {
  119.             obrazek = ImageIO.read(wybranyPlik);
  120.             Graphics g = tablica.getGraphics();
  121.             g.drawImage(obrazek, 0, 0, this);
  122.         } catch (Exception ex) {
  123.             nazwaPliku.setText("Błąd otwierania pliku kmiocie!");
  124.            
  125.         }
  126.          
  127.     }                                      
  128.  
  129.     /**
  130.      * @param args the command line arguments
  131.      */
  132.     public static void main(String args[]) {
  133.         /*
  134.          * Set the Nimbus look and feel
  135.          */
  136.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  137.         /*
  138.          * If Nimbus (introduced in Java SE 6) is not available, stay with the
  139.          * default look and feel. For details see
  140.          * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  141.          */
  142.         try {
  143.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  144.                 if ("Nimbus".equals(info.getName())) {
  145.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  146.                     break;
  147.                 }
  148.             }
  149.         } catch (ClassNotFoundException ex) {
  150.             java.util.logging.Logger.getLogger(Obrazki.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  151.         } catch (InstantiationException ex) {
  152.             java.util.logging.Logger.getLogger(Obrazki.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  153.         } catch (IllegalAccessException ex) {
  154.             java.util.logging.Logger.getLogger(Obrazki.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  155.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  156.             java.util.logging.Logger.getLogger(Obrazki.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  157.         }
  158.         //</editor-fold>
  159.  
  160.         /*
  161.          * Create and display the form
  162.          */
  163.         java.awt.EventQueue.invokeLater(new Runnable() {
  164.  
  165.             public void run() {
  166.                 new Obrazki().setVisible(true);
  167.             }
  168.         });
  169.     }
  170.     // Variables declaration - do not modify                    
  171.     private javax.swing.JLabel nazwaPliku;
  172.     private javax.swing.JButton otworz;
  173.     private javax.swing.JPanel tablica;
  174.     // End of variables declaration                  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement