Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JavaFX Game tutorials ?  (Read 34718 times)
0 Members and 1 Guest are viewing this topic.
Offline jboadas
« Posted 2015-03-11 18:09:10 »

I am looking for some JavaFX game related tutorials, books or videos

I Have found these so far

http://www.hascode.com/2013/06/creating-and-packaging-a-game-in-java-fx-2-2/
https://carlfx.wordpress.com/2012/03/29/javafx-2-gametutorial-part-1/

and here some videos, didn't see it yet

https://www.youtube.com/channel/UCmjXvUa36DjqCJ1zktXVbUA/videos

do anyone have some more information on JavaFX games ?

I know JavaFX is not the best suited tool for games better use x, y or z, blah, blah, blah Smiley
I just want to give a try for myself.

Thanks

Offline jboadas
« Reply #1 - Posted 2015-03-18 20:37:10 »

Well I got the time to put toguether a simple barebones example of a JavaFX 8 Game loop
maybe not the best but it get the work done.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
package library;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class MainLoop extends Application {

   //size in pixels of the main game canvas
   private final int SIZE_W = 800;
   private final int SIZE_H = 600;
   //one second in nanoseconds
   private final long ONE_SECOND = 1000000000;
   //used to store the current time to calculate fps
   private long currentTime = 0;
   //used to store the last time to calculate fps
   private long lastTime = 0;
   //fps counter
   private int fps = 0;
   //acumulated difference between current time and last time
   private double delta = 0;
   //text to display fps
   private Text tFPS;
   //the game loop timer
   private AnimationTimer mainLoop;
   //the shape to test the main loop
   private Polygon star;
   //direction x,y of the star shape
   private int dirx = 1;
   private int diry = 1;
   
   
   @Override
   public void start(Stage stage) throws Exception {
     
      //main game canvas
      Pane root = new Pane();
      root.setPrefSize(SIZE_W, SIZE_H);
      root.setBackground(new Background(new BackgroundFill(Color.BLACK, null, null)));
     
      //initialize all objects of the main loop
      initNodes(root.getChildren());
      //define the main loop
      initMainLoop(root.getChildren());
      //main scene
      Scene scene = new Scene(root);
      //the main window
      stage.setScene(scene);
      stage.setResizable(false);
      stage.setOnCloseRequest(event -> {
         mainLoop.stop();
         System.exit(0);
      });
      stage.show();
      //start the main loop
      mainLoop.start();
     
     
   }
   
   public void initNodes(ObservableList<Node> rootPane){
      tFPS = new Text("FPS : ");
      tFPS.setTranslateX(SIZE_W-180);
      tFPS.setTranslateY(60);
      tFPS.setFill(Color.WHITE);
      tFPS.setFont(new Font(40));
      rootPane.add(tFPS);
      initAnimation(rootPane);
   }
   
   public void initAnimation(ObservableList<Node> rootPane){
      star = new Polygon();
      Double[] starCoord = new Double[]{
            35.0, 120.5, 37.9, 129.1, 46.9, 129.1, 39.7, 134.5, 42.3,
            143.1, 35.0 , 139.0, 27.7, 143.1, 30.3, 134.5, 23.1, 129.1,
            32.1,129.1};
      star.getPoints().addAll(starCoord);
      star.setTranslateX(100);
      star.setTranslateY(100);
      star.setFill(Color.RED);
      rootPane.add(star);

   }
   
   public void initMainLoop(ObservableList<Node> rootPane){
      lastTime = System.nanoTime();
      mainLoop = new AnimationTimer() {
         
         @Override
         public void handle(long now) {
            currentTime = now;
            fps++;
            delta += currentTime-lastTime;
           
            updateAnimation(rootPane);
           
            if(delta > ONE_SECOND){
               tFPS.setText("FPS : "+fps);
               delta -= ONE_SECOND;
               fps = 0;
            }
           
            lastTime = currentTime;
         }
      };
   }
   
   public void updateAnimation(ObservableList<Node> rootPane){
      Bounds starbounds = star.getBoundsInParent();
     
      double posx = star.getTranslateX();
      double posy = star.getTranslateY();

      if((starbounds.getMaxX() >= SIZE_W) || (starbounds.getMinX() <= 0.0)){
         dirx = -dirx;
         star.setScaleX(1);
         star.setScaleY(1);
      }
      if((starbounds.getMaxY() >= SIZE_H) || (starbounds.getMinY() <= 0.0)){
         diry = -diry;
         star.setScaleX(1);
         star.setScaleY(1);
      }
      star.setTranslateX(posx+(3*dirx));
      star.setTranslateY(posy+(3*diry));
      star.setScaleX(star.getScaleX()+0.1);
      star.setScaleY(star.getScaleY()+0.1);

   }
   
   public static void main(String[] args){
      launch(args);
   }
   

}


