Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
  • revert-d81c9d6d
2 results

Target

Select target project
  • b21228304/graphic-2020-2-0
  • l21221596/graphic-2020
  • c19014407/graphic-2020
  • b23019494/graphic-2020
  • g21212987/graphic-2020
  • couetoux.b/graphic-2020
  • s22029480/graphic-2020
  • c23025119/tp-3-graphique
  • m19023837/graphic-2020-tp-5
  • s22029480/graphic-tp-4
  • m19023837/graphic-2020-tp-7
  • n22021899/tp-3-programmation-conception
  • r24025701/graphic-2020
  • a23022716/exo-2-tp-3-algo
  • m23000189/graphic-2024
  • b21228304/graphic-2020
  • t21234458/graphic-2020
  • v23014723/graphic-2020
  • h22021483/graphic-2020
  • t21234458/graphic-2020-tiakout-khaled
  • m19023837/graphic-2020-chadi
  • b20031964/graphic-2020
22 results
Select Git revision
  • master
1 result
Show changes
Commits on Source (12)
Showing
with 492 additions and 65 deletions
......@@ -20,7 +20,7 @@ dependencies {
testImplementation 'org.hamcrest:hamcrest-library:1.3'
}
mainClassName = "shape.App"
mainClassName = "state.App"
test {
......
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 {
......
......@@ -7,7 +7,6 @@ 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 {
......
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) {
Drawer drawer = new Drawer(600, 400);
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));
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 shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Circle implements Shape {
public Circle(double x, double y, double sqrt) {
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 int pointsCount() {
return 0;
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 Point2D point(int index) {
return null;
public void translate(double dx, double dy) {
this.x += dx;
this.y += dy;
}
@Override
public void draw(GraphicsContext context) {
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 shape;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import state.DrawerContext;
import 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;
}
}
......@@ -5,23 +5,84 @@ import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Rectangle implements Shape{
Color color;
Rectangle(Color color, Point2D point0, Point2D point1){
this.color = color;
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 int pointsCount() {
return 0;
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 Point2D point(int index) {
return null;
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 draw(GraphicsContext context) {
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;}
}
}
......@@ -2,9 +2,11 @@ package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
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);
boolean isFinished();
}
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){}
public void draw(GraphicsContext context){
for(Shape shape : shapes)
shape.draw(context);
}
}
......@@ -4,6 +4,7 @@ import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import shape.Circle;
public class App extends Application {
......@@ -15,6 +16,7 @@ public class App extends Application {
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();
......
package state;
import javafx.scene.canvas.Canvas;
import shape.Circle;
import shape.Shape;
import java.util.ArrayList;
......@@ -19,10 +18,23 @@ public class Drawer extends Canvas {
setOnMouseReleased(event->context.mouseReleased(event));
setOnMouseMoved(event->context.mouseMoved(event));
setOnMouseDragged(event->context.mouseMoved(event));
//setOnKeyPressed(event -> context.keyPressed(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 state;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
......@@ -11,6 +12,10 @@ public class DrawerContext {
public DrawerContext(Drawer drawer) {
this.drawer = drawer;
this.currentState = new NullDrawerState();
}
public void paint(GraphicsContext graphicsContext) {
drawer.repaint();
}
void mousePressed(MouseEvent event){
......@@ -22,12 +27,36 @@ public class DrawerContext {
currentState.mouseReleased(this,event.getX(),event.getY());
}
void mouseMoved(MouseEvent event){}
void mouseMoved(MouseEvent event){
currentState.mouseMoved(this,event.getX(),event.getY());
}
public void keyPressed(KeyEvent event) {
switch (event.getText()) {
case "c":
currentState = new StateCircle0();
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;
}
}
......@@ -3,4 +3,6 @@ package 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 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 state;
public enum Shapes {
RECTANGLE,
CIRCLE,
}
package state;
import 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));
}
......@@ -10,4 +16,14 @@ public class StateCircle0 implements DrawerState {
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 state;
import 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 state;
import 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 state;
import 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 state;
import 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) {
}
}