Cours de Programmation avancée


TP 3 : Adapter

Tâche 1 : Complétez la classe FrenchPainter dans le package french qui implémente l’interface Painter.

package french;

import javafx.scene.canvas.GraphicsContext;
import viewer.Painter;

public class FrenchPainter implements Painter {
  GraphicsContext graphicsContext;
  public FrenchPainter(GraphicsContext graphicsContext) {
    this.graphicsContext = graphicsContext;
  }
  public void drawRectangle(double xUpperLeft, double yUpperLeft, double width, double height) {
    graphicsContext.strokeRect(xUpperLeft, yUpperLeft, width, height);
  }
  public void drawCircle(double xCenter, double yCenter, double radius) {
    graphicsContext.strokeOval(xCenter-radius,yCenter-radius,2*radius,2*radius);
  }
}

Tâche 2 : Proposez une implémentation de la classe EnglishPainterAdapter qui ne demande aucune modification du code existant.

package english;

import viewer.Painter;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;

public class EnglishPainterAdapter implements Painter {
  EnglishPainter englishPainter;

  public EnglishPainterAdapter(GraphicsContext graphicsContext) {
    englishPainter = new EnglishPainter(graphicsContext);
  }

  @Override
  public void drawRectangle(double x, double y, double w, double h) {
    englishPainter.paintRectangle(new Point2D(x,y),new Point2D(x+w,y+h));
  }

  @Override
  public void drawCircle(double x, double y, double radius) {
    englishPainter.paintCircle(new Point2D(x,y), new Point2D(x+radius,y));
  }
}