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
Gioco del 15! - Frame.java

Frame.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.  * Prova.java
  8.  *
  9.  * Created on 20-apr-2010, 21.06.55
  10.  */
  11. package game.graphics;
  12.  
  13. import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
  14. import game.Ground;
  15. import java.awt.Color;
  16. import java.awt.Dimension;
  17. import java.awt.Image;
  18. import java.awt.Toolkit;
  19. import java.awt.event.MouseAdapter;
  20. import java.awt.event.MouseEvent;
  21. import java.awt.image.CropImageFilter;
  22. import java.awt.image.FilteredImageSource;
  23. import java.awt.image.ReplicateScaleFilter;
  24. import java.io.File;
  25. import javax.swing.ImageIcon;
  26. import javax.swing.JLabel;
  27. import javax.swing.JOptionPane;
  28. import javax.swing.UIManager;
  29. import javax.swing.UnsupportedLookAndFeelException;
  30. import javax.swing.border.LineBorder;
  31. import javax.swing.filechooser.FileFilter;
  32. import javax.swing.filechooser.FileNameExtensionFilter;
  33.  
  34. /**
  35.  *
  36.  * @author paolo
  37.  */
  38. public class Frame extends javax.swing.JFrame {
  39.  
  40.     private Image[] imgTasselli;
  41.     private Image imgRisolta;
  42.     private boolean sceltaImage;
  43.     private JLabel[][] tasselli;
  44.     private Ground ground;
  45.  
  46.     private class Mouse extends MouseAdapter {
  47.  
  48.         private int i;
  49.         private int j;
  50.  
  51.         public Mouse(int pI, int pJ) {
  52.             this.i = pI;
  53.             this.j = pJ;
  54.         }
  55.  
  56.         @Override
  57.         public void mouseClicked(MouseEvent e) {
  58.             ground.sposta(this.i, this.j);
  59.             ridisegna();
  60.             if (ground.isWinner()) {
  61.                 JOptionPane.showMessageDialog(rootPane, "Hai vinto!!!");
  62.                 removeMouseListenerToLabel();
  63.             }
  64.         }
  65.     }
  66.  
  67.     /** Creates new form Prova */
  68.     public Frame() {
  69.         try {
  70.             UIManager.setLookAndFeel(new NimbusLookAndFeel());
  71.         } catch (UnsupportedLookAndFeelException ex) {
  72.         }
  73.         initComponents();
  74.         jFileChooser1.removeChoosableFileFilter(jFileChooser1.getFileFilter());
  75.         jFileChooser1.addChoosableFileFilter(new FileFilter() {
  76.  
  77.             @Override
  78.             public boolean accept(File f) {
  79.                 String name=f.getName().toLowerCase();
  80.                 if(name.endsWith("jpg")||name.endsWith("gif")||name.endsWith("png")||name.endsWith("jpeg")||name.endsWith("bmp")||f.isDirectory()){
  81.                     return true;
  82.                 }
  83.                 return false;
  84.             }
  85.  
  86.             @Override
  87.             public String getDescription() {
  88.                 return "File immagine (jpg, gif, png, jpeg, png, bmp)";
  89.             }
  90.         });
  91.         this.ground = new Ground();
  92.         this.initLabel();
  93.         this.sceltaImage = false;
  94.     }
  95.  
  96.     private void initLabel() {
  97.         this.tasselli = new JLabel[4][4];
  98.         for (int i = 0; i < 4; i++) {
  99.             for (int j = 0; j < 4; j++) {
  100.                 this.tasselli[i][j] = new JLabel();
  101.                 this.tasselli[i][j].setBorder(new LineBorder(Color.BLACK));
  102.                 this.jPanel1.add(this.tasselli[i][j]);
  103.             }
  104.         }
  105.     }
  106.  
  107.     private void addMouseListenerToLabel() {
  108.         for (int i = 0; i < 4; i++) {
  109.             for (int j = 0; j < 4; j++) {
  110.                 this.tasselli[i][j].addMouseListener(new Mouse(i, j));
  111.             }
  112.         }
  113.     }
  114.  
  115.     private void removeMouseListenerToLabel() {
  116.         for (int i = 0; i < 4; i++) {
  117.             for (int j = 0; j < 4; j++) {
  118.                 this.tasselli[i][j].removeMouseListener(this.tasselli[i][j].getMouseListeners()[0]);
  119.             }
  120.         }
  121.     }
  122.  
  123.     @SuppressWarnings("static-access")
  124.     private void cutImage(boolean pDefault) {
  125.         String path = "/game/graphics/images/def.png";
  126.         int retval = jFileChooser1.APPROVE_OPTION - 1;
  127.         if (!pDefault) {
  128.             retval = this.jFileChooser1.showOpenDialog(this);
  129.             if (retval == jFileChooser1.APPROVE_OPTION) {
  130.                 path = this.jFileChooser1.getSelectedFile().getAbsolutePath();
  131.             }
  132.         }
  133.         this.imgTasselli = new Image[16];
  134.         int cont = 0;
  135.         Image img = null;
  136.         if (pDefault) {
  137.             img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(path));
  138.         } else {
  139.             img = Toolkit.getDefaultToolkit().getImage(path);
  140.         }
  141.         ReplicateScaleFilter scala = new ReplicateScaleFilter(300, 300);
  142.         FilteredImageSource fis = new FilteredImageSource(img.getSource(), scala);
  143.         img = createImage(fis);
  144.         this.imgRisolta=img;
  145.         for (int i = 0; i < 4; i++) {
  146.             for (int j = 0; j < 4; j++) {
  147.                 CropImageFilter cutting = new CropImageFilter(j * 75, i * 75, 75, 75);
  148.                 fis = new FilteredImageSource(img.getSource(), cutting);
  149.                 this.imgTasselli[cont] = createImage(fis);
  150.                 cont++;
  151.             }
  152.         }
  153.         if (retval == this.jFileChooser1.APPROVE_OPTION) {
  154.             this.sceltaImage = true;
  155.             JOptionPane.showMessageDialog(rootPane, "Immagine selezionata, premere nuovo gioco per iniziare a giocare!");
  156.         }
  157.  
  158.     }
  159.  
  160.     public void ridisegna() {
  161.         int[][] tavolo = ground.toMatrix();
  162.         for (int i = 0; i < 4; i++) {
  163.             for (int j = 0; j < 4; j++) {
  164.                 if (tavolo[i][j] == 16) {
  165.                     this.tasselli[i][j].setIcon(new ImageIcon(getClass().getResource("/game/graphics/images/vuoto.png")));
  166.                 } else {
  167.                     this.tasselli[i][j].setIcon(new ImageIcon(this.imgTasselli[(tavolo[i][j]) - 1]));
  168.                 }
  169.             }
  170.         }
  171.     }
  172.  
  173.     public void newGame() {
  174.         this.ground.shuffle();
  175.         this.ridisegna();
  176.         this.addMouseListenerToLabel();
  177.     }
  178.  
  179.     /** This method is called from within the constructor to
  180.      * initialize the form.
  181.      * WARNING: Do NOT modify this code. The content of this method is
  182.      * always regenerated by the Form Editor.
  183.      */
  184.     @SuppressWarnings("unchecked")
  185.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  186.     private void initComponents() {
  187.  
  188.         jFileChooser1 = new javax.swing.JFileChooser();
  189.         jPanel1 = new javax.swing.JPanel();
  190.         jPanel2 = new javax.swing.JPanel();
  191.         jButton2 = new javax.swing.JButton();
  192.         jButton1 = new javax.swing.JButton();
  193.         jButton3 = new javax.swing.JButton();
  194.  
  195.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  196.         setTitle("Gioco del 15 - by Paolo Ricciuti");
  197.         setResizable(false);
  198.  
  199.         jPanel1.setBackground(new java.awt.Color(224, 14, 14));
  200.         jPanel1.setLayout(new java.awt.GridLayout(4, 4));
  201.  
  202.         jPanel2.setBackground(new java.awt.Color(224, 14, 14));
  203.         jPanel2.setLayout(null);
  204.  
  205.         jButton2.setText("Seleziona immagine");
  206.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  207.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  208.                 jButton2ActionPerformed(evt);
  209.             }
  210.         });
  211.         jPanel2.add(jButton2);
  212.         jButton2.setBounds(10, 10, 170, 30);
  213.  
  214.         jButton1.setText("Nuovo gioco");
  215.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  216.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  217.                 jButton1ActionPerformed(evt);
  218.             }
  219.         });
  220.         jPanel2.add(jButton1);
  221.         jButton1.setBounds(185, 10, 120, 30);
  222.  
  223.         jButton3.setText("Guarda l'immagine risolta");
  224.         jButton3.setEnabled(false);
  225.         jButton3.addActionListener(new java.awt.event.ActionListener() {
  226.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  227.                 jButton3ActionPerformed(evt);
  228.             }
  229.         });
  230.         jPanel2.add(jButton3);
  231.         jButton3.setBounds(10, 50, 290, 30);
  232.  
  233.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  234.         getContentPane().setLayout(layout);
  235.         layout.setHorizontalGroup(
  236.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  237.             .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
  238.             .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
  239.         );
  240.         layout.setVerticalGroup(
  241.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  242.             .addGroup(layout.createSequentialGroup()
  243.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
  244.                 .addContainerGap(99, Short.MAX_VALUE))
  245.             .addGroup(layout.createSequentialGroup()
  246.                 .addGap(310, 310, 310)
  247.                 .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
  248.         );
  249.  
  250.         pack();
  251.     }// </editor-fold>//GEN-END:initComponents
  252.  
  253.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
  254.         this.cutImage(false);
  255. }//GEN-LAST:event_jButton2ActionPerformed
  256.  
  257.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  258.         if (!this.sceltaImage) {
  259.             this.cutImage(true);
  260.         }
  261.         this.jButton3.setEnabled(true);
  262.         this.newGame();
  263. }//GEN-LAST:event_jButton1ActionPerformed
  264.  
  265.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
  266.         LookImg look= new LookImg(this.imgRisolta);
  267.         Dimension screen= Toolkit.getDefaultToolkit().getScreenSize();
  268.         int x=(screen.width-look.getSize().width)/2;
  269.         int y=(screen.height-look.getSize().height)/2;
  270.         look.setLocation(x, y);
  271.         look.setVisible(true);
  272.     }//GEN-LAST:event_jButton3ActionPerformed
  273.  
  274.     /**
  275.      * @param args the command line arguments
  276.      */
  277.     public static void main(String args[]) {
  278.         java.awt.EventQueue.invokeLater(new Runnable() {
  279.  
  280.             public void run() {
  281.                 Frame f=new Frame();
  282.                 Dimension screen= Toolkit.getDefaultToolkit().getScreenSize();
  283.                 int x=(screen.width-f.getSize().width)/2;
  284.                 int y=(screen.height-f.getSize().height)/2;
  285.                 f.setLocation(x, y);
  286.                 f.setVisible(true);
  287.             }
  288.         });
  289.     }
  290.     // Variables declaration - do not modify//GEN-BEGIN:variables
  291.     private javax.swing.JButton jButton1;
  292.     private javax.swing.JButton jButton2;
  293.     private javax.swing.JButton jButton3;
  294.     private javax.swing.JFileChooser jFileChooser1;
  295.     private javax.swing.JPanel jPanel1;
  296.     private javax.swing.JPanel jPanel2;
  297.     // End of variables declaration//GEN-END:variables
  298. }