more info here:

http://blog.netopyr.com/2012/06/14/using-the-javafx-animationtimer/#comments

hope somebody found this usefull.
Offline AllanM

Junior Newbie





« Reply #2 - Posted 2015-03-23 00:07:22 »

Thanks for this. Some good tutorails there  Smiley

A new book on JavaFX game programming has recently been released. http://www.apress.com/9781484204160

You can download the source code from the link.



Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2015-09-10 01:58:19 »

@AllanM I just came across the book you linked (Beginning Java 8 Games Development, Wallace Jackson) on Safari online, via my public library while looking for a good intro to JavaFX. Chapter 4 seems to be the key. How is it working for you?

Are you taking the NetBeans route it recommends?

I've mostly limited myself to Java2D for graphics, but am thinking of investing some time into learning JavaFX for a couple of reasons. For one, Oracle is committed to developing JavaFX going forward as the main library for graphics, rather than doing more with AWT or Swing. For another, it seems more likely that initiatives such as RoboVM are more likely to first support JavaFX than Swing.

music and music apps: http://adonax.com
Offline philfrei
« Reply #4 - Posted 2015-09-10 04:18:15 »

Came across some more resources. I've started to work through this book:
JavaFX 8, Introduction by Example, Carl Dea

It has a little section with some game-oriented references that look promising (haven't checked them out yet):


music and music apps: http://adonax.com
Offline philfrei
« Reply #5 - Posted 2015-09-17 20:50:52 »

I decided to ditch the "JavaFX Introduction by Example". It's probably fine if you are the type that has read and understands "Effective Java" by Bloch. It's probably also fine if you don't mind learning all about abstract concepts pertaining to lambdas before even getting to basics of layout, or mind mastering conceptual definitions of "Node" prior to actually programming a Button.

I'm reading "JavaFX for Dummies" and learned more useful stuff in 1 hour than I did struggling with the Dea book over three torturous sessions. (Am reading via public library online utilities.)

music and music apps: http://adonax.com
Offline Kami

Senior Devvie


Medals: 16
Projects: 1


I am not a vampire


« Reply #6 - Posted 2015-09-17 21:12:47 »

Came across some more resources. I've started to work through this book:
JavaFX 8, Introduction by Example, Carl Dea
This^^

Although I would go for LibGDX instead of JavaFX  Pointing But hey, everyone got their own reasons Smiley

"If I can't be the best I sure as hell can be the worst"
Twitter: https://twitter.com/mushkilann
LinkedIn: https://www.linkedin.com/pub/kami-nasri/108/227/436
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (397 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5544 views)
2018-11-16 13:35:35

hadezbladez (1150 views)
2018-11-16 13:32:03

EgonOlsen (4585 views)
2018-06-10 19:43:48

EgonOlsen (5462 views)
2018-06-10 19:43:44

EgonOlsen (3119 views)
2018-06-10 19:43:20

DesertCoockie (4016 views)
2018-05-13 18:23:11

nelsongames (4708 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!