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