Citadel Game Design Club Lesson 1: Creating a JavaFX Project

For this exercise, you will need to have a computer with and the most recent (JDK) from Oracle installed, both of which are free downloads.

1.) Create a New Project Open Eclipse. The left pane is labeled Package Explorer. If you've used Eclipse before, you may see projects already listed there. Right-click on the Package Explorer's white space and select "New -> Java Project".

In the box next to "Project Name:", let's give our first project a simple name like "FirstProject".

Then click "Finish".

2.) Create a New Class You should now see a FirstProject item listed in the Package Explorer. Click the little arrow next to FirstProject to expand it. Now right-click src and select "New -> Class".

We need to give this new class a name. Since we're making a Hello World program, in the box next to "Name:" let's type "Hello" for our new class.

Click "Finish".

You should now see a blank Hello.java page in the center pane. This is where we type our program. Note that Eclipse creates a blank Hello class for us.

3.) Launch the Application Let's start off by letting Java know this is going to be a JavaFX graphics program. Type the following.

import .application.Application;

public class Hello extends Application { public static void main(String[] args) { Application.launch(args); } } Let's run our program. Under the "Run" command at top, you'll see a little green arrow. Click that to run your project. If you have multiple projects open, you can click the upside-down arrow next to it to select the "Hello" project.

At this stage, the program should do nothing.

If the program compiled or has error "Exception in Application start method" , go to Step 4.

If the program does not compile with a different error message , it likely means that Eclipse does not know where the JavaFX library is located. This is a little tricky, but you will have to click the project folder and select "Properties -> Java Build Path". Under the Libraries tab, you will have to "Add External JAR" and find the file jfxrt.jar . On my computer, it was under the path: C: \ Program Files (x86) \ Java \ jre1.8.0_31 \ lib \ ext \ jfxrt.jar Ask a faculty member for help if you cannot find it.

4.) Build the Stage We want a stage to place our graphics upon. Think of the stage as the where you will paint your masterpiece. Add the following code. import javafx.application.Application; import javafx.stage.Stage;

public class Hello extends Application { public static void main(String[] args) { Application. launch (args); }

@Override public void start (Stage stage) { stage.setTitle ("My first JavaFX program"); stage.show(); } } Try running this code. You should see a blank white window pop up with your title at the top. 5.) Draw the Scene Now we need to set the scene for our stage. The scene is something you will draw in the background. When the drawing is finished, we will show our scene on the stage. The opening scene is drawn in the "start" function.

Add the code below. Note that when you type the period after the word "Color" you will get a drop-down menu showing the different available colors. It is important that you select a color from the list rather than just type a word. Pick any 2 colors, as long as they are different. I picked the colors "AQUA" and "TOMATO".

Also, there is a trick for getting the imported libraries to appear. You can skip typing the libraries at top and type the code in the start function. Eclipse will often import the correct libraries automatically. When Eclipse puts a red squiggle under a word it doesn't recognize like "Scene", you can click the word and tell Eclipse to add the library. Hopefully, Eclipse will suggest the correct library. Try this out by typing the code for the "start" function first.

import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.text.Text;

public class Hello extends Application { public static void main(String[] args) { Application. launch (args); }

@Override public void start (Stage stage) { stage.setTitle ("My first JavaFX program"); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.AQUA); Text hello = new Text (50,50,"Hello World!"); hello.setFill(Color.TOMATO);

root.getChildren().add(hello); stage.setScene(scene); stage.show(); } }

Run your program. You should see a simple message on the screen. You can try different colors. You could also try changing the position.

Note that our variable hello is an object of the class Text. You could also try out the Text functions below. You would type the code before hello is added to the scene. hello.setScaleX(5); hello.setScaleY(10); hello.setRotate(45);

Be sure to save your program! You will build on this project for the next lesson.