Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    591

    (principiante) problemi con ActionListener

    Buon giorno, Da pochi giorni sto affrontando la programmazione in Java e sto iniziando a provare piccoli programmi scritti (male) da me.
    Ora , ho realizzato un piccolo programmino ma non mi funziona come mi aspetto e non riesco a capire dove sbaglio, mi riesce a correggere e aspiegare l'errore qcuno??
    Dovrebbe funzionare così: viene disegnata una finestra e tramite le frecce è possibile spostarla sul video ed imposta un parametro direzione, con i tasti + e - si agisce su un parametro passo , cliccando su start la maschera si deve spostare autonomamente con il passo e la velocità impostata, in movimento con i tasti di cui sopra (frecce +-) si variano i parametri e quindi la maschera varia il movimento. Stop ferma il movimento autonomo della maschera.
    Il primo problema è che dopo aver impostato i parametri di movimento e dato lo start non è più possibile modificarli nè in movimento nè dopo aver cliccato lo stop.

    Questo è il codice:
    codice:
    package test;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import test.*;
    /**
     *
     * @author m
     */
    public class test1 extends JFrame  implements ActionListener,KeyListener {
        JPanel TestPanel,TestPanel2;
        JButton bottone ;
        JButton start,stop;
        Timer timer;
        JLabel lab = new JLabel("TEST");
        private static int i = 1;
        private static int XX=200;
        private int YY=100;
        private static int direzione=0;
        private static int passo=0;
        public test1(String Nome, int x, int    y, int k){
           super(Nome);
           setMyID(k);
           bottone = new JButton("nuova");
           start = new JButton("start");
           stop = new JButton("stop");
    //       this.addKeyListener(new MovementAdapter());
           addWindowListener(new WAdapter());
           bottone.setSize(50,200);
           TestPanel= new JPanel();
           bottone.addActionListener(this);
           start.addActionListener(this);
           stop.addActionListener(this);
           TestPanel.setBackground( Color.green );
           TestPanel.setLayout(new GridLayout( 3, 3 ));
           TestPanel2= new JPanel();
           
           TestPanel2.setBackground( Color.red );
           TestPanel2.setLayout(new GridLayout( 1, 3 ));
           TestPanel.add(bottone,BorderLayout.NORTH);
           TestPanel.add(lab,BorderLayout.CENTER);
           TestPanel2.add(start,BorderLayout.SOUTH);
           TestPanel2.add(stop,BorderLayout.SOUTH);
           TestPanel.setSize(200,200);
          Container c = getContentPane();
          c.add( TestPanel, BorderLayout.CENTER );
          c.add( TestPanel2, BorderLayout.SOUTH );
          this.addKeyListener(this);
          setFocusable(true);
           setLocation(XX,YY);
           setSize(400,500);
           pack();
           show();
        };
        public test1(String Nome){
            super("default");
            setLocation(50,50);
            setSize(200,100);
            show();
        };
        public void start() {
        if (timer == null) {
            timer = new Timer(100,this);
            timer.start();
        } else timer.start();
       }
      public int getPasso(){
          return this.passo;
      }
      public int getDirezione(){
          return this.direzione;
      }
      public int getMyID(){
          return this.i;
      }
      public void setMyID(int ID){
          this.i=ID;
      }
      public void setPasso(int p){
          if (p>=0)   this.passo=p;
      }
      public void setDirezione(int d){
          if (d>0 && d<5) this.direzione=d;
      }
      public void stop() {
         if (timer != null) {
           timer.stop();
         }
       }
       public void actionPerformed(ActionEvent e) {
           if (e.getSource()==stop){
               System.out.println("stop");
               stop();
           } else
           if (e.getSource()==start){
               start();
               System.out.println("start ");
           } else 
           if (e.getSource()==bottone){
                  int i = this.getMyID();
                  System.out.println("bottone "+i);
    //              timed w = new timed(i);
                  
    //                w = new test1("Finestra "+i,10+(i*10), 10+(i*10));
                  
           } else {
           int segno=1;
           int go_x=0;
           int go_y=0;
           int x,y;
           x=(int)this.getLocation().getX();
           y=(int)this.getLocation().getY();
           
           int dir=this.getDirezione();
           int pass=this.getPasso();
           System.out.println(">"+x+" , "+y+" , "+pass+" "+dir);
           if (dir==1||dir==2) segno=-1;
           if (dir==2||dir==4) {
               go_x=x+(segno*pass);
               if (go_x>850) go_x=0;
               if (go_x<-5) go_x=850;
               go_y=y;
           }
           else {
               go_y=y+(segno*pass);
               if (go_y>620) go_y=0;
               if (go_y<-5) go_y=620;
               go_x=x;
           }
           this.setLocation(go_x,go_y);
           this.repaint();
           }
     }
    
        /* over-ridden del metodo della classe JPanel per consentire 
    al pannello di ricevere il "focus" della tastiera.
    Senza questo metodo il pannello non sarà in grado di
    intercettare gli eventi della tastiera!!!
    */
        public boolean isFocusable() { 
            return true;
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
           System.out.println("Ho creato la finestra");
           test1 w= new test1("Finestra "+i,10+(i*50), 10+(i*50),i);
    
    };
    
    //    class MovementAdapter extends KeyAdapter {
            /** Handle for the KeyListener interface 
            * key control for movement */
          public void  keyReleased(KeyEvent e){
              
          }
          public void keyTyped(KeyEvent e){
              
          }
            public void keyPressed(KeyEvent ke) {  
                    int kc = ke.getKeyCode();
                    if (kc == ke.VK_DOWN) {     
                            // Down
                            this.setDirezione(3);
                            ke.getComponent().setLocation(ke.getComponent().getX(),ke.getComponent().getY()+50);
                            ke.getComponent().repaint();
                            System.out.println("giu ");
                    } else if (kc == ke.VK_LEFT) {
                            // Left == West
                            this.setDirezione(2);
                            ke.getComponent().setLocation(ke.getComponent().getX() - 50,ke.getComponent().getY());
                            ke.getComponent().repaint();
                              System.out.println("sinistra");
                    } else if (kc == ke.VK_UP) {
                            // up
                            this.setDirezione(1);
                            ke.getComponent().setLocation(ke.getComponent().getX(),ke.getComponent().getY()-50);
                            ke.getComponent().repaint();
                              System.out.println("su");
                    } else if (kc == ke.VK_RIGHT) {
                            // Right
                            this.setDirezione(4);
                            ke.getComponent().setLocation(ke.getComponent().getX() + 50,ke.getComponent().getY());
                            ke.getComponent().repaint();
                              System.out.println("destra");
                    } else if (kc == ke.VK_F1) {
                            int i=this.getMyID();
                            System.out.println(i);
                            if (i==1){
                                System.exit(1);
                            } else {
                                  i--;
                                  hide();
                            }
                    } else if (kc == ke.VK_F2) {
                            int i=this.getMyID();
                            i++;
                            System.out.println(i);
    //                        test1 w = new test1("Finestra "+i,10+(i*10), 10+(i*10));
    //                        timed w = new timed(i);
                  
                   
                    } else if (kc == ke.VK_MINUS) {
                            if (this.getPasso() > 0 ) this.setPasso(this.getPasso()-1);   
                            System.out.println(this.getPasso()+" "+this.getDirezione());
                    } else if (kc == ke.VK_ADD) {
                            if (this.getPasso() < 10 ) this.setPasso(this.getPasso()+1);            
                            System.out.println(this.getPasso()+" "+this.getDirezione());
                    }
            }     
    
     class WAdapter extends WindowAdapter {
                 public void windowClosing( WindowEvent e )
                {
                            System.out.println("window "+1);
                            if (i==1){
                                System.exit(0);
                            } else {
                                  i--;
                                  hide();
                            }
                }
        }
    }
    Grazie a tutti
    stabi

  2. #2
    Non pensi di aver iniziato un po' troppo aggressivamente?? Di solito si parte dalle basi...

    Comunque...diamo un'occhiata a questo codice... :master:
    Si può vincere o perdere, l'importante è vincere o perdere da uomini...

    Neverland - Il mio Forum

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    591
    Si lo so , ma si impara di più se ci si imbatte in qualche difficoltà, poi non te ne dimentichi.
    tieni presente che potrei aver scritto una montagna di minc*iate a partire dal fatto che non so se è corretto usare il timer piuttosto che un thread ( non mi è ancora chiaro come fare ad implementare una cosa simile).

  4. #4
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    591
    Tenete presente che non mi interessa avere il codice esatto( se poi viene anche questo ok), mi interessano di più le spiegazioni del perchè non funziona come mi aspetto ed eventualmente le dritte per implementare la cosa correttamente, per poter provarci io.

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    591
    Ho fatto qualche modifica, ora va meglio ma se dò lo start senza aver generato nessun'altra form con F2 prima per farle collidere non c'è verso di farla fermare, non sente + nulla, nè mouse ne tastiera. Qualche idea per ovviare al problema?
    Ecco il codice modificato:
    codice:
    package test;
    
    import java.awt.*;
    //import java.lang.*;  //non serve, importata di default
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Vector;
    import test.*;
    /**
     *
     * @author m
     */
    public class test2 extends JFrame  implements ActionListener,Runnable {
        JPanel TestPanel,TestPanel2;
        JButton bottone ;
        JButton start,stop;
        JLabel lab = new JLabel("TEST");
        private  int i = 1;
        private  int XX=200;
        private int YY=100;
        private  int direzione=0;
        private  int passo=0;
        private boolean muovo= false;
        static Vector mappa = new Vector();
        Thread me;
        public test2(String Nome, int x, int    y, int k){
           super(Nome);
    
           setMyID(k);
           bottone = new JButton("nuova");
           start = new JButton("start");
           stop = new JButton("stop");
    //       this.addKeyListener(new MovementAdapter());
           addWindowListener(new WAdapter());
           bottone.setSize(50,200);
           TestPanel= new JPanel();
           bottone.addActionListener(this);
           start.addActionListener(this);
           stop.addActionListener(this);
           bottone.addKeyListener(new tasti());
           start.addKeyListener(new tasti());
           stop.addKeyListener(new tasti());
           TestPanel.setBackground( Color.green );
           TestPanel.setLayout(new GridLayout( 3, 3 ));
           TestPanel2= new JPanel();
           
           TestPanel2.setBackground( Color.red );
           TestPanel2.setLayout(new GridLayout( 1, 3 ));
           TestPanel.add(bottone,BorderLayout.NORTH);
           TestPanel.add(lab,BorderLayout.CENTER);
           TestPanel2.add(start,BorderLayout.SOUTH);
           TestPanel2.add(stop,BorderLayout.SOUTH);
           TestPanel.setSize(200,200);
          Container c = getContentPane();
          c.add( TestPanel, BorderLayout.CENTER );
          c.add( TestPanel2, BorderLayout.SOUTH );
     //     c.add(D,BorderLayout.CENTER);
          addKeyListener(new tasti());
          setFocusable(true);
           setLocation(XX,YY);
           setSize(400,500);
           mappa.addElement(this);
           pack();
           show();
           me=new Thread(this);
           me.start();
        };
        public test2(String Nome){
            this(Nome,100,100,1);
        }
        public test2(int k){
            this("Nuova finestra "+k,100,100,k);
        }
      public int getPasso(){
          return this.passo;
      }
      public int getDirezione(){
          return this.direzione;
      }
      public int getMyID(){
          return this.i;
      }
      public void setMyID(int ID){
          this.i=ID;
      }
      public void setPasso(int p){
          if (p>=0)   this.passo=p;
      }
      public void setDirezione(int d){
          if (d>0 && d<5) this.direzione=d;
      }
      public void run(){
          
      }
      public void muovi(){
          while (muovo){
          int segno=1;
          int go_x=0;
          int go_y=0;
          int x,y;
          x=(int)this.getLocation().getX();
          y=(int)this.getLocation().getY();
          int larg = this.getWidth();
          int lung = this.getHeight();
          int dir=this.getDirezione();
          int pass=this.getPasso();
          if (muovo){
           int k = this.getMyID();
           int t = mappa.size();
           
           boolean ok=false;
           test2 prova;
           for (int d=0;d<t;){
               prova=(test2)mappa.get(d);
               int kt = prova.getMyID();
               d++;
               int xt = (int)prova.getLocation().getX();
               int yt = (int)prova.getLocation().getY();
               int largt = prova.getWidth();
               int lungt = prova.getHeight();
               System.out.println(x+", "+(x+larg)+" , "+xt+","+(xt+largt)+" , "+y+", "+(y+lung)+","+yt+", "+(yt+lungt));
               if (kt!=k){
                if ( (x>xt && x<xt+largt && y>yt && y<yt+lungt) || // punto alto a sx
                        (x+larg>xt && x+larg<xt+largt && y>yt && y<yt+lungt) || // punto alto a dx
                        (x>xt && x<xt+largt && y+lung>yt && y+lung<yt+lungt) || // punto basso a sx
                        (x+larg>xt && x+larg<xt+largt && y+lung>yt && y+lung<yt+lungt) // punto basso a dx
                ){
                    this.muovo=false;
                }
               }
           }
         }
         if (muovo){
           System.out.println(">"+x+" , "+y+" , "+pass+" "+dir);
           if ((dir==1) || (dir==2)) segno=-1;
           if ((dir==2) || (dir==4)) {
               go_x=x+(segno*pass);
               if (go_x>880) go_x=0;    
               if (go_x<-5) go_x=880;
               go_y=y;
           }
           else {
               go_y=y+(segno*pass);
               if (go_y>640) go_y=0;
               if (go_y<-5) go_y=640;
               go_x=x;
           }
           this.setLocation(go_x,go_y);
           this.repaint();
          }
          try {
            me.sleep (200);
    
            } catch(Exception e) {}
          
          }
      }
       public void actionPerformed(ActionEvent e) {
        
           if (e.getSource()==stop){
               System.out.println("stop");
               this.muovo=false;
           } else
           if (e.getSource()==start){
               System.out.println("start ");
                if (getDirezione()>0 && getPasso()>0)     {
                   this.muovo=true;
                   muovi();
                }
           } else 
           if (e.getSource()==bottone){
                  int i = this.getMyID();
                  System.out.println("bottone "+i);
    //              timed w = new timed(i);
                  
    //                w = new test1("Finestra "+i,10+(i*10), 10+(i*10));
                  
           } else {
               System.out.println(e.getSource().toString());
           }
           
           
      //    return ;
     }
    
        /* over-ridden del metodo della classe JPanel per consentire 
    al pannello di ricevere il "focus" della tastiera.
    Senza questo metodo il pannello non sarà in grado di
    intercettare gli eventi della tastiera!!!
    */
        public boolean isFocusable() { 
            return true;
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            int i = 1;
           System.out.println("Ho creato la finestra"+1);
           test2 w= new test2("Finestra "+i,10+(i*50), 10+(i*50),i);
    };
    
        class tasti extends KeyAdapter {
            /** Handle for the KeyListener interface 
            * key control for movement */
          public void  keyReleased(KeyEvent e){
              
          }
          public void keyTyped(KeyEvent e){
              
          }
      
            public void keyPressed(KeyEvent ke) {  
                    int kc = ke.getKeyCode();
                    if (kc == ke.VK_DOWN) {     
                            // Down
                              setDirezione(3);
                //            muovi();
                            System.out.println("giu ");
                    } else if (kc == ke.VK_LEFT) {
                            // Left == West
                              setDirezione(2);
                //            muovi();
                              System.out.println("sinistra");
                    } else if (kc == ke.VK_UP) {
                            // up
                              setDirezione(1);
                //            muovi();
                              System.out.println("su");
                    } else if (kc == ke.VK_RIGHT) {
                            // Right
                              setDirezione(4);
                 //           muovi();
                              System.out.println("destra");
                    } else if (kc == ke.VK_F1) {
                            int i=getMyID();
                            //mappa.V.remove(this);
                            mappa.removeElement(this);
                            System.out.println(i);
                            if (i==1){
                                System.exit(1);
                            } else {
                                  i--;
                                  hide();
                            }
                    } else if (kc == ke.VK_F2) {
                            int i=getMyID();
                            i++;
                            System.out.println(i);
    //                        test1 w = new test1("Finestra "+i,10+(i*10), 10+(i*10));
                           test2 w = new test2(i);
           
                  
                   
                    } else if (kc == ke.VK_MINUS) {
                            if (getPasso() > 0 ) setPasso(getPasso()-1);   
                            System.out.println(getPasso()+" "+getDirezione());
                    } else if (kc == ke.VK_ADD) {
                            if (getPasso() < 10 ) setPasso(getPasso()+1);            
                            System.out.println(getPasso()+" "+getDirezione());
                    }
            }     
        }
        class WAdapter extends WindowAdapter {
                 public void windowClosing( WindowEvent e )
                {
                            int i=getMyID();
                            System.out.println("window "+i);
                            if (i==1){
                                System.exit(0);
                            } else {
                                  // i--;
                                  hide();
                            }
                }
        }
       }

  6. #6
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328
    L'unico modo per poter fare in modo di gestire la finestra mentre questa si muove è utilizzare un Thread che gestisca il movimento, lasciando libera la finestra.
    Vedo che tu hai creato un thread prendendo la finestra come parent (che implementa Runnable)... lasciando tra l'altro vuoto il metodo run() che è il corpo principale del thread.


    Io direi che dovresti cominciare con qualcosa di più semplice. Capisco la buona volontà e la fretta di mettere in pratica tante cose, ma così non ne vieni a capo.


    Dovresti per prima cosa leggere qualcosa sui Thread: il loro utilizzo, la loro utilità, la loro implementazione.

    Poi applicarlo al caso specifico.


    Il tutorial della Sun sui Thread è un buon punto di partenza. Lo trovi qui .


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  7. #7
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    591
    Grazie , per il momento mi fermo sull'argomento thread,visto che ho reso funzionante il mio codice, molto probabilmente è migliorabile ma sembra funzionare secondo le mie aspettative.
    Ora passo a sperimentare qcosa d'altro, senz'altro avroò ancora bisogno.

    ecco il codice se può mai interessare qualcuno.
    codice:
    package test;
    
    import java.awt.*;
    //import java.lang.*;  //non serve, importata di default
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Vector;
    import test.*;
    /**
     *
     * @author m
     */
    public class test1 extends JFrame  implements ActionListener {
        JPanel TestPanel,TestPanel2;
        JButton bottone ;
        JButton start,stop;
        JLabel lab = new JLabel("TEST");
        private  int i = 1;
        private  int XX=200;
        private int YY=100;
        private  int direzione=0;
        private  int passo=0;
        private boolean muovo= false;
        static Vector mappa = new Vector();
        Movimento mov;
        public test1(String Nome, int x, int    y, int k){
           super(Nome);
    
           setMyID(k);
           bottone = new JButton("nuova");
           start = new JButton("start");
           stop = new JButton("stop");
    //       this.addKeyListener(new MovementAdapter());
           addWindowListener(new WAdapter());
           bottone.setSize(50,200);
           TestPanel= new JPanel();
           bottone.addActionListener(this);
           start.addActionListener(this);
           stop.addActionListener(this);
           bottone.addKeyListener(new tasti());
           start.addKeyListener(new tasti());
           stop.addKeyListener(new tasti());
           TestPanel.setBackground( Color.green );
           TestPanel.setLayout(new GridLayout( 3, 3 ));
           TestPanel2= new JPanel();
           
           TestPanel2.setBackground( Color.red );
           TestPanel2.setLayout(new GridLayout( 1, 3 ));
           TestPanel.add(bottone,BorderLayout.NORTH);
           TestPanel.add(lab,BorderLayout.CENTER);
           TestPanel2.add(start,BorderLayout.SOUTH);
           TestPanel2.add(stop,BorderLayout.SOUTH);
           TestPanel.setSize(200,200);
          Container c = getContentPane();
          c.add( TestPanel, BorderLayout.CENTER );
          c.add( TestPanel2, BorderLayout.SOUTH );
     //     c.add(D,BorderLayout.CENTER);
          addKeyListener(new tasti());
          setFocusable(true);
           setLocation(XX,YY);
           setSize(400,500);
           mappa.addElement(this);
           pack();
           show();
           mov=new Movimento(this);
        };
        public test1(String Nome){
            this(Nome,100,100,1);
        }
        public test1(int k){
            this("Nuova finestra "+k,100,100,k);
        }
      public int getPasso(){
          return this.passo;
      }
      public int getDirezione(){
          return this.direzione;
      }
      public int getMyID(){
          return this.i;
      }
      public void setMyID(int ID){
          this.i=ID;
      }
      public void setPasso(int p){
          if (p>=0)   this.passo=p;
      }
      public void setDirezione(int d){
          if (d>0 && d<5) this.direzione=d;
      }
     
       public void actionPerformed(ActionEvent e) {
        
           if (e.getSource()==stop){
               System.out.println("stop");
               this.muovo=false;
               System.out.println(mov.getState().toString());
               mov.yield();
           } else
           if (e.getSource()==start){
               System.out.println("start ");
                if (getDirezione()>0 && getPasso()>0)     {
                   this.muovo=true;
               System.out.println(mov.getState().toString());
               if (mov.getState().toString()=="TERMINATED")
                   mov=new Movimento(this);
               if (mov.getState().toString()!="TIMED_WAITING")
                    mov.start();
                }
           } else 
           if (e.getSource()==bottone){
                  int i = this.getMyID();
                  System.out.println("bottone "+i);
    //              timed w = new timed(i);
                  
    //                w = new test1("Finestra "+i,10+(i*10), 10+(i*10));
                  
           } else {
               System.out.println(e.getSource().toString());
           }
           
           
      //    return ;
     }
    
        /* over-ridden del metodo della classe JPanel per consentire 
    al pannello di ricevere il "focus" della tastiera.
    Senza questo metodo il pannello non sarà in grado di
    intercettare gli eventi della tastiera!!!
    */
        public boolean isFocusable() { 
            return true;
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            int i = 1;
           System.out.println("Ho creato la finestra"+1);
           test1 w= new test1("Finestra "+i,10+(i*50), 10+(i*50),i);
    };
    
        class tasti extends KeyAdapter {
            /** Handle for the KeyListener interface 
            * key control for movement */
          public void  keyReleased(KeyEvent e){
              
          }
          public void keyTyped(KeyEvent e){
              
          }
      
            public void keyPressed(KeyEvent ke) {  
                    int kc = ke.getKeyCode();
                    if (kc == ke.VK_DOWN) {     
                            // Down
                              setDirezione(3);
                //            muovi();
                            System.out.println("giu ");
                    } else if (kc == ke.VK_LEFT) {
                            // Left == West
                              setDirezione(2);
                //            muovi();
                              System.out.println("sinistra");
                    } else if (kc == ke.VK_UP) {
                            // up
                              setDirezione(1);
                //            muovi();
                              System.out.println("su");
                    } else if (kc == ke.VK_RIGHT) {
                            // Right
                              setDirezione(4);
                 //           muovi();
                              System.out.println("destra");
                    } else if (kc == ke.VK_F1) {
                            int i=getMyID();
                            //mappa.V.remove(this);
                            mappa.removeElement(this);
                            System.out.println(i);
                            if (i==1){
                                System.exit(1);
                            } else {
                                  i--;
                                  hide();
                            }
                    } else if (kc == ke.VK_F2) {
                            int i=getMyID();
                            i++;
                            System.out.println(i);
    //                        test1 w = new test1("Finestra "+i,10+(i*10), 10+(i*10));
                           test1 w = new test1(i);
           
                  
                   
                    } else if (kc == ke.VK_MINUS) {
                            if (getPasso() > 0 ) setPasso(getPasso()-1);   
                            System.out.println(getPasso()+" "+getDirezione());
                    } else if (kc == ke.VK_ADD) {
                            if (getPasso() < 10 ) setPasso(getPasso()+1);            
                            System.out.println(getPasso()+" "+getDirezione());
                    }
            }     
        }
        class WAdapter extends WindowAdapter {
                 public void windowClosing( WindowEvent e )
                {
                            int i=getMyID();
                            System.out.println("window "+i);
                            if (i==1){
                                System.exit(0);
                            } else {
                                  // i--;
                                  hide();
                            }
                }
        }
        class Movimento extends Thread {
            private test1 o;
            synchronized public void run (){
              while (o.muovo){
          int segno=1;
          int go_x=0;
          int go_y=0;
          int x,y;
          x=(int)o.getLocation().getX();
          y=(int)o.getLocation().getY();
          int larg = o.getWidth();
          int lung = o.getHeight();
          int dir=o.getDirezione();
          int pass=o.getPasso();
          if (o.muovo){
           int k = o.getMyID();
           int t = mappa.size();
           
           boolean ok=false;
           test1 prova;
           for (int d=0;d<t;){
               prova=(test1)mappa.get(d);
               int kt = prova.getMyID();
               d++;
               int xt = (int)prova.getLocation().getX();
               int yt = (int)prova.getLocation().getY();
               int largt = prova.getWidth();
               int lungt = prova.getHeight();
               System.out.println(x+", "+(x+larg)+" , "+xt+","+(xt+largt)+" , "+y+", "+(y+lung)+","+yt+", "+(yt+lungt));
               if (kt!=k){
                if ( (x>xt && x<xt+largt && y>yt && y<yt+lungt) || // punto alto a sx
                        (x+larg>xt && x+larg<xt+largt && y>yt && y<yt+lungt) || // punto alto a dx
                        (x>xt && x<xt+largt && y+lung>yt && y+lung<yt+lungt) || // punto basso a sx
                        (x+larg>xt && x+larg<xt+largt && y+lung>yt && y+lung<yt+lungt) // punto basso a dx
                ){
                    o.muovo=false;
                }
               }
           }
         }
         if (o.muovo){
           System.out.println(">"+x+" , "+y+" , "+pass+" "+dir);
           if ((dir==1) || (dir==2)) segno=-1;
           if ((dir==2) || (dir==4)) {
               go_x=x+(segno*pass);
               if (go_x>880) go_x=0;    
               if (go_x<-5) go_x=880;
               go_y=y;
           }
           else {
               go_y=y+(segno*pass);
               if (go_y>640) go_y=0;
               if (go_y<-5) go_y=640;
               go_x=x;
           }
           o.setLocation(go_x,go_y);
           o.repaint();
          }
          try {
            this.wait(200);
            } catch(Exception e) {}
          
          }
            }
            public  Movimento(test1 f){
                o=f;
                //this.start();
            }
        }
       }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.