Skip to content
Snippets Groups Projects
Commit 570ea93e authored by CHAHINE Rami's avatar CHAHINE Rami
Browse files

TP 5

parent c195287b
No related branches found
No related tags found
No related merge requests found
Pipeline #34677 failed
Showing
with 562 additions and 0 deletions
package serializer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
DrawerWithSave container = new DrawerWithSave(800, 600);
root.getChildren().add(container);
primaryStage.setScene(new Scene(root));
primaryStage.show();
Button save = new Button("save");
root.getChildren().add(save);
save.setLayoutX(0);
save.setLayoutY(0);
root.setOnKeyPressed(event->container.context.keyPressed(event));
//save.setOnKeyPressed(event->container.context.keyPressed(event));
save.setOnAction(event -> container.write());
}
}
\ No newline at end of file
package serializer;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Alert;
import javafx.stage.FileChooser;
import shape.Shape;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
public class DrawerWithSave extends state.Drawer {
public DrawerWithSave(int width, int height) {
super(width, height);
}
void write(){
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save");
File file = fileChooser.showSaveDialog(getScene().getWindow());
if (file == null) return;
try {
ShapeWriter.write(file, super.shapes);
}
catch (IOException e) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Error Dialog");
alert.setHeaderText(null);
alert.setContentText("Ooops, there was an error!");
alert.showAndWait();
}
}
public void load() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Load");
File file = fileChooser.showOpenDialog(getScene().getWindow());
if (file == null) return;
try {
super.shapes = ShapeReader.read(file);
repaint();
} catch (IOException e) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Error Dialog");
alert.setHeaderText(null);
alert.setContentText("Ooops, there was an error!");
alert.showAndWait();
}
}
}
package serializer;
import shape.Shape;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collector;
public class ShapeReader {
public static List<Shape> read(File file) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
/*for(String line : bufferedReader.lines().toList()) {
System.out.println(line);
}*/
return null;
}
}
package serializer;
import shape.Shape;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
public class ShapeWriter {
public static void write (File file, List<Shape> shapes) throws IOException {
PrintWriter printWriter = new PrintWriter(file);
printWriter.println("Coucou");
}
}
package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import java.util.List;
public abstract class Abstractshape implements Shape {
private List<Point2D> points;
public Abstractshape(List<Point2D> points) {
this.points = points;
}
public void addPoints(Point2D... points){
for (Point2D point: points)
this.points.add(point);
}
@Override
public int pointsCount() {
return points.size();
}
@Override
public Point2D point(int index) {
return index < points.size() ? points.get(index) : null;
}
@Override
public void draw(GraphicsContext context) {
if (pointsCount()>0){
context.beginPath();
context.moveTo(point(0).getX(), point(0).getY());
for (int i = 1; i<pointsCount(); i++){
context.lineTo(point(i).getX(),point(i).getY());
}
context.closePath();
context.stroke();
}
}
}
\ No newline at end of file
package shape;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Canvas canvas = new Canvas(130, 110);
GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
ShapeContainer shapeContainer = new ShapeContainer();
graphicsContext.setFill(Color.AQUAMARINE);
graphicsContext.fillOval(10,10,10,10);
shapeContainer.addShape(new Rectangle(Color.BLUE,new Point2D(10,10), new Point2D(40,40)));
shapeContainer.draw(graphicsContext);
root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
\ No newline at end of file
package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class BorderDecorator extends Decorator {
private double radius;
public BorderDecorator(Shape decoratedShape ,double radius) {
super(decoratedShape);
this.radius = radius;
}
protected void drawDecoration(GraphicsContext context) {
context.setStroke(Color.BLACK);
context.setLineWidth(2);
for (int i=0; i<pointsCount(); i++){
Point2D point = point(i);
context.strokeOval(point.getX() - radius, point.getY()- radius,radius*2,radius*2);
}
}
}
package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class CenterDecorator extends Decorator{
private double radius;
public CenterDecorator(Shape decoratedShape, double radius) {
super(decoratedShape);
this.radius = radius;
}
protected void drawDecoration(GraphicsContext context) {
double centerX = 0;
double centerY = 0;
for (int i =0; i<pointsCount(); i++){
Point2D point = point(i);
centerX += point.getX();
centerY += point.getY();
}
centerX /= pointsCount();
centerY /= pointsCount();
context.setStroke(Color.RED);
context.setLineWidth(2);
context.strokeOval(centerX-radius, centerY-radius, radius*2,radius*2);
}
}
\ No newline at end of file
package shape;
import javafx.scene.canvas.GraphicsContext;
public class Circle implements Shape {
private double x, y, radius;
public Circle(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public void paint(GraphicsContext gc) {
gc.strokeOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
@Override
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 translate(double dx, double dy) {
this.x += dx;
this.y += dy;
}
}
package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public abstract class Decorator implements Shape {
protected 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 void drawDecoration(GraphicsContext context) {
}
}
\ No newline at end of file
package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.List;
public class Polygon extends Abstractshape {
private Color color;
public Polygon(Color color, Point2D... points) {
super(List.of(points));
this.color = color;
}
@Override
public int pointsCount() {
return super.pointsCount();
}
@Override
public Point2D point(int index) {
return super.point(index);
}
@Override
public void draw(GraphicsContext context) {
context.setFill(color);
super.draw(context);
context.fill();
}
}
\ No newline at end of file
package shape;
import javafx.scene.canvas.GraphicsContext;
public class Rectangle implements Shape {
private double x, y, width, 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 paint(GraphicsContext gc) {
gc.strokeRect(x, y, width, height);
}
@Override
public boolean contains(double x, double y) {
return x >= this.x && x <= this.x + width && y >= this.y && y <= this.y + height;
}
@Override
public void translate(double dx, double dy) {
this.x += dx;
this.y += dy;
}
}
package shape;
import javafx.scene.canvas.GraphicsContext;
public interface Shape {
void paint(GraphicsContext gc);
boolean contains(double x, double y);
void translate(double dx, double dy);
}
package shape;
import javafx.scene.canvas.GraphicsContext;
import java.util.ArrayList;
import java.util.List;
public class ShapeContainer{
private List<Shape> shapes = new ArrayList<>();
public void addShape(Shape shape){
shapes.add(shape);
}
public void draw(GraphicsContext context){
for(Shape shape : shapes)
shape.draw(context);
}
}
\ No newline at end of file
package state;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Drawer container = new Drawer(800, 600);
root.getChildren().add(container);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
\ No newline at end of file
public class CircleDrawerState0 implements DrawerState {
@Override
public void mousePressed(DrawerContext context, double x, double y) {
context.setState(new CircleDrawerState1(x, y));
}
@Override
public void mouseReleased(DrawerContext context, double x, double y) {}
@Override
public void mouseMoved(DrawerContext context, double x, double y) {}
@Override
public void paint(GraphicsContext gc) {}
}
public class CircleDrawerState1 implements DrawerState {
private double x, y;
public CircleDrawerState1(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public void mousePressed(DrawerContext context, double x, double y) {}
@Override
public void mouseReleased(DrawerContext context, double x, double y) {
double radius = Math.hypot(this.x - x, this.y - y);
context.getDrawer().add(new Circle(this.x, this.y, radius));
context.setState(new CircleDrawerState0()); // Reset to initial state
}
@Override
public void mouseMoved(DrawerContext context, double x, double y) {}
@Override
public void paint(GraphicsContext gc) {}
}
\ No newline at end of file
package state;
import javafx.scene.canvas.GraphicsContext;
import java.util.ArrayList;
import java.util.List;
public class Drawer {
private List<Shape> shapes;
private double width, height;
public Drawer(double width, double height) {
this.width = width;
this.height = height;
this.shapes = new ArrayList<>();
}
public void add(Shape shape) {
shapes.add(shape);
}
public void repaint(GraphicsContext gc) {
gc.clearRect(0, 0, width, height); // Clear canvas
for (Shape shape : shapes) {
shape.paint(gc);
}
}
public Shape shapeContaining(double x, double y) {
for (Shape shape : shapes) {
if (shape.contains(x, y)) {
return shape;
}
}
return null;
}
}
package state;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
public class DrawerContext {
private Drawer drawer;
private DrawerState currentState;
public DrawerContext(Drawer drawer) {
this.drawer = drawer;
this.currentState = new NullDrawerState(); // Default state
}
public void setState(DrawerState state) {
this.currentState = state;
}
public void paint(GraphicsContext gc) {
currentState.paint(gc);
}
public void mousePressed(MouseEvent event) {
currentState.mousePressed(this, event.getX(), event.getY());
}
public void mouseReleased(MouseEvent event) {
currentState.mouseReleased(this, event.getX(), event.getY());
}
public void mouseMoved(MouseEvent event) {
currentState.mouseMoved(this, event.getX(), event.getY());
}
public void keyPressed(KeyEvent event) {
switch (event.getText()) {
case "r":
setState(new RectangleDrawerState0());
break;
case "c":
setState(new CircleDrawerState0());
break;
case "m":
Shape shape = getDrawer().shapeContaining(lastMouseX, lastMouseY);
if (shape != null) {
setState(new MoveShapeState(shape, lastMouseX, lastMouseY));
}
break;
}
}
public Drawer getDrawer() {
return drawer;
}
}
package state;
interface DrawerState {
void mousePressed(DrawerContext context, double x, double y);
void mouseReleased(DrawerContext context, double x, double y);
void mouseMoved(DrawerContext context, double x, double y);
void paint(GraphicsContext gc);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment