Ajouter un commentaire

perluce
problème java.awt.Robot

Bonjour,

j'utilise la classe Robot pour me déplacer automatiquement sur le panel. Mais j'aimerais qu'au clic de l'utilisateur, un point soit créé.

J'ai un thread qui exécute le code du robot. Et j'appelle ce thread dans mon MouseClicked, juste avant que l'utilisateur puisse cliquer pour sélectionner son point. Seulement, le clic, ne fait rien, le robot continue à se déplacer automatiquement. Est ce que quelqu'un saurait comment je peux régler ce problème?

Voici mon code:

thread implémentant le robot

import java.awt.Robot;

public class FirstThread extends Thread {

	PanelParallèle p;
	boolean actif;
	 
    
    public FirstThread(PanelParallèle p) {
       this.p =p;
       this.actif=false;
       
    }

    public void run() {
    	actif=true;
    	
    	while(actif==true){
        	
    		try{
      			Robot r= new Robot();
      			for (int i = 0; i < 300; i++)
      	        {
      	           for (int j = 0; j < 300; j++)
      	           {
      	              if ((j - (p.coefDir2 * i) - (p.absOr2 * 20)) < 0.5f && (j - (p.coefDir2 * i) - (p.absOr2 * 20)) > -0.5f)
      	              r.mouseMove((int)(i+p.getLocationOnScreen().getX()),(int)(j+p.getLocationOnScreen().getY()));
      	           }
      	        }
      			//Thread.yield();
      		}
      		 catch(Exception ex){
      			 System.out.println("Exception :"+ex.getMessage());
      		 }
      		    }
    }
   
    public void setActif(boolean a){
    	actif=a; 	
    }
}

méthode mouseClicked qui exécute le thread

   public void mouseClicked(MouseEvent e)

   {
	Graphics g = getGraphics();
 if(etape==5){
    	  System.out.println("dans 6ème étape");
		  thr1 = new FirstThread(this);	
		  //SwingUtilities.invokeLater(thr1);
		  thr1.start(); 
		  System.out.println("fin thread");
      }
      
      if(etape==6){
    	  System.out.println("dernière étape");
  			thr1.setActif(false);
  			System.out.println(thr1.actif);
  			thr1=null;
  		
  			p4 = new MonPoint(e.getX(),e.getY());
  			   	  
  			System.out.println("p4 x" +p4.x);
  			System.out.println("p4 y" +p4.y);
  			   		p4.affiche(g);
  			   		li.add(p4);
  			   		forme.add(p4);
  			   	
  			   		Ligne l2 = new Ligne(li.size());
  			   		MonPoint[] tab = new MonPoint[li.size()];

  			   		for (int k=0; k<li.size(); k++){
  			   		tab[k]=li.get(k);
  			   		l2.points=tab;
  			   		}
  			   		l2.affiche(g);
  			   		forme.add(l2);	
  			}
      etape++;
	 }

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 PPPP   M   M  M   M  N   N   CCC 
P P MM MM MM MM NN N C
PPPP M M M M M M N N N C
P M M M M N NN C
P M M M M N N CCC