Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dtek1049-harj-d
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
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
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
Admin message
Maintenance moved to Monday 17.3. at 13:00. ETA 60 - 90 minutes.
Show more breadcrumbs
Lauri Orava
dtek1049-harj-d
Commits
bc25a9ab
Commit
bc25a9ab
authored
5 years ago
by
juoksuorava
Browse files
Options
Downloads
Patches
Plain Diff
Lisätty toiminnallisuudet 1-6 tehtävänannosta.
parent
54896997
Branches
master
No related tags found
No related merge requests found
Pipeline
#21413
passed
5 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/fi/utu/tech/visualnotes/graphics/MainWindow.java
+95
-6
95 additions, 6 deletions
...ain/java/fi/utu/tech/visualnotes/graphics/MainWindow.java
with
95 additions
and
6 deletions
src/main/java/fi/utu/tech/visualnotes/graphics/MainWindow.java
+
95
−
6
View file @
bc25a9ab
...
...
@@ -3,12 +3,20 @@ package fi.utu.tech.visualnotes.graphics;
import
fi.utu.tech.graphics.Point2D
;
import
fi.utu.tech.visualnotes.graphics.shapes.*
;
import
fi.utu.tech.visualnotes.graphics.shapes.Shape.ShapeType
;
import
javafx.collections.FXCollections
;
import
javafx.collections.ObservableList
;
import
javafx.event.EventHandler
;
import
javafx.geometry.Pos
;
import
javafx.scene.canvas.Canvas
;
import
javafx.scene.canvas.GraphicsContext
;
import
javafx.scene.control.Button
;
import
javafx.scene.control.ComboBox
;
import
javafx.scene.input.MouseEvent
;
import
javafx.scene.layout.VBox
;
import
javafx.stage.FileChooser
;
import
javafx.stage.Stage
;
import
java.io.File
;
import
java.nio.file.Path
;
public
class
MainWindow
extends
VBox
{
...
...
@@ -26,24 +34,71 @@ public class MainWindow extends VBox {
this
.
stage
=
stage
;
root
=
new
ShapeGraphRoot
();
view
=
new
ShapeGraphView
(
root
);
view
.
setView
(
0
,
0
,
5
00
,
5
0
0
);
canvas
=
new
Canvas
(
10
00
,
10
00
);
color
=
Color
.
Black
;
view
.
setView
(
0
,
0
,
3
00
,
1
50
);
canvas
=
new
Canvas
(
6
00
,
3
00
);
color
=
Color
.
Red
;
shapeToDraw
=
ShapeType
.
Line
;
setPrefSize
(
500
,
500
);
setMaxSize
(
500
,
500
);
getChildren
().
addAll
(
canvas
);
setPrefSize
(
300
,
150
);
setMaxSize
(
300
,
150
);
graphicsContext
=
canvas
.
getGraphicsContext2D
();
//Buttons for the options-menu
ObservableList
<
Color
>
colors
=
FXCollections
.
observableArrayList
(
Color
.
Red
,
Color
.
Blue
,
Color
.
Green
,
Color
.
Yellow
,
Color
.
Orange
,
Color
.
Black
,
Color
.
White
);
ObservableList
<
ShapeType
>
shapes
=
FXCollections
.
observableArrayList
(
ShapeType
.
Line
,
ShapeType
.
Oval
,
ShapeType
.
Rectangle
);
ComboBox
listOfColors
=
new
ComboBox
(
colors
);
listOfColors
.
setValue
(
colors
.
get
(
0
));
listOfColors
.
setOnAction
(
event
->
{
color
=
(
Color
)
listOfColors
.
getValue
();
});
ComboBox
listOfShapes
=
new
ComboBox
(
shapes
);
listOfShapes
.
setValue
(
shapes
.
get
(
0
));
listOfShapes
.
setOnAction
(
event
->
{
shapeToDraw
=
(
ShapeType
)
listOfShapes
.
getValue
();
});
Button
clear
=
new
Button
(
"Clear"
);
clear
.
setOnAction
(
event
->
{
graphicsContext
.
clearRect
(
0
,
0
,
canvas
.
getWidth
(),
canvas
.
getHeight
());
root
.
clear
();
});
Button
load
=
new
Button
(
"Open file"
);
load
.
setOnAction
(
event
->
loadFile
());
Button
save
=
new
Button
(
"Save file"
);
save
.
setOnAction
(
event
->
saveFile
());
//Options-menu
VBox
options
=
new
VBox
(
listOfColors
,
listOfShapes
,
clear
,
load
,
save
);
options
.
setAlignment
(
Pos
.
BOTTOM_LEFT
);
options
.
setSpacing
(
5
);
getChildren
().
addAll
(
canvas
,
options
);
//Event handlers for drawing
canvas
.
addEventHandler
(
MouseEvent
.
MOUSE_PRESSED
,
new
EventHandler
<
MouseEvent
>()
{
@Override
public
void
handle
(
MouseEvent
mouseEvent
)
{
startPoint
=
new
Point2D
(
mouseEvent
.
getX
(),
mouseEvent
.
getY
());
}
});
canvas
.
addEventHandler
(
MouseEvent
.
MOUSE_RELEASED
,
new
EventHandler
<
MouseEvent
>()
{
@Override
public
void
handle
(
MouseEvent
mouseEvent
)
{
...
...
@@ -70,4 +125,38 @@ public class MainWindow extends VBox {
}
});
}
private
void
loadFile
()
{
FileChooser
fileChooser
=
new
FileChooser
();
fileChooser
.
setTitle
(
"Load from"
);
File
fileSelected
=
fileChooser
.
showOpenDialog
(
stage
);
if
(
fileSelected
==
null
)
return
;
Path
path
=
fileSelected
.
toPath
();
if
(
path
.
toString
().
endsWith
(
".vin"
))
{
graphicsContext
.
clearRect
(
0
,
0
,
canvas
.
getWidth
(),
getHeight
());
new
Thread
(()
->
{
try
{
root
.
load
(
path
);
for
(
Shape
shape
:
root
.
contents
())
{
shape
.
render
(
graphicsContext
,
view
.
offset
());
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}).
start
();
}
else
{
return
;
}
}
private
void
saveFile
()
{
FileChooser
fileChooser
=
new
FileChooser
();
fileChooser
.
getExtensionFilters
().
add
(
new
FileChooser
.
ExtensionFilter
(
"vin files (.vin)"
,
"*.vin"
));
fileChooser
.
setTitle
(
"Save to"
);
File
fileSelected
=
fileChooser
.
showSaveDialog
(
stage
);
if
(
fileSelected
==
null
)
return
;
Path
path
=
fileSelected
.
toPath
();
new
Thread
(()
->
{
try
{
root
.
save
(
path
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}).
start
();
}
}
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