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
00d017b8
Commit
00d017b8
authored
6 months ago
by
MANSOUR Chadi
Browse files
Options
Downloads
Patches
Plain Diff
drawing rectangle done
parent
ed11c01f
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/main/java/shape/App.java
+1
-16
1 addition, 16 deletions
src/main/java/shape/App.java
src/main/java/shape/Drawer.java
+15
-5
15 additions, 5 deletions
src/main/java/shape/Drawer.java
src/main/java/shape/Rectangle.java
+9
-0
9 additions, 0 deletions
src/main/java/shape/Rectangle.java
with
25 additions
and
21 deletions
src/main/java/shape/App.java
+
1
−
16
View file @
00d017b8
...
...
@@ -17,24 +17,9 @@ public class App extends Application {
public
void
start
(
Stage
primaryStage
)
{
Drawer
drawer
=
new
Drawer
(
400
,
400
);
Circle
circle
=
new
Circle
(
200
,
200
,
50
);
Rectangle
rectangle
=
new
Rectangle
(
100
,
100
,
150
,
100
);
drawer
.
add
(
circle
);
drawer
.
add
(
rectangle
);
drawer
.
repaint
();
drawer
.
getCanvas
().
setOnMouseClicked
(
event
->
{
double
mouseX
=
event
.
getX
();
double
mouseY
=
event
.
getY
();
Shape
clickedShape
=
drawer
.
shapeContaining
(
mouseX
,
mouseY
);
if
(
clickedShape
!=
null
)
{
System
.
out
.
println
(
"Shape clicked: "
+
clickedShape
);
}
});
Group
root
=
new
Group
();
root
.
getChildren
().
add
(
drawer
.
getCanvas
());
Scene
scene
=
new
Scene
(
root
,
400
,
400
);
primaryStage
.
setTitle
(
"JavaFX Drawer"
);
...
...
This diff is collapsed.
Click to expand it.
src/main/java/shape/Drawer.java
+
15
−
5
View file @
00d017b8
...
...
@@ -10,16 +10,16 @@ import java.util.List;
public
class
Drawer
{
private
List
<
Shape
>
shapes
;
private
double
width
;
private
double
height
;
//
private double width;
//
private double height;
private
Canvas
canvas
;
private
GraphicsContext
gc
;
private
Rectangle
tempRectangle
=
null
;
public
Drawer
(
double
width
,
double
height
)
{
this
.
width
=
width
;
this
.
height
=
height
;
//
this.width = width;
//
this.height = height;
shapes
=
new
ArrayList
<>();
canvas
=
new
Canvas
(
width
,
height
);
gc
=
canvas
.
getGraphicsContext2D
();
...
...
@@ -30,7 +30,7 @@ public class Drawer {
shapes
.
add
(
shape
);
}
public
void
repaint
(){
gc
.
clearRect
(
0
,
0
,
width
,
h
eight
);
gc
.
clearRect
(
0
,
0
,
canvas
.
getWidth
(),
canvas
.
getH
eight
()
);
for
(
Shape
shape
:
shapes
){
shape
.
paint
(
gc
);
}
...
...
@@ -47,6 +47,16 @@ public class Drawer {
tempRectangle
=
new
Rectangle
(
x
,
y
,
0
,
0
);
}
});
canvas
.
setOnMouseDragged
(
event
->{
if
(
tempRectangle
!=
null
)
{
double
x
=
event
.
getX
();
double
y
=
event
.
getY
();
tempRectangle
.
updateSize
(
x
,
y
);
repaint
();
}
});
canvas
.
setOnMouseReleased
(
event
->
{
if
(
event
.
getButton
()
==
MouseButton
.
PRIMARY
){
shapes
.
add
(
tempRectangle
);
...
...
This diff is collapsed.
Click to expand it.
src/main/java/shape/Rectangle.java
+
9
−
0
View file @
00d017b8
...
...
@@ -20,6 +20,7 @@ public class Rectangle implements Shape{
@Override
public
void
paint
(
GraphicsContext
graphicsContext
)
{
graphicsContext
.
setStroke
(
Color
.
BLACK
);
graphicsContext
.
strokeRect
(
x
,
y
,
width
,
height
);
}
...
...
@@ -35,4 +36,12 @@ public class Rectangle implements Shape{
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
;
}
}
}
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