Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CanvasTemplateJavaFX
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Kaller
CanvasTemplateJavaFX
Commits
0a13947a
Commit
0a13947a
authored
3 years ago
by
kaller01
Browse files
Options
Downloads
Patches
Plain Diff
init
parents
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
App.java
+56
-0
56 additions, 0 deletions
App.java
Main.java
+57
-0
57 additions, 0 deletions
Main.java
with
113 additions
and
0 deletions
App.java
0 → 100644
+
56
−
0
View file @
0a13947a
import
java.util.HashSet
;
import
javafx.scene.Group
;
import
javafx.scene.Scene
;
import
javafx.scene.canvas.Canvas
;
import
javafx.scene.canvas.GraphicsContext
;
import
javafx.scene.paint.Color
;
import
javafx.stage.Stage
;
public
class
App
{
private
Stage
primaryStage
;
private
Canvas
canvas
;
// These should probably not be here, just for example
private
int
x
=
0
;
private
int
xSpeed
=
3
;
public
App
(
Stage
stage
)
{
primaryStage
=
stage
;
Group
root
=
new
Group
();
// Create root element for scene, ex group
Scene
scene
=
new
Scene
(
root
);
// Create the scene with root element
canvas
=
new
Canvas
(
Main
.
WIDTH
,
Main
.
HEIGHT
);
root
.
getChildren
().
add
(
canvas
);
primaryStage
.
setScene
(
scene
);
primaryStage
.
show
();
}
/**
* This method should be called for every frame
*
* @param dt the passed time since last drawn frame
*/
public
void
update
(
double
dt
)
{
GraphicsContext
gc
=
canvas
.
getGraphicsContext2D
();
// clear backgroud, try removing this line.
gc
.
clearRect
(
0
,
0
,
canvas
.
getWidth
(),
canvas
.
getHeight
());
gc
.
fill
();
gc
.
setFill
(
Color
.
GREEN
);
gc
.
fillRect
(
x
,
0
,
100
,
100
);
x
+=
xSpeed
;
}
/**
* Update the currently active keys
* @param currentlyActiveKeys
*/
public
void
update
(
HashSet
<
String
>
currentlyActiveKeys
)
{
if
(
currentlyActiveKeys
.
contains
(
"SPACE"
))
xSpeed
=
-
xSpeed
;
}
public
Scene
getScene
()
{
return
primaryStage
.
getScene
();
}
}
This diff is collapsed.
Click to expand it.
Main.java
0 → 100644
+
57
−
0
View file @
0a13947a
import
javafx.animation.KeyFrame
;
import
javafx.animation.Timeline
;
import
javafx.application.Application
;
import
javafx.scene.Scene
;
import
javafx.stage.Stage
;
import
javafx.util.Duration
;
import
java.util.HashSet
;
public
class
Main
extends
Application
{
public
static
final
double
WIDTH
=
1600
;
public
static
final
double
HEIGHT
=
800
;
public
static
final
int
FRAMES_PER_SECOND
=
60
;
private
static
final
int
MILLISECOND_DELAY
=
1000
/
FRAMES_PER_SECOND
;
private
static
final
double
SECOND_DELAY
=
1.0
/
FRAMES_PER_SECOND
;
private
HashSet
<
String
>
currentlyActiveKeys
=
new
HashSet
<>();
private
App
app
;
@Override
public
void
start
(
Stage
primaryStage
)
throws
Exception
{
primaryStage
.
setTitle
(
"JavaFX canvas"
);
app
=
new
App
(
primaryStage
);
// timeline and fps
KeyFrame
frame
=
new
KeyFrame
(
Duration
.
millis
(
MILLISECOND_DELAY
),
e
->
{
app
.
update
(
SECOND_DELAY
);
});
Timeline
animation
=
new
Timeline
();
animation
.
setCycleCount
(
Timeline
.
INDEFINITE
);
animation
.
getKeyFrames
().
add
(
frame
);
animation
.
play
();
// Listeners
keyListener
(
app
.
getScene
());
}
public
void
keyListener
(
Scene
scene
)
{
scene
.
setOnKeyPressed
(
event
->
{
String
codeString
=
event
.
getCode
().
toString
();
if
(!
currentlyActiveKeys
.
contains
(
codeString
))
{
currentlyActiveKeys
.
add
(
codeString
);
System
.
out
.
println
(
currentlyActiveKeys
);
app
.
update
(
currentlyActiveKeys
);
}
});
scene
.
setOnKeyReleased
(
event
->
{
currentlyActiveKeys
.
remove
(
event
.
getCode
().
toString
());
app
.
update
(
currentlyActiveKeys
);
});
}
public
static
void
main
(
String
[]
args
)
{
launch
(
args
);
}
}
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