Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
EXO2_TP3_ALGO
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
AREZKI Celia
EXO2_TP3_ALGO
Commits
08fd7c8b
Commit
08fd7c8b
authored
8 months ago
by
AREZKI Celia
Browse files
Options
Downloads
Patches
Plain Diff
Add mouse event handling to allow the user to draw a rectanglein Drawer class
parent
0b28f0d0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/shape/tp5/Drawer.java
+40
-14
40 additions, 14 deletions
src/main/java/shape/tp5/Drawer.java
with
40 additions
and
14 deletions
src/main/java/shape/tp5/Drawer.java
+
40
−
14
View file @
08fd7c8b
...
...
@@ -2,21 +2,51 @@ package shape.tp5;
import
javafx.scene.canvas.Canvas
;
import
javafx.scene.canvas.GraphicsContext
;
import
javafx.scene.input.MouseButton
;
import
javafx.scene.input.MouseEvent
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
Drawer
extends
Canvas
{
private
List
<
Shape
>
shapes
;
class
Drawer
extends
Canvas
{
private
List
<
Shape
>
shapes
=
new
ArrayList
<>();
private
double
startX
,
startY
,
endX
,
endY
;
private
boolean
drawing
=
false
;
public
Drawer
(
double
width
,
double
height
)
{
super
(
width
,
height
);
shapes
=
new
ArrayList
<>();
setOnMousePressed
(
this
::
handleMousePressed
);
setOnMouseDragged
(
this
::
handleMouseDragged
);
setOnMouseReleased
(
this
::
handleMouseReleased
);
}
private
void
handleMousePressed
(
MouseEvent
event
)
{
if
(
event
.
getButton
()
==
MouseButton
.
PRIMARY
)
{
startX
=
event
.
getX
();
startY
=
event
.
getY
();
drawing
=
true
;
}
}
private
void
handleMouseDragged
(
MouseEvent
event
)
{
if
(
drawing
)
{
endX
=
event
.
getX
();
endY
=
event
.
getY
();
repaint
();
}
}
public
void
add
(
Shape
shape
)
{
shapes
.
add
(
shape
);
private
void
handleMouseReleased
(
MouseEvent
event
)
{
if
(
drawing
)
{
endX
=
event
.
getX
();
endY
=
event
.
getY
();
shapes
.
add
(
new
Rectangle
(
Math
.
min
(
startX
,
endX
),
Math
.
min
(
startY
,
endY
),
Math
.
abs
(
endX
-
startX
),
Math
.
abs
(
endY
-
startY
)));
drawing
=
false
;
repaint
();
}
}
public
void
repaint
()
{
GraphicsContext
gc
=
getGraphicsContext2D
();
...
...
@@ -24,14 +54,10 @@ public class Drawer extends Canvas {
for
(
Shape
shape
:
shapes
)
{
shape
.
paint
(
gc
);
}
if
(
drawing
)
{
gc
.
setStroke
(
javafx
.
scene
.
paint
.
Color
.
BLACK
);
gc
.
strokeRect
(
Math
.
min
(
startX
,
endX
),
Math
.
min
(
startY
,
endY
),
Math
.
abs
(
endX
-
startX
),
Math
.
abs
(
endY
-
startY
));
}
public
Shape
shapeContaining
(
double
x
,
double
y
)
{
for
(
Shape
shape
:
shapes
)
{
if
(
shape
.
contains
(
x
,
y
))
{
return
shape
;
}
}
return
null
;
}
}
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