next up previous
Next: Exercice 2 Up: No Title Previous: Exercice 3 : Calcul

Exercice 1

1.
Elle affiche un carré de taille 10,10 aux coordonnées de la souris quand on appuie sur un bouton. Les carrés peuvent se chevaucher.
2.
Fichier : PointColore.java
import java.awt.Graphics;   
import java.awt.Color;   

public class PointColore extends Point1 {  

  public Color color = Color.yellow;

  public PointColore (int x, int y, Color c) {  
    super(x, y);             
    color = c;
  }
  public PointColore (int x, int y) {  
    super(x, y); 
  }

  public void show (Graphics g) {
    g.setColor(color);
    g.fillRect(x, y, 10, 10);
  }    

}

3.
Fichier : Point3test.java
import java.applet.*;       
import java.awt.*; 
import java.awt.event.*;

public class Point3test extends Point2test {   

  public Color color = Color.blue;
  Choice color_choice = new Choice();
       
  class AdaptateurSouris extends MouseAdapter implements MouseListener {
      public void mousePressed (MouseEvent e)
      {
        int x = e.getX();
        int y = e.getY();
        PointColore pt = new PointColore(x, y, color);
        System.out.println("Prem mouse adapter");
        pt.show(Point3test.this.getGraphics());
        pt.display();
      }
   }

  class AdaptateurItem  implements ItemListener {

     public void itemStateChanged(ItemEvent e) {
       System.out.println("Modification d'Item");
       if (e.getSource() == color_choice) { 
         System.out.println(color_choice.getSelectedItem());
         if ( color_choice.getSelectedItem().equals("Rouge") ) {
           color = Color.red;
         } else if ( color_choice.getSelectedItem().equals("Vert") ) {
         color = Color.green;
         }
      }
    }
  }

  public void init_evenement() {
    MouseListener clic = new AdaptateurSouris();
    ItemListener sel = new AdaptateurItem();
    System.out.println("init ev P3");
    addMouseListener(clic);
    color_choice.addItemListener(sel);
     
  }
    
  public void init () { 
    super.init();
    color_choice.add("Rouge");
    color_choice.add("Vert"); 
    this.add(color_choice);
    init_evenement();
  }
       
}

4.
Tous les cas ne sont pas traités : il faut que les coordonnées du relachement de la souris soient > aux coordonnées initiales. De plus l'objet rectangle ne ocnserve pas la oculeur.
Fichier : Point4test.java
import java.applet.*;       
import java.awt.*; 
import java.awt.event.*;


public class Point4test extends Point3test {   

  int last_x, last_y;
  public static final int sensitivity = 5;
  
  class AdaptateurSouris extends MouseAdapter implements MouseListener {

    public void mousePressed (MouseEvent e)
    {
      System.out.println("mouse Pressed");
      last_x = e.getX();
      last_y = e.getY();              
    }

    public void mouseReleased (MouseEvent e) {
      int x = e.getX();
      int y = e.getY();
      Graphics g = Point4test.this.getGraphics(); 
      System.out.println("mouse Released");        
      if (    Math.abs(last_x - x) < sensitivity 
         && Math.abs(last_y - y) < sensitivity ) {
        PointColore pt = new PointColore(x, y, color);  
        pt.show(g);
      } else {
        Rectangle r = new Rectangle(last_x, last_y, x-last_x, y-last_y);  
        g.setColor(color);
        g.fillRect(r.x,r.y,r.width,r.height);
      }
    }
  } 


  public void init_evenement() {
    MouseListener clic = new AdaptateurSouris();
    ItemListener sel = new Point3test.AdaptateurItem();
    System.out.println("init ev de P4");
    addMouseListener(clic);
    color_choice.addItemListener(sel);
     
  }

    
}



Emmanuel CHAILLOUX
12/7/1997