Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JImageModifier - JModImage.java

JModImage.java

Caricato da:
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * JModImage.java
  8.  *
  9.  * Created on 24-giu-2010, 20.31.52
  10.  */
  11. package jmodimage.graphics;
  12.  
  13. import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
  14. import java.awt.Color;
  15. import java.awt.Image;
  16. import java.awt.Point;
  17. import java.awt.image.FilteredImageSource;
  18. import java.awt.image.ReplicateScaleFilter;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javax.swing.ImageIcon;
  24. import javax.swing.JColorChooser;
  25. import javax.swing.JFileChooser;
  26. import javax.swing.JOptionPane;
  27. import javax.swing.UIManager;
  28. import javax.swing.UnsupportedLookAndFeelException;
  29. import javax.swing.filechooser.FileFilter;
  30. import jmodimage.Immagine;
  31.  
  32. /**
  33.  *
  34.  * @author Paolo
  35.  */
  36. public class JModImage extends javax.swing.JFrame {
  37.  
  38.     private Immagine img;
  39.  
  40.     /** Creates new form JModImage */
  41.     public JModImage() {
  42.         try {
  43.             UIManager.setLookAndFeel(new NimbusLookAndFeel());
  44.         } catch (UnsupportedLookAndFeelException ex) {
  45.         }
  46.         initComponents();
  47.     }
  48.  
  49.     /** This method is called from within the constructor to
  50.      * initialize the form.
  51.      * WARNING: Do NOT modify this code. The content of this method is
  52.      * always regenerated by the Form Editor.
  53.      */
  54.     @SuppressWarnings("unchecked")
  55.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  56.     private void initComponents() {
  57.  
  58.         imgLabel = new javax.swing.JLabel();
  59.         apriButton = new javax.swing.JButton();
  60.         scrittaLabel = new javax.swing.JLabel();
  61.         scrittaField = new javax.swing.JTextField();
  62.         salvaButton = new javax.swing.JButton();
  63.         xSpinner = new javax.swing.JSpinner();
  64.         xLabel = new javax.swing.JLabel();
  65.         yLabel = new javax.swing.JLabel();
  66.         ySpinner = new javax.swing.JSpinner();
  67.         dimensioniLabel = new javax.swing.JLabel();
  68.  
  69.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  70.         setTitle("JImageModifier - by Paolo Ricciuti (C)");
  71.         setResizable(false);
  72.  
  73.         imgLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  74.         imgLabel.setBorder(javax.swing.BorderFactory.createTitledBorder("Immagine"));
  75.  
  76.         apriButton.setText("Apri");
  77.         apriButton.addActionListener(new java.awt.event.ActionListener() {
  78.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  79.                 apriButtonActionPerformed(evt);
  80.             }
  81.         });
  82.  
  83.         scrittaLabel.setText("Scritta:");
  84.  
  85.         salvaButton.setText("Salva");
  86.         salvaButton.setEnabled(false);
  87.         salvaButton.addActionListener(new java.awt.event.ActionListener() {
  88.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  89.                 salvaButtonActionPerformed(evt);
  90.             }
  91.         });
  92.  
  93.         xLabel.setText("X:");
  94.  
  95.         yLabel.setText("Y:");
  96.  
  97.         dimensioniLabel.setText("Dimensioni:");
  98.  
  99.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  100.         getContentPane().setLayout(layout);
  101.         layout.setHorizontalGroup(
  102.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  103.             .addGroup(layout.createSequentialGroup()
  104.                 .addContainerGap()
  105.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  106.                     .addComponent(imgLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
  107.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  108.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  109.                             .addGroup(layout.createSequentialGroup()
  110.                                 .addComponent(apriButton, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
  111.                                 .addGap(18, 18, 18)
  112.                                 .addComponent(scrittaLabel))
  113.                             .addComponent(dimensioniLabel, javax.swing.GroupLayout.Alignment.LEADING))
  114.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  115.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  116.                             .addComponent(scrittaField, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
  117.                             .addGroup(layout.createSequentialGroup()
  118.                                 .addComponent(xLabel)
  119.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  120.                                 .addComponent(xSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
  121.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  122.                         .addComponent(yLabel)
  123.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  124.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  125.                             .addComponent(salvaButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  126.                             .addComponent(ySpinner, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE))))
  127.                 .addContainerGap())
  128.         );
  129.         layout.setVerticalGroup(
  130.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  131.             .addGroup(layout.createSequentialGroup()
  132.                 .addContainerGap()
  133.                 .addComponent(imgLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
  134.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  135.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  136.                     .addComponent(apriButton)
  137.                     .addComponent(scrittaLabel)
  138.                     .addComponent(scrittaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  139.                     .addComponent(salvaButton))
  140.                 .addGap(18, 18, Short.MAX_VALUE)
  141.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  142.                     .addComponent(dimensioniLabel)
  143.                     .addComponent(ySpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  144.                     .addComponent(yLabel)
  145.                     .addComponent(xSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  146.                     .addComponent(xLabel))
  147.                 .addGap(21, 21, 21))
  148.         );
  149.  
  150.         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  151.         setBounds((screenSize.width-399)/2, (screenSize.height-366)/2, 399, 366);
  152.     }// </editor-fold>//GEN-END:initComponents
  153.  
  154.     private void apriButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apriButtonActionPerformed
  155.         JFileChooser apri = new JFileChooser();
  156.         apri.removeChoosableFileFilter(apri.getAcceptAllFileFilter());
  157.         apri.addChoosableFileFilter(new FileFilter() {
  158.  
  159.             @Override
  160.             public boolean accept(File f) {
  161.                 String path = f.getName().toLowerCase();
  162.                 boolean accept = path.endsWith(".jpg");
  163.                 accept = accept || path.endsWith(".gif");
  164.                 accept = accept || path.endsWith(".png");
  165.                 accept = accept || path.endsWith(".bmp");
  166.                 if (f.isDirectory() || accept) {
  167.                     return true;
  168.                 }
  169.                 return false;
  170.             }
  171.  
  172.             @Override
  173.             public String getDescription() {
  174.                 return "File immagine (jpg, png, gif, bmp)";
  175.             }
  176.         });
  177.         int retval = apri.showOpenDialog(this);
  178.         if (retval == JFileChooser.APPROVE_OPTION) {
  179.             try {
  180.                 this.img = new Immagine(apri.getSelectedFile());
  181.                 Image imm = this.img.getImg();
  182.                 this.dimensioniLabel.setText("Dimensioni: " + imm.getWidth(this) + " x " + imm.getHeight(this));
  183.                 this.setIcon();
  184.                 this.salvaButton.setEnabled(true);
  185.             } catch (IOException ex) {
  186.                 JOptionPane.showMessageDialog(this, "Errore nell'apertura del file");
  187.             }
  188.         }
  189.     }//GEN-LAST:event_apriButtonActionPerformed
  190.  
  191.     private void setIcon() {
  192.         Image imgToLabel = this.img.getImg();
  193.         if (imgToLabel.getWidth(this) > 300 || imgToLabel.getHeight(this) > 200) {
  194.             int larghezza = (200 * imgToLabel.getWidth(this)) / imgToLabel.getHeight(this);
  195.             ReplicateScaleFilter scala = new ReplicateScaleFilter(larghezza, 200);
  196.             FilteredImageSource fis = new FilteredImageSource(imgToLabel.getSource(), scala);
  197.             imgToLabel = createImage(fis);
  198.         }
  199.         this.imgLabel.setIcon(new ImageIcon(imgToLabel));
  200.  
  201.     }
  202.  
  203.     private void salvaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvaButtonActionPerformed
  204.         if (this.img != null) {
  205.             JFileChooser salva = new JFileChooser();
  206.             salva.removeChoosableFileFilter(salva.getAcceptAllFileFilter());
  207.             salva.addChoosableFileFilter(new FileFilter() {
  208.  
  209.                 @Override
  210.                 public boolean accept(File f) {
  211.                     String path = f.getName().toLowerCase();
  212.                     boolean accept = path.endsWith(".jpg");
  213.                     accept = accept || path.endsWith(".gif");
  214.                     accept = accept || path.endsWith(".png");
  215.                     accept = accept || path.endsWith(".bmp");
  216.                     if (f.isDirectory() || accept) {
  217.                         return true;
  218.                     }
  219.                     return false;
  220.                 }
  221.  
  222.                 @Override
  223.                 public String getDescription() {
  224.                     return "File immagine (jpg, png, gif, bmp)";
  225.                 }
  226.             });
  227.             int retval = salva.showSaveDialog(this);
  228.             if (retval == JFileChooser.APPROVE_OPTION) {
  229.                 Color color=JColorChooser.showDialog(this, "Scegli colore stringa", Color.yellow);
  230.                 Integer x = (Integer) this.xSpinner.getValue();
  231.                 Integer y = (Integer) this.ySpinner.getValue();
  232.                 Point punto = new Point(x, y);
  233.                 String scritta = this.scrittaField.getText();
  234.                 this.img.scriviImmagine(punto, scritta, color);
  235.                 File select = salva.getSelectedFile();
  236.                 String path = select.getName();
  237.                 boolean accept = path.endsWith(".jpg");
  238.                 accept = accept || path.endsWith(".gif");
  239.                 accept = accept || path.endsWith(".png");
  240.                 accept = accept || path.endsWith(".bmp");
  241.                 if (!accept) {
  242.                     select = new File(select.getAbsolutePath() + ".jpg");
  243.                 }
  244.                 try {
  245.                     this.img.salvaImmagine(select);
  246.                     this.setIcon();
  247.                     JOptionPane.showMessageDialog(this, "Salvataggio completato");
  248.                 } catch (IOException ex) {
  249.                     JOptionPane.showMessageDialog(this, "Errore nel salvataggio file");
  250.                 }
  251.             }
  252.         }
  253.     }//GEN-LAST:event_salvaButtonActionPerformed
  254.  
  255.     /**
  256.      * @param args the command line arguments
  257.      */
  258.     public static void main(String args[]) {
  259.         java.awt.EventQueue.invokeLater(new Runnable() {
  260.  
  261.             public void run() {
  262.                 new JModImage().setVisible(true);
  263.             }
  264.         });
  265.     }
  266.     // Variables declaration - do not modify//GEN-BEGIN:variables
  267.     private javax.swing.JButton apriButton;
  268.     private javax.swing.JLabel dimensioniLabel;
  269.     private javax.swing.JLabel imgLabel;
  270.     private javax.swing.JButton salvaButton;
  271.     private javax.swing.JTextField scrittaField;
  272.     private javax.swing.JLabel scrittaLabel;
  273.     private javax.swing.JLabel xLabel;
  274.     private javax.swing.JSpinner xSpinner;
  275.     private javax.swing.JLabel yLabel;
  276.     private javax.swing.JSpinner ySpinner;
  277.     // End of variables declaration//GEN-END:variables
  278. }