Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package otwieraniezdjec;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.io.File;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.imageio.ImageIO;
- import javax.swing.JFileChooser;
- /**
- *
- * @author P42
- */
- public class Obrazki extends javax.swing.JFrame {
- /**
- * Creates new form NewJFrame
- */
- public Obrazki() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- otworz = new javax.swing.JButton();
- nazwaPliku = new javax.swing.JLabel();
- tablica = new javax.swing.JPanel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- otworz.setText("Otwórz");
- otworz.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- otworzActionPerformed(evt);
- }
- });
- nazwaPliku.setText("jLabel1");
- tablica.setBackground(new java.awt.Color(0, 0, 0));
- javax.swing.GroupLayout tablicaLayout = new javax.swing.GroupLayout(tablica);
- tablica.setLayout(tablicaLayout);
- tablicaLayout.setHorizontalGroup(
- tablicaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 0, Short.MAX_VALUE)
- );
- tablicaLayout.setVerticalGroup(
- tablicaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 260, Short.MAX_VALUE)
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(otworz)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(nazwaPliku)
- .addGap(0, 289, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(tablica, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(otworz)
- .addComponent(nazwaPliku))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(tablica, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- Image obrazek=null;
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- if(obrazek==null) {
- return;
- }
- scaleImageIfNeeded();
- Graphics tablicaG = tablica.getGraphics();
- tablicaG.drawImage(obrazek, 0, 0, this);
- }
- private void scaleImageIfNeeded() {
- if ((obrazek == null) || (obrazek.getWidth(null) != getWidth()) || (obrazek.getHeight(null) != getHeight())) {
- obrazek = obrazek.getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT);
- }
- }
- private void otworzActionPerformed(java.awt.event.ActionEvent evt) {
- JFileChooser oknoOtwierania = new JFileChooser();
- int sposobZamkniecia = oknoOtwierania.showOpenDialog(this);
- if (sposobZamkniecia != JFileChooser.APPROVE_OPTION) {
- return;
- }
- File wybranyPlik = oknoOtwierania.getSelectedFile();
- nazwaPliku.setText( wybranyPlik.getName());
- try {
- obrazek = ImageIO.read(wybranyPlik);
- Graphics g = tablica.getGraphics();
- g.drawImage(obrazek, 0, 0, this);
- } catch (Exception ex) {
- nazwaPliku.setText("Błąd otwierania pliku kmiocie!");
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /*
- * Set the Nimbus look and feel
- */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /*
- * If Nimbus (introduced in Java SE 6) is not available, stay with the
- * default look and feel. For details see
- * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Obrazki.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Obrazki.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Obrazki.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Obrazki.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /*
- * Create and display the form
- */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Obrazki().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JLabel nazwaPliku;
- private javax.swing.JButton otworz;
- private javax.swing.JPanel tablica;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement