next up previous contents
suivant: Particularités de Java monter: Introduction à la programmation précédent: pile en Java   Table des matières

Figures et dessins

Cet autre exemple sert à introduire le coeur de la programmation objet. Il utilise la notion d'héritage et de liaison dynamique.



transparent
Point, Figures


package figure;
public class Point {
  public int x=0;
  public int y=0;
  public Point(int x, int y){
    this.x=x; this.y=y;
  }
}


package figure;
public abstract class Figure{
  public abstract double aire();
  public abstract double périmètre();
}


transparent
Rectangle...


package figure;
public class Rectangle extends Figure{
  // variables d'instance

  protected int base = 0;
  protected int hauteur = 0;
  protected Point origine;

  // constructeurs
  public Rectangle(){
    origine = new Point(0,0);
  }
  public Rectangle(int b, int h){
    this(new Point(0,0),b,h);
  }
  //...


transparent
Rectangle...


  // Rectangle (suite)
  public Rectangle(Point p, int b, int h){
    origine=p;
    base=b;
    hauteur=h;
  }
  // méthodes d'instance
  public void move(int x, int y){
    origine.x=x;
    origine.y=y;
  }
  // remplace la méthode
  public double aire(){
    return base*hauteur;
  } // ...


transparent
Rectangle ... suite


  // rectangle suite
  public double périmètre(){
    return 2*base*hauteur;
  }
  // méthode de classe
  static Rectangle maxAire(Rectangle a, Rectangle b){
    if (a.aire() > b.aire()) return a; else  return b;
  }
  // pour l'encapsulation
  public int getHauteur() {
    return hauteur;
  }
  public void setHauteur(int h) {
    hauteur=h;
  }
}


transparent
Cercle


package figure;
public class Cercle extends Figure {
  Point centre;
  int rayon;
  static double PI=3.1415;
  public double aire(){
    return PI*rayon*rayon;
  }
  public double périmètre(){
    return 2*PI*rayon;
  }
  // ...
}


transparent
Dessinable


import java.awt.*;
public interface Dessinable {
  public void setCouleur(Color c);
  public void dessiner();
  // ...
}
import java.awt.*;
import figure.Cercle;
import figure.Point;
class GraphicCercle extends Cercle
  implements Dessinable{
     public void dessiner(){ // ...
  }
  public void setCouleur(Color c){ // ...
  }
  // ...
}


transparent
GraphicRectangle...


import java.awt.*;
import figure.Rectangle;
import figure.Point;
public class GraphicRectangle extends Rectangle
  implements Dessinable{
  Color couleur_intérieur;
  public GraphicRectangle(int b, int h){
    super(b,h);
  }
  public GraphicRectangle(Point p,int b, int h){
    super(p,b,h);
  }
  public void dessiner(){// ...
  }
  public void setCouleur(Color c){ couleur_intérieur=c;}
  // ...
}


transparent
utilisation...


class Exemple{
 public static void main(String[] args){
  double aire_totale=0;
  Dessinable[] fig= new Dessinable[3];
  GraphicRectangle r1= new 
    GraphicRectangle(4,6);
  GraphicCercle c1= new GraphicCercle();
  GraphicRectangle r2= new
    GraphicRectangle(new Point(2,3),7,8);
  fig[0]=r1; fig[1]=c1; fig[2]=r2;
  for(int i=0;i<fig.length;i++){
   aire_totale += ((Figure)(fig[i])).aire();
   fig[i].dessiner();
  }
 }
}




Hugues Fauconnier 2002-01-11