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
JLineWriter - JLineWriter.java

JLineWriter.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.  * JLineWriter.java
  8.  *
  9.  * Created on 1-mag-2011, 15.21.44
  10.  */
  11. package jlinewriter.graphics;
  12.  
  13. import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
  14. import java.awt.Desktop;
  15. import java.awt.Toolkit;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javax.swing.JFileChooser;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.UIManager;
  23. import javax.swing.UnsupportedLookAndFeelException;
  24. import javax.swing.filechooser.FileFilter;
  25. import jlinewriter.LineWriter;
  26.  
  27. /**
  28.  *
  29.  * @author Paolo
  30.  */
  31. public class JLineWriter extends javax.swing.JFrame {
  32.  
  33.     private LineWriter writer;
  34.     private JFileChooser chooser;
  35.  
  36.     /** Creates new form JLineWriter */
  37.     public JLineWriter() {
  38.         try {
  39.             UIManager.setLookAndFeel(new NimbusLookAndFeel());
  40.         } catch (UnsupportedLookAndFeelException ex) {
  41.             JOptionPane.showMessageDialog(this, "Look & Feel not supported");
  42.         }
  43.         initComponents();
  44.         this.background.setImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/jlinewriter/graphics/images/back.png")));
  45.         this.writer = new LineWriter();
  46.         this.initChooser();
  47.     }
  48.  
  49.     private void initChooser() {
  50.         this.chooser = new JFileChooser();
  51.         this.chooser.removeChoosableFileFilter(this.chooser.getAcceptAllFileFilter());
  52.         this.chooser.addChoosableFileFilter(new FileFilter() {
  53.  
  54.             @Override
  55.             public boolean accept(File f) {
  56.                 String path = f.getAbsolutePath();
  57.                 String ext = path.substring(path.length() - 3);
  58.                 if (ext.equalsIgnoreCase("png") || ext.equalsIgnoreCase("bmp") || ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg") || f.isDirectory()) {
  59.                     return true;
  60.                 }
  61.                 return false;
  62.             }
  63.  
  64.             @Override
  65.             public String getDescription() {
  66.                 return "Immagini (png, bmp, jpg, jpeg)";
  67.             }
  68.         });
  69.     }
  70.  
  71.     /** This method is called from within the constructor to
  72.      * initialize the form.
  73.      * WARNING: Do NOT modify this code. The content of this method is
  74.      * always regenerated by the Form Editor.
  75.      */
  76.     @SuppressWarnings("unchecked")
  77.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  78.     private void initComponents() {
  79.  
  80.         background = new jlinewriter.graphics.JBackground();
  81.         panel = new javax.swing.JPanel();
  82.         fraseLabel = new javax.swing.JLabel();
  83.         frase = new javax.swing.JTextField();
  84.         saveButton = new javax.swing.JButton();
  85.         textPanel = new javax.swing.JPanel();
  86.         textLabel = new javax.swing.JLabel();
  87.  
  88.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  89.         setTitle("JLineWriter - by Paolo Ricciuti (C)");
  90.         setResizable(false);
  91.  
  92.         panel.setBackground(new java.awt.Color(0, 0, 0));
  93.  
  94.         fraseLabel.setForeground(new java.awt.Color(255, 255, 255));
  95.         fraseLabel.setText("Frase:");
  96.  
  97.         saveButton.setText("Salva Immagine");
  98.         saveButton.addActionListener(new java.awt.event.ActionListener() {
  99.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  100.                 saveButtonActionPerformed(evt);
  101.             }
  102.         });
  103.  
  104.         javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
  105.         panel.setLayout(panelLayout);
  106.         panelLayout.setHorizontalGroup(
  107.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  108.             .addGroup(panelLayout.createSequentialGroup()
  109.                 .addContainerGap()
  110.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  111.                     .addGroup(panelLayout.createSequentialGroup()
  112.                         .addComponent(fraseLabel)
  113.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  114.                         .addComponent(frase, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE))
  115.                     .addComponent(saveButton))
  116.                 .addContainerGap())
  117.         );
  118.         panelLayout.setVerticalGroup(
  119.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  120.             .addGroup(panelLayout.createSequentialGroup()
  121.                 .addContainerGap()
  122.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  123.                     .addComponent(fraseLabel)
  124.                     .addComponent(frase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  125.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
  126.                 .addComponent(saveButton)
  127.                 .addGap(20, 20, 20))
  128.         );
  129.  
  130.         textPanel.setBackground(new java.awt.Color(0, 0, 0));
  131.  
  132.         textLabel.setForeground(new java.awt.Color(255, 255, 255));
  133.         textLabel.setText("<html>Questa applicazione permette di creare delle foto con una frase nascosta. Il programma nasconde la frase in una immagine che all'apparenza è formata da sole linee nere ma che diventerà visibile inclinando lo schermo del computer oppure strizzando un poco gli occhi. Un esempio di foto di questo genere si trova di sfondo a questo programma!!</html>");
  134.  
  135.         javax.swing.GroupLayout textPanelLayout = new javax.swing.GroupLayout(textPanel);
  136.         textPanel.setLayout(textPanelLayout);
  137.         textPanelLayout.setHorizontalGroup(
  138.             textPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  139.             .addGroup(textPanelLayout.createSequentialGroup()
  140.                 .addContainerGap()
  141.                 .addComponent(textLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
  142.                 .addContainerGap())
  143.         );
  144.         textPanelLayout.setVerticalGroup(
  145.             textPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  146.             .addGroup(textPanelLayout.createSequentialGroup()
  147.                 .addContainerGap()
  148.                 .addComponent(textLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
  149.                 .addContainerGap())
  150.         );
  151.  
  152.         javax.swing.GroupLayout backgroundLayout = new javax.swing.GroupLayout(background);
  153.         background.setLayout(backgroundLayout);
  154.         backgroundLayout.setHorizontalGroup(
  155.             backgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  156.             .addGroup(backgroundLayout.createSequentialGroup()
  157.                 .addContainerGap()
  158.                 .addGroup(backgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  159.                     .addComponent(textPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  160.                     .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  161.                 .addContainerGap())
  162.         );
  163.         backgroundLayout.setVerticalGroup(
  164.             backgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  165.             .addGroup(backgroundLayout.createSequentialGroup()
  166.                 .addContainerGap()
  167.                 .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  168.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
  169.                 .addComponent(textPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  170.                 .addContainerGap())
  171.         );
  172.  
  173.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  174.         getContentPane().setLayout(layout);
  175.         layout.setHorizontalGroup(
  176.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  177.             .addComponent(background, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  178.         );
  179.         layout.setVerticalGroup(
  180.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  181.             .addComponent(background, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  182.         );
  183.  
  184.         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  185.         setBounds((screenSize.width-606)/2, (screenSize.height-428)/2, 606, 428);
  186.     }// </editor-fold>//GEN-END:initComponents
  187.  
  188.     private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
  189.         int retval=this.chooser.showSaveDialog(this);
  190.         if(retval==JFileChooser.APPROVE_OPTION){
  191.             try {
  192.                 this.writer.createImage(this.frase.getText());
  193.                 this.writer.saveImage(this.chooser.getSelectedFile());
  194.                 JOptionPane.showMessageDialog(this, "Immagine salvata correttamente!!");
  195.             } catch (IOException ex) {
  196.                 JOptionPane.showMessageDialog(this, "IO Exception");
  197.             }
  198.         }
  199.     }//GEN-LAST:event_saveButtonActionPerformed
  200.  
  201.     /**
  202.      * @param args the command line arguments
  203.      */
  204.     public static void main(String args[]) {
  205.         java.awt.EventQueue.invokeLater(new Runnable() {
  206.  
  207.             public void run() {
  208.                 new JLineWriter().setVisible(true);
  209.             }
  210.         });
  211.     }
  212.     // Variables declaration - do not modify//GEN-BEGIN:variables
  213.     private jlinewriter.graphics.JBackground background;
  214.     private javax.swing.JTextField frase;
  215.     private javax.swing.JLabel fraseLabel;
  216.     private javax.swing.JPanel panel;
  217.     private javax.swing.JButton saveButton;
  218.     private javax.swing.JLabel textLabel;
  219.     private javax.swing.JPanel textPanel;
  220.     // End of variables declaration//GEN-END:variables
  221. }