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 (4)
Showing with 303 additions and 42 deletions
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.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class AbstractShape implements Shape {
protected List<Point2D> points = new ArrayList<>();
public void addPoints(Point2D... points){
this.points.addAll(Arrays.asList(points));
}
public int pointsCount() {
return points.size();
}
public Point2D point(int index) {
if( index >= 0 && index < points.size()){
return points.get(index);
}else {
throw new IndexOutOfBoundsException("Index out of bounds");
}
}
public abstract void draw(GraphicsContext context) ;
}
......@@ -7,29 +7,26 @@ 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(400, 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, 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
package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class BorderDecorator extends Decorator {
@Override
public void paint(GraphicsContext graphicsContext) {
}
@Override
public boolean contains(double x, double y) {
return false;
}
@Override
public void translate(double dx, double dy) {
}
}
package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class CenterDecorator extends Decorator{
@Override
public void paint(GraphicsContext graphicsContext) {
}
@Override
public boolean contains(double x, double y) {
return false;
}
@Override
public void translate(double dx, double dy) {
}
}
......@@ -2,24 +2,39 @@ 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;
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.setStroke(Color.RED);
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 Math.sqrt(dx*dx + dy*dy) <= radius;
}
@Override
public void draw(GraphicsContext context) {
public void translate(double dx, double dy) {
this.x += dx;
this.y += dy;
}
public void updateRadius(double newX, double newY) {
this.radius = Math.sqrt(Math.pow(newX - this.x, 2) + Math.pow(newY - this.y, 2));
}
}
package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public abstract class Decorator implements Shape {
}
package shape;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
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 String currentShapeType = "rectangle";
private Shape tempShape = 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();
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);
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);
tempShape = null;
repaint();
}
});
}
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);
}
});
}
/* 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 shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Polygon extends AbstractShape {
private final Color color;
public Polygon(Color color, Point2D... points) {
this.color = color;
addPoints(points);
}
@Override
public void draw(GraphicsContext context) {
context.setStroke(color);
context.beginPath();
context.moveTo(points.get(0).getX(), points.get(0).getY());
for (int i = 1; i < pointsCount() ; i++) {
context.lineTo(points.get(i).getX(), points.get(i).getY());
}
context.closePath();
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) {
}
}
......@@ -5,23 +5,43 @@ 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;
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 int pointsCount() {
return 0;
public void paint(GraphicsContext graphicsContext) {
graphicsContext.setStroke(Color.BLACK);
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;
}
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;
}
}
}
......@@ -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);
}
......@@ -9,10 +9,10 @@ public class ShapeContainer{
private List<Shape> shapes = new ArrayList<>();
public void addShape(Shape shape){}
public void addShape(Shape shape){
shapes.add(shape);
}
public void draw(GraphicsContext context){
for(Shape shape : shapes)
shape.draw(context);
}
}
package state;
import javafx.scene.canvas.Canvas;
import shape.Circle;
import shape.Shape;
import java.util.ArrayList;
......