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

.

parent 0e769f3c
No related branches found
No related tags found
No related merge requests found
Showing
with 762 additions and 0 deletions
package TP5.serializer;
import javafx.scene.control.Alert;
import javafx.stage.FileChooser;
import java.io.File;
import java.io.IOException;
public class DrawerWithSave extends TP5.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 TP5.serializer;
import TP5.shape.Shape;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
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 TP5.serializer;
import TP5.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 TP5.shape;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) {
Drawer drawer = new Drawer(600, 400);
Group root = new Group();
root.getChildren().add(drawer.getCanvas());
Scene scene = new Scene(root, 600, 400);
primaryStage.setTitle("JavaFX Drawer");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
\ No newline at end of file
package TP5.shape;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Circle implements Shape {
private double radius, x, y;
private boolean isFinished;
public Circle(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.isFinished = true;
}
@Override
public void paint(GraphicsContext graphicsContext) {
graphicsContext.setStroke(Color.BLACK);
graphicsContext.setLineWidth(2);
if (isFinished) {
graphicsContext.setFill(Color.GREEN.deriveColor(0,1,1,0.5));
graphicsContext.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
graphicsContext.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 Math.sqrt(dx * dx + dy * dy) <= radius;
}
@Override
public void translate(double dx, double dy) {
this.x += dx;
this.y += dy;
}
@Override
public boolean isFinished() {
return isFinished;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setFinished(boolean finished) {
isFinished = finished;
}
public void updateRadius(double newX, double newY) {
this.radius = Math.sqrt(Math.pow(newX - this.x, 2) + Math.pow(newY - this.y, 2));
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setDimensions(double x, double y, double width, double height){
this.x = x;
this.y = y;
this.radius = width/2;
}
}
package TP5.shape;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import TP5.state.DrawerContext;
import TP5.state.StateMoveShape;
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 String currentShapeType = "rectangle";
private Shape tempShape = null;
private boolean isFinished = false;
public DrawerContext context;
public Drawer(double width, double height) {
// this.width = width;
// this.height = height;
shapes = new ArrayList<>();
canvas = new Canvas(width,height);
gc = canvas.getGraphicsContext2D();
setupMouseHandlers();
setupKeyHandlers();
}
public void add(Shape shape) {
shapes.add(shape);
}
public void repaint(){
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
for(Shape shape : shapes){
shape.paint(gc);
}
if (tempShape != null){
tempShape.paint(gc);
}
}
private void setupMouseHandlers(){
canvas.setOnMousePressed(event -> {
if(event.getButton() == MouseButton.PRIMARY){
double x = event.getX();
double y = event.getY();
// tempShape = new Rectangle(x, y, 0, 0);
System.out.println(currentShapeType);
if (currentShapeType.equals("rectangle")) {
tempShape = new Rectangle(x, y, 0, 0);
}else if (currentShapeType.equals("circle")) {
tempShape = new Circle(x, y, 0);
}
}
});
canvas.setOnMouseDragged(event ->{
if (tempShape != null) {
double x = event.getX();
double y = event.getY();
if (tempShape instanceof Rectangle){
((Rectangle) tempShape).updateSize(x,y);
}else if (tempShape instanceof Circle){
((Circle) tempShape).updateRadius(x,y);
}
repaint();
}
});
canvas.setOnMouseReleased(event -> {
if(event.getButton() == MouseButton.PRIMARY && tempShape != null){
shapes.add(tempShape);
if (tempShape instanceof Rectangle){
((Rectangle) tempShape).setFinished(true);
}else if (tempShape instanceof Circle){
((Circle) tempShape).setFinished(true);
}
repaint();
tempShape = null;
}
});
}
private void setupKeyHandlers(){
canvas.setFocusTraversable(true);
canvas.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.R){
currentShapeType = "rectangle";
System.out.println("Current shape type is: " + currentShapeType);
}else if (event.getCode() == KeyCode.C){
currentShapeType = "circle";
System.out.println("Current shape type is: " + currentShapeType);
} else if (event.getCode() == KeyCode.M) {
currentShapeType = null ;
}
});
}
/* 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;
}
}
package TP5.shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Rectangle implements Shape{
private double x,y,width, height;
private boolean isFinished;
public Color fillColor;
public Rectangle(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.isFinished = true;
}
@Override
public void paint(GraphicsContext graphicsContext) {
graphicsContext.setStroke(Color.BLACK);
graphicsContext.setLineWidth(2);
if(isFinished){
graphicsContext.setFill(Color.RED.deriveColor(0,1,1,0.5));
graphicsContext.fillRect(x, y, width, height);
}
graphicsContext.strokeRect(x, y, width, height);
}
@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;
}
@Override
public void translate(double dx, double dy) {
x += dx;
y += dy;
}
@Override
public boolean isFinished() {
return isFinished;
}
public void updateSize(double newX, double newY) {
this.width = Math.abs(newX - this.x);
this.height = Math.abs(newY - this.y);
if (newX < this.x && newY < this.y) {
this.x = newX;
this.y = newY;
}
}
public Color getFillColor() {
return fillColor;
}
public void setFinished(boolean finished) {
isFinished = finished;
}
public void setFillColor(Color fillColor) {
this.fillColor = fillColor;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setDimensions(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = Math.abs(width);
this.height = Math.abs(height);
if(width < 0 ){ this.x += width;}
if(height < 0){ this.y += height;}
}
}
package TP5.shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public interface Shape {
void paint(GraphicsContext graphicsContext);
boolean contains(double x, double y);
void translate(double dx, double dy);
boolean isFinished();
}
package TP5.state;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import TP5.shape.Circle;
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
package TP5.state;
import javafx.scene.canvas.Canvas;
import TP5.shape.Shape;
import java.util.ArrayList;
import java.util.List;
public class Drawer extends Canvas {
protected List<Shape> shapes = new ArrayList<>();
public DrawerContext context = new DrawerContext(this);
public Drawer(int width, int height) {
super(width,height);
setFocusTraversable(true);
setOnMousePressed(event->context.mousePressed(event));
setOnMouseReleased(event->context.mouseReleased(event));
setOnMouseMoved(event->context.mouseMoved(event));
setOnMouseDragged(event->context.mouseMoved(event));
setOnKeyPressed(event -> context.keyPressed(event));
}
public void repaint(){
this.getGraphicsContext2D().clearRect(0,0,this.getWidth(),this.getHeight());
for(Shape shape : shapes){
shape.paint(this.getGraphicsContext2D());
}
}
public void addShape(Shape shape){
shapes.add(shape);
repaint();
}
public Shape shapeContains(double x, double y){
for(Shape shape : shapes){
if(shape.contains(x,y)) return shape;
}
return null;
}
}
package TP5.state;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
public class DrawerContext {
Drawer drawer;
DrawerState currentState;
public DrawerContext(Drawer drawer) {
this.drawer = drawer;
this.currentState = new NullDrawerState();
}
public void paint(GraphicsContext graphicsContext) {
drawer.repaint();
}
void mousePressed(MouseEvent event){
currentState.mousePressed(this,event.getX(),event.getY());
}
void mouseReleased(MouseEvent event){
currentState.mouseReleased(this,event.getX(),event.getY());
}
void mouseMoved(MouseEvent event){
currentState.mouseMoved(this,event.getX(),event.getY());
}
public void keyPressed(KeyEvent event) {
switch (event.getText()) {
case "c":
setState(new StateCircle0());
System.out.println("circle state");
break;
case "r":
setState(new StateRectangle0());
break;
case "m":
setState(new StateMoveShape());
System.out.println("state: " + currentState);
break;
default:
setState(new NullDrawerState());
}
}
public Drawer drawer(){
return drawer;
}
public Drawer getDrawer() {
return drawer;
}
public void setState(DrawerState state) {
this.currentState = state;
}
}
package TP5.state;
public 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);
public void paint(DrawerContext context);
}
package TP5.state;
public class NullDrawerState implements DrawerState {
@Override
public void mousePressed(DrawerContext context, double x, double 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(DrawerContext context) {
}
}
package TP5.state;
public enum Shapes {
RECTANGLE,
CIRCLE,
}
package TP5.state;
import TP5.shape.Circle;
public class StateCircle0 implements DrawerState {
@Override
public void mousePressed(DrawerContext context, double x, double y) {
Circle circle = new Circle(x, y,0);
circle.setFinished(false);
context.getDrawer().addShape(circle);
context.setState(new StateCircle1(circle));
}
@Override
public void mouseReleased(DrawerContext context, double x, double y) {
}
@Override
public void mouseMoved(DrawerContext context, double x, double y) {
}
@Override
public void paint(DrawerContext context) {
}
}
package TP5.state;
import TP5.shape.Circle;
public class StateCircle1 implements DrawerState{
private Circle circle;
public StateCircle1(Circle circle) {
this.circle = circle;
}
@Override
public void mousePressed(DrawerContext context, double x, double y) {}
@Override
public void mouseReleased(DrawerContext context, double x, double y) {
circle.setFinished(true);
context.paint(context.getDrawer().getGraphicsContext2D());
context.setState(new StateCircle0());
}
@Override
public void mouseMoved(DrawerContext context, double x, double y) {
double radius = Math.sqrt(Math.pow(x - circle.getX(), 2) + Math.pow(y - circle.getY(), 2));
circle.setDimensions(circle.getX(), circle.getY(), radius*2, radius*2);
context.paint(context.getDrawer().getGraphicsContext2D());
}
@Override
public void paint(DrawerContext context) {
}
}
package TP5.state;
import TP5.shape.Shape;
public class StateMoveShape implements DrawerState{
private Shape selectedShape;
private double previousX, previousY;
@Override
public void mousePressed(DrawerContext context, double x, double y) {
selectedShape = context.getDrawer().shapeContains(x,y);
System.out.println("selectedShape : " + selectedShape);
if(selectedShape != null) {
previousX = x;
previousY = y;
}
}
@Override
public void mouseReleased(DrawerContext context, double x, double y) {
selectedShape = null;
}
@Override
public void mouseMoved(DrawerContext context, double x, double y) {
if(selectedShape != null) {
double deltaX = x - previousX;
double deltaY = y - previousY;
selectedShape.translate(deltaX, deltaY);
previousX = x;
previousY = y;
context.getDrawer().repaint();
}
}
@Override
public void paint(DrawerContext context) {
}
}
package TP5.state;
import TP5.shape.Rectangle;
public class StateRectangle0 implements DrawerState{
public StateRectangle0() { }
@Override
public void mousePressed(DrawerContext context, double x, double y) {
Rectangle rectangle = new Rectangle(x, y,0,0);
rectangle.setFinished(false);
context.getDrawer().addShape(rectangle);
context.setState(new StateRectangle1(rectangle));
}
@Override
public void mouseReleased(DrawerContext context, double x, double y) {
}
@Override
public void mouseMoved(DrawerContext context, double x, double y) {
}
@Override
public void paint(DrawerContext context) {
}
}
package TP5.state;
import TP5.shape.Rectangle;
public class StateRectangle1 implements DrawerState {
private Rectangle rectangle;
private double startX, startY;
public StateRectangle1(Rectangle rectangle) {
this.rectangle = rectangle;
this.startX = rectangle.getX();
this.startY = rectangle.getY();
}
@Override
public void mousePressed(DrawerContext context, double x, double y) {}
@Override
public void mouseReleased(DrawerContext context, double x, double y) {
rectangle.setFinished(true);
context.paint(context.getDrawer().getGraphicsContext2D());
context.setState(new StateRectangle0());
}
@Override
public void mouseMoved(DrawerContext context, double x, double y) {
double width = x - startX;
double height = y - startY;
rectangle.setDimensions(startX,startY,width,height);
context.paint(context.getDrawer().getGraphicsContext2D());
}
@Override
public void paint(DrawerContext context) {
}
}
package TP7.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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment