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

Arcier.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.  * Arcier.java
  8.  *
  9.  * Created on 26-apr-2010, 15.34.33
  10.  */
  11. package arcier.graphics;
  12.  
  13. import java.awt.Color;
  14. import java.awt.Dimension;
  15. import java.awt.Point;
  16. import java.awt.Rectangle;
  17. import java.awt.Toolkit;
  18. import java.awt.event.KeyAdapter;
  19. import java.awt.event.KeyEvent;
  20. import java.awt.event.MouseAdapter;
  21. import java.awt.event.MouseEvent;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import javax.swing.ImageIcon;
  25. import javax.swing.JLabel;
  26. import javax.swing.border.LineBorder;
  27.  
  28. /**
  29.  *
  30.  * @author paolo
  31.  */
  32. public class Arcier extends javax.swing.JFrame {
  33.  
  34.     private class Palloncini extends Thread {
  35.  
  36.         @Override
  37.         public void run() {
  38.             boolean control=true;
  39.             while (control) {
  40.                 for (int i = 0; i < 10; i++) {
  41.                     if (palloncini[i].getLocation().y >= 700) {
  42.                         palloncini[i].setLocation(palloncini[i].getLocation().x, -10);
  43.                     }
  44.                     palloncini[i].setLocation(palloncini[i].getLocation().x, palloncini[i].getLocation().y + 1);
  45.                     checkImpact(palloncini[i]);
  46.                     if(checkWinner()){
  47.                         win.setVisible(true);
  48.                         control=false;
  49.                     }
  50.                 }
  51.                 try {
  52.                     sleep(10);
  53.                 } catch (InterruptedException ex) {
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     private class Freccia extends Thread {
  60.  
  61.         @Override
  62.         public void run() {
  63.             for (int i = 0; i < 400; i++) {
  64.                 Point loc = freccia.getLocation();
  65.                 freccia.setLocation(loc.x + 2, loc.y);
  66.                 try {
  67.                     sleep(10);
  68.                 } catch (InterruptedException ex) {
  69.                 }
  70.             }
  71.             freccia.setVisible(false);
  72.             freccia.setLocation(freccia.getLocation().x - 800, freccia.getLocation().y);
  73.             addMouseListener(new Mouse());
  74.         }
  75.     }
  76.  
  77.     private class Mouse extends MouseAdapter {
  78.  
  79.         @Override
  80.         public void mouseMoved(MouseEvent e) {
  81.             int y = e.getPoint().y;
  82.             arciere.setLocation(arciere.getLocation().x, y - arciere.getSize().height / 2);
  83.         }
  84.  
  85.         @Override
  86.         public void mousePressed(MouseEvent e) {
  87.             arciere.setIcon(new ImageIcon(getClass().getResource("/arcier/graphics/images/hero_armed.png")));
  88.         }
  89.  
  90.         @Override
  91.         public void mouseReleased(MouseEvent e) {
  92.             int y = e.getPoint().y;
  93.             freccia.setVisible(true);
  94.             arciere.setIcon(new ImageIcon(getClass().getResource("/arcier/graphics/images/hero_without_arrow.png")));
  95.             freccia.setLocation(freccia.getLocation().x+30, (y - freccia.getSize().height / 2)-10);
  96.             Freccia f = new Freccia();
  97.             f.start();
  98.             removeMouseListener(getMouseListeners()[0]);
  99.         }
  100.     }
  101.  
  102.     private JLabel[] palloncini;
  103.  
  104.     /** Creates new form Arcier */
  105.     public Arcier() {
  106.         initComponents();
  107.         this.win.setVisible(false);
  108.         this.freccia.setVisible(false);
  109.         initLabel();
  110.         this.addMouseMotionListener(new Mouse());
  111.         this.addMouseListener(new Mouse());
  112.         new Palloncini().start();
  113.     }
  114.  
  115.     private void checkImpact(JLabel pPalloncino){
  116.         if(this.impact(pPalloncino)){
  117.             pPalloncino.setVisible(false);
  118.         }
  119.     }
  120.  
  121.     private boolean checkWinner(){
  122.         int cont=0;
  123.         for(int i=0; i<10; i++){
  124.             if(!this.palloncini[i].isVisible()){
  125.                 cont++;
  126.             }
  127.         }
  128.         return cont==10;
  129.     }
  130.  
  131.     private boolean impact(JLabel pPalloncino){
  132.         Dimension frecciaD= this.freccia.getSize();
  133.         Dimension palloncinoD= pPalloncino.getSize();
  134.         Point locFreccia= this.freccia.getLocation();
  135.         Point locPalloncino= pPalloncino.getLocation();
  136.         Rectangle frecciaR= new Rectangle(locFreccia, frecciaD);
  137.         Rectangle palloncinoR= new Rectangle(locPalloncino, palloncinoD);
  138.         return frecciaR.intersects(palloncinoR);
  139.     }
  140.  
  141.     private void initLabel() {
  142.         this.palloncini = new JLabel[10];
  143.         for (int i = 0; i < 10; i++) {
  144.             this.palloncini[i] = new JLabel(new ImageIcon(getClass().getResource("/arcier/graphics/images/palloncino.png")));
  145.             this.palloncini[i].setVisible(true);
  146.             this.palloncini[i].setSize(50, 80);
  147.             this.palloncini[i].setLocation(270 + (i * 50), -80);
  148.             this.jPanel1.add(this.palloncini[i]);
  149.         }
  150.     }
  151.  
  152.     /** This method is called from within the constructor to
  153.      * initialize the form.
  154.      * WARNING: Do NOT modify this code. The content of this method is
  155.      * always regenerated by the Form Editor.
  156.      */
  157.     @SuppressWarnings("unchecked")
  158.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  159.     private void initComponents() {
  160.  
  161.         jPanel1 = new javax.swing.JPanel();
  162.         arciere = new javax.swing.JLabel();
  163.         freccia = new javax.swing.JLabel();
  164.         win = new javax.swing.JLabel();
  165.  
  166.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  167.         setTitle("Arcier - by Paolo Ricciuti");
  168.         setResizable(false);
  169.  
  170.         jPanel1.setBackground(new java.awt.Color(78, 204, 106));
  171.         jPanel1.setLayout(null);
  172.  
  173.         arciere.setIcon(new javax.swing.ImageIcon(getClass().getResource("/arcier/graphics/images/hero_without_arrow.png"))); // NOI18N
  174.         arciere.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
  175.         arciere.setFocusable(false);
  176.         arciere.setRequestFocusEnabled(false);
  177.         jPanel1.add(arciere);
  178.         arciere.setBounds(12, 12, 96, 131);
  179.  
  180.         freccia.setIcon(new javax.swing.ImageIcon(getClass().getResource("/arcier/graphics/images/arrow.png"))); // NOI18N
  181.         freccia.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
  182.         jPanel1.add(freccia);
  183.         freccia.setBounds(12, 181, 75, 7);
  184.  
  185.         win.setIcon(new javax.swing.ImageIcon(getClass().getResource("/arcier/graphics/images/win.png"))); // NOI18N
  186.         win.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
  187.         jPanel1.add(win);
  188.         win.setBounds(280, 190, 310, 270);
  189.  
  190.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  191.         getContentPane().setLayout(layout);
  192.         layout.setHorizontalGroup(
  193.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  194.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 793, Short.MAX_VALUE)
  195.         );
  196.         layout.setVerticalGroup(
  197.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  198.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
  199.         );
  200.  
  201.         pack();
  202.     }// </editor-fold>//GEN-END:initComponents
  203.  
  204.     /**
  205.      * @param args the command line arguments
  206.      */
  207.     public static void main(String args[]) {
  208.         java.awt.EventQueue.invokeLater(new Runnable() {
  209.  
  210.             public void run() {
  211.                 Arcier a=new Arcier();
  212.                 Dimension screen= Toolkit.getDefaultToolkit().getScreenSize();
  213.                 int x=(screen.width-a.getSize().width)/2;
  214.                 int y=(screen.height-a.getSize().height)/2;
  215.                 a.setLocation(x, y);
  216.                 a.setVisible(true);
  217.             }
  218.         });
  219.     }
  220.     // Variables declaration - do not modify//GEN-BEGIN:variables
  221.     private javax.swing.JLabel arciere;
  222.     private javax.swing.JLabel freccia;
  223.     private javax.swing.JPanel jPanel1;
  224.     private javax.swing.JLabel win;
  225.     // End of variables declaration//GEN-END:variables
  226. }