Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
graphic-2020-TP5
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MANSOUR Chadi
graphic-2020-TP5
Commits
b29bdbe1
Commit
b29bdbe1
authored
6 months ago
by
MANSOUR Chadi
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/shape/Circle.java
+8
-3
8 additions, 3 deletions
src/main/java/shape/Circle.java
src/main/java/shape/Drawer.java
+35
-11
35 additions, 11 deletions
src/main/java/shape/Drawer.java
with
43 additions
and
14 deletions
src/main/java/shape/Circle.java
+
8
−
3
View file @
b29bdbe1
...
...
@@ -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
));
}
}
This diff is collapsed.
Click to expand it.
src/main/java/shape/Drawer.java
+
35
−
11
View file @
b29bdbe1
...
...
@@ -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
(
temp
Rectangl
e
!=
null
){
temp
Rectangl
e
.
paint
(
gc
);
if
(
temp
Shap
e
!=
null
){
temp
Shap
e
.
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
(
temp
Rectangl
e
!=
null
)
{
if
(
temp
Shap
e
!=
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
(
temp
Rectangl
e
);
temp
Rectangl
e
=
null
;
if
(
event
.
getButton
()
==
MouseButton
.
PRIMARY
&&
tempShape
!=
null
){
shapes
.
add
(
temp
Shap
e
);
temp
Shap
e
=
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
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment