Skip to content
Snippets Groups Projects
Commit b29bdbe1 authored by MANSOUR Chadi's avatar MANSOUR Chadi
Browse files

drawing circles and added Key pressing function to change shapes

Tested and is working
parent 00d017b8
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ package shape; ...@@ -2,6 +2,7 @@ package shape;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Circle implements Shape { public class Circle implements Shape {
...@@ -16,14 +17,15 @@ public class Circle implements Shape { ...@@ -16,14 +17,15 @@ public class Circle implements Shape {
@Override @Override
public void paint(GraphicsContext graphicsContext) { public void paint(GraphicsContext graphicsContext) {
graphicsContext.setStroke(Color.RED);
graphicsContext.strokeOval(x-radius, y-radius, 2 * radius, 2 * radius); graphicsContext.strokeOval(x-radius, y-radius, 2 * radius, 2 * radius);
} }
@Override @Override
public boolean contains(double x, double y) { public boolean contains(double x, double y) {
double dx = x -= this.x; double dx = x - this.x;
double dy = y -= this.y; double dy = y - this.y;
return dx*dx + dy*dy <= radius*radius; return Math.sqrt(dx*dx + dy*dy) <= radius;
} }
@Override @Override
...@@ -31,5 +33,8 @@ public class Circle implements Shape { ...@@ -31,5 +33,8 @@ public class Circle implements Shape {
this.x += dx; this.x += dx;
this.y += dy; 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));
}
} }
...@@ -2,6 +2,7 @@ package shape; ...@@ -2,6 +2,7 @@ package shape;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton; import javafx.scene.input.MouseButton;
...@@ -15,7 +16,8 @@ public class Drawer { ...@@ -15,7 +16,8 @@ public class Drawer {
private Canvas canvas; private Canvas canvas;
private GraphicsContext gc; private GraphicsContext gc;
private Rectangle tempRectangle = null; private String currentShapeType = "rectangle";
private Shape tempShape = null;
public Drawer(double width, double height) { public Drawer(double width, double height) {
// this.width = width; // this.width = width;
...@@ -24,6 +26,7 @@ public class Drawer { ...@@ -24,6 +26,7 @@ public class Drawer {
canvas = new Canvas(width,height); canvas = new Canvas(width,height);
gc = canvas.getGraphicsContext2D(); gc = canvas.getGraphicsContext2D();
setupMouseHandlers(); setupMouseHandlers();
setupKeyHandlers();
} }
public void add(Shape shape) { public void add(Shape shape) {
...@@ -34,8 +37,8 @@ public class Drawer { ...@@ -34,8 +37,8 @@ public class Drawer {
for(Shape shape : shapes){ for(Shape shape : shapes){
shape.paint(gc); shape.paint(gc);
} }
if (tempRectangle != null){ if (tempShape != null){
tempRectangle.paint(gc); tempShape.paint(gc);
} }
} }
...@@ -44,36 +47,57 @@ public class Drawer { ...@@ -44,36 +47,57 @@ public class Drawer {
if(event.getButton() == MouseButton.PRIMARY){ if(event.getButton() == MouseButton.PRIMARY){
double x = event.getX(); double x = event.getX();
double y = event.getY(); double y = event.getY();
tempRectangle = new Rectangle(x, y, 0, 0); // 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 ->{ canvas.setOnMouseDragged(event ->{
if (tempRectangle != null) { if (tempShape != null) {
double x = event.getX(); double x = event.getX();
double y = event.getY(); double y = event.getY();
tempRectangle.updateSize(x,y); if (tempShape instanceof Rectangle){
((Rectangle) tempShape).updateSize(x,y);
}else if (tempShape instanceof Circle){
((Circle) tempShape).updateRadius(x,y);
}
repaint(); repaint();
} }
}); });
canvas.setOnMouseReleased(event -> { canvas.setOnMouseReleased(event -> {
if(event.getButton() == MouseButton.PRIMARY){ if(event.getButton() == MouseButton.PRIMARY && tempShape != null){
shapes.add(tempRectangle); shapes.add(tempShape);
tempRectangle = null; tempShape = null;
repaint(); 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){ /* public Shape shapeContaining(double x, double y){
for(Shape shape : shapes){ for(Shape shape : shapes){
if (shape.contains(x, y)){ if (shape.contains(x, y)){
return shape; return shape;
} }
} }
return null; return null;
} }*/
public Canvas getCanvas() { public Canvas getCanvas() {
return canvas; return canvas;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment