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 (6)
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 {
......
......@@ -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 Circle implements Shape {
public Circle(double x, double y, double sqrt) {
private double radius, x, y;
public Color fillColor;
private boolean isFinished;
public Circle(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.fillColor = Color.GREEN;
this.isFinished = false;
}
@Override
public void paint(GraphicsContext graphicsContext) {
graphicsContext.setStroke(Color.BLACK);
if(isFinished) {
graphicsContext.setFill(fillColor);
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;
}
@Override
public void setFillColor(Color fillColor) {
this.fillColor = fillColor;
}
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.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;
private boolean isFinished = false;
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);
if (tempShape instanceof Rectangle){
tempShape.setFillColor(((Rectangle) tempShape).fillColor);
tempShape.isFinished();
}else if (tempShape instanceof Circle){
tempShape.setFillColor(((Circle) tempShape).fillColor);
tempShape.isFinished();
}
isFinished = 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);
}
});
}
/* 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,61 @@ 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;
private boolean isFinished;
public Color fillColor = Color.RED;
public Rectangle(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.isFinished = false;
}
@Override
public void paint(GraphicsContext graphicsContext) {
graphicsContext.setStroke(Color.BLACK);
if(isFinished){
graphicsContext.setFill(fillColor);
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 int pointsCount() {
return 0;
public void translate(double dx, double dy) {
x += dx;
y += dy;
}
@Override
public Point2D point(int index) {
return null;
public boolean isFinished() {
return isFinished;
}
@Override
public void draw(GraphicsContext context) {
public void setFillColor(Color fillColor) {
this.fillColor = fillColor;
}
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;
}
}
}
......@@ -2,9 +2,12 @@ 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();
void setFillColor(Color fillColor);
}
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);
}
}
package state;
import javafx.scene.canvas.Canvas;
import shape.Circle;
import shape.Shape;
import java.util.ArrayList;
......