Skip to content
Snippets Groups Projects
Commit ed11c01f authored by MANSOUR Chadi's avatar MANSOUR Chadi
Browse files

exercice 1 and a bit of the 2

parent d81c9d6d
No related branches found
No related tags found
No related merge requests found
......@@ -14,12 +14,12 @@ public abstract class AbstractShape implements Shape {
public void addPoints(Point2D... points){
this.points.addAll(Arrays.asList(points));
}
@Override
public int pointsCount() {
return points.size();
}
@Override
public Point2D point(int index) {
if( index >= 0 && index < points.size()){
return points.get(index);
......@@ -28,6 +28,5 @@ public abstract class AbstractShape implements Shape {
}
}
@Override
public abstract void draw(GraphicsContext context) ;
}
......@@ -13,30 +13,35 @@ import javafx.stage.Stage;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Drawer drawer = new Drawer(400, 400);
Circle circle = new Circle(200, 200, 50);
Rectangle rectangle = new Rectangle(100, 100, 150, 100);
drawer.add(circle);
drawer.add(rectangle);
drawer.repaint();
drawer.getCanvas().setOnMouseClicked(event -> {
double mouseX = event.getX();
double mouseY = event.getY();
Shape clickedShape = drawer.shapeContaining(mouseX, mouseY);
if (clickedShape != null) {
System.out.println("Shape clicked: " + clickedShape);
}
});
Group root = new Group();
Canvas canvas = new Canvas(300, 300);
GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
ShapeContainer shapeContainer = new ShapeContainer();
Shape rectangle = new Rectangle(Color.BLUE, new Point2D(50, 50), new Point2D(150, 100));
rectangle = new BorderDecorator(rectangle, 10);
rectangle = new CenterDecorator(rectangle, 5);
shapeContainer.addShape(rectangle);
Shape polygone = new Polygon(Color.YELLOW, new Point2D(210,100),new Point2D(250,100),new Point2D(150,160), new Point2D(90,100));
polygone = new BorderDecorator(polygone, 15);
polygone = new CenterDecorator(polygone, 5);
shapeContainer.addShape(polygone);
shapeContainer.draw(graphicsContext);
root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root));
root.getChildren().add(drawer.getCanvas());
Scene scene = new Scene(root, 400, 400);
primaryStage.setTitle("JavaFX Drawer");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
\ No newline at end of file
......@@ -6,25 +6,18 @@ import javafx.scene.paint.Color;
public class BorderDecorator extends Decorator {
private final double radius;
@Override
public void paint(GraphicsContext graphicsContext) {
public BorderDecorator(Shape decoratedShape ,double radius) {
super(decoratedShape);
this.radius = radius;
}
protected void drawDecoration(GraphicsContext context) {
context.setStroke(Color.BLACK);
context.setLineWidth(2);
if (decoratedshape.pointsCount() >= 2){
Point2D point0 = decoratedshape.point(0);
Point2D point1 = decoratedshape.point(1);
@Override
public boolean contains(double x, double y) {
return false;
}
@Override
public void translate(double dx, double dy) {
double x = Math.min(point0.getX(), point1.getX());
double y = Math.min(point0.getY(), point1.getY());
double width = Math.abs(point1.getX() - point0.getX());
double height = Math.abs(point1.getY() - point0.getY());
context.strokeRoundRect(x,y, width,height,radius, radius);
}
}
}
......@@ -5,22 +5,19 @@ import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class CenterDecorator extends Decorator{
private final double radius;
public CenterDecorator(Shape decoratedShape, double radius) {
super(decoratedShape);
this.radius = radius;
@Override
public void paint(GraphicsContext graphicsContext) {
}
protected void drawDecoration(GraphicsContext context) {
if (decoratedshape.pointsCount() >= 2) {
Point2D point0 = decoratedshape.point(0);
Point2D point1 = decoratedshape.point(1);
@Override
public boolean contains(double x, double y) {
return false;
}
double centerX = point0.getX() + point1.getX() / 2;
double centerY = point0.getY() + point1.getY() / 2;
@Override
public void translate(double dx, double dy) {
context.setFill(Color.RED);
context.strokeOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
}
}
}
......@@ -5,21 +5,31 @@ import javafx.scene.canvas.GraphicsContext;
public class Circle implements Shape {
public Circle(double x, double y, double sqrt) {
private double radius, x, y;
public Circle(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public int pointsCount() {
return 0;
public void paint(GraphicsContext graphicsContext) {
graphicsContext.strokeOval(x-radius, y-radius, 2 * radius, 2 * radius);
}
@Override
public Point2D point(int index) {
return null;
public boolean contains(double x, double y) {
double dx = x -= this.x;
double dy = y -= this.y;
return dx*dx + dy*dy <= radius*radius;
}
@Override
public void draw(GraphicsContext context) {
public void translate(double dx, double dy) {
this.x += dx;
this.y += dy;
}
}
......@@ -4,24 +4,5 @@ import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public abstract class Decorator implements Shape {
protected final Shape decoratedshape;
public Decorator(Shape decoratedShape) {
this.decoratedshape = decoratedShape;
}
@Override
public int pointsCount() {
return decoratedshape.pointsCount();
}
@Override
public Point2D point(int index) {
return decoratedshape.point(index);
}
@Override
public void draw(GraphicsContext context) {
decoratedshape.draw(context);
drawDecoration(context);
}
protected abstract void drawDecoration(GraphicsContext context) ;
}
package shape;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseButton;
import java.util.ArrayList;
import java.util.List;
public class Drawer {
private List<Shape> shapes;
private double width;
private double height;
private Canvas canvas;
private GraphicsContext gc;
private Rectangle tempRectangle = null;
public Drawer(double width, double height) {
this.width = width;
this.height = height;
shapes = new ArrayList<>();
canvas = new Canvas(width,height);
gc = canvas.getGraphicsContext2D();
setupMouseHandlers();
}
public void add(Shape shape) {
shapes.add(shape);
}
public void repaint(){
gc.clearRect(0, 0, width, height);
for(Shape shape : shapes){
shape.paint(gc);
}
if (tempRectangle != null){
tempRectangle.paint(gc);
}
}
private void setupMouseHandlers(){
canvas.setOnMousePressed(event -> {
if(event.getButton() == MouseButton.PRIMARY){
double x = event.getX();
double y = event.getY();
tempRectangle = new Rectangle(x, y, 0, 0);
}
});
canvas.setOnMouseReleased(event -> {
if(event.getButton() == MouseButton.PRIMARY){
shapes.add(tempRectangle);
tempRectangle = null;
repaint();
}
});
}
public Shape shapeContaining(double x, double y){
for(Shape shape : shapes){
if (shape.contains(x, y)){
return shape;
}
}
return null;
}
public Canvas getCanvas() {
return canvas;
}
}
......@@ -26,4 +26,19 @@ public class Polygon extends AbstractShape {
context.stroke();
}
@Override
public void paint(GraphicsContext graphicsContext) {
}
@Override
public boolean contains(double x, double y) {
return false;
}
@Override
public void translate(double dx, double dy) {
}
}
......@@ -4,26 +4,35 @@ import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Rectangle extends AbstractShape{
private final Color color;
public class Rectangle implements Shape{
public Rectangle(Color color, Point2D point0, Point2D point1){
this.color = color;
addPoints(point0, point1);
private double x;
private double y;
private double width;
private double height;
public Rectangle(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public void draw(GraphicsContext context) {
context.setStroke(color);
public void paint(GraphicsContext graphicsContext) {
graphicsContext.strokeRect(x, y, width, height);
Point2D point0 = points.get(0);
Point2D point1 = points.get(1);
}
double x = Math.min(point0.getX(), point1.getX());
double y = Math.min(point0.getY(), point1.getY());
double width = Math.abs(point1.getX() - point0.getX());
double height = Math.abs(point1.getY() - point0.getY());
@Override
public boolean contains(double x, double y) {
return x>= this.x && x<= this.x + this.width
&& y>= this.y && y<= this.y + this.height;
}
context.strokeRect(x,y,width,height);
@Override
public void translate(double dx, double dy) {
x += dx;
y += dy;
}
}
......@@ -4,7 +4,7 @@ import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public interface Shape {
int pointsCount();
Point2D point(int index);
void draw(GraphicsContext context);
void paint(GraphicsContext graphicsContext);
boolean contains(double x, double y);
void translate(double dx, double dy);
}
......@@ -14,7 +14,5 @@ public class ShapeContainer{
}
public void draw(GraphicsContext context){
for(Shape shape : shapes)
shape.draw(context);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment