JPalle - Palla.java
Cerca
 











Palla.java

Caricato da: Paoloricciuti
Scarica il programma completo

  1. package frame;
  2.  
  3. import java.awt.Graphics;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import javax.swing.JLabel;
  7.  
  8. public class Palla extends JLabel implements Runnable{
  9.  
  10.     private Angle angolo;
  11.     private int velocita;
  12.     private Thread start;
  13.  
  14.     public void start(){
  15.         this.start= new Thread(this);
  16.         this.start.start();
  17.     }
  18.  
  19.     public void stop(){
  20.         this.start.stop();
  21.     }
  22.  
  23.     public Palla(){
  24.         this(new Angle(), 5);
  25.     }
  26.  
  27.     public Palla(Angle pAngolo){
  28.         this(pAngolo, 5);
  29.     }
  30.  
  31.     public Palla(Angle pAngolo, int pVelocità){
  32.         super();
  33.         super.setBounds(185, 185, 30, 30);
  34.         this.angolo= pAngolo;
  35.         this.velocita=pVelocità;
  36.     }
  37.  
  38.     @Override
  39.     protected void paintComponent(Graphics g) {
  40.         g.fillOval(0, 0, 30, 30);
  41.         super.paintComponent(g);
  42.     }
  43.  
  44.  
  45.     public void run() {
  46.        int x=0;
  47.        int y=0;
  48.         while(true){
  49.             x=(int) (this.angolo.getX()*this.velocita);
  50.             y=(int) (this.angolo.getY()*this.velocita);
  51.             this.setLocation(this.getLocation().x+x, this.getLocation().y-y);
  52.             if(this.getLocation().x<=0 || this.getLocation().x>=370){
  53.                 this.angolo.setAngolo(180-this.angolo.getAlfa());
  54.                 while(this.getLocation().x<=0 || this.getLocation().x>=370){
  55.                     x=(int) (this.angolo.getX()*this.velocita);
  56.                     y=(int) (this.angolo.getY()*this.velocita);
  57.                     this.setLocation(this.getLocation().x+x, this.getLocation().y-y);
  58.                 }
  59.             }
  60.             if(this.getLocation().y<=0 || this.getLocation().y>=340){
  61.                 this.angolo.setAngolo(-this.angolo.getAlfa());
  62.                 while(this.getLocation().y<=0 || this.getLocation().y>=340){
  63.                     x=(int) (this.angolo.getX()*this.velocita);
  64.                     y=(int) (this.angolo.getY()*this.velocita);
  65.                     this.setLocation(this.getLocation().x+x, this.getLocation().y-y);
  66.                 }
  67.             }
  68.             try {
  69.                 Thread.sleep(10);
  70.             } catch (InterruptedException ex) {
  71.             }
  72.         }
  73.     }
  74.  
  75.  
  76. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.