Question

Java FX Application Purpose The purpose of this assignment is to get you familiar with the...

Java FX Application

Purpose

The purpose of this assignment is to get you familiar with the basics of the JavaFX GUI interface components. This assignment will cover Labels, Fonts, Basic Images, and Layouts. You will use a StackPane and a BorderPane to construct the layout to the right. In addition you will use the Random class to randomly load an image when the application loads

Picture.png

Introduction

The application sets the root layout to a BorderPane. The BorderPane can be divided into four areas: North, South, East, West, Center. In the North of the Border Pane is the Label that says Random Card. In the South is the Label that says 'Nothing Up My Sleeve'. The middle of the BorderPane is a Label that holds image of the card. This Label is placed inside a StackPane which is then placed in the middle.

The image is randomly loaded by creating the name of the card from a random number that is generated using the Random Object

The Cards

There are 55 gif images of the cards. They are named 101.gif up to 155.gif. This includes two Jokers and the card back. The images have been placed in a zip file and can be downloaded here.

You should download the cards and unzip the folder and place the images in a folder named img. This folder should be in the root of your project folder. If you are using Netbeans the root folder should have the following folders:

build

dist

nbproject

src

and img which you will create.

Step 1 - Getting Started

The first thing you need to do is create a new JavaFX application project. You can name this project anything you wish but something like Assignment3 or Lab3 seems to be appropriate.

If you have used NetBeans it creates a 'Hello World' application that will run. This is OK you are just going to have to change a few things. First, make sure that you have imported javafx.scene.layout.* You may simply need to change the javafx.scene.layout.StackPane import. The reason we are doing this is that we are going to use two layouts.

Inside the start method do the following:

Create a BorderPane called root.

Create a new Scene object called scene and pass the BorderPane called root, and a height of 300, width of 250 to the constructor.

Call the setTitle method that is a member of primaryStage and set the title to 'Assignment 3'

Call the show method that is part of the primaryStage object. This has no parameters.

At this point you should have a simple empty window that runs.

Step 2 - Creating Components

Create a new StackPane called middle

Create a Label for the top. Name it anything you like. Use the constructor to give it some text of your choosing

Create a Label to hold the card image. Name it lblCard. Use the default constructor to leave the Label blank

Create a Label for the bottom. Name it anything you like. Use the constructor to give it some text of your choosing

Add the Label you created for the top to the North of the BorderPane called root

Add the Label you created for the bottom to the South of the BorderPane called root.

Add lblCard to the StackPane called middle

Add the StackPane to the Center of the BorderPane called root.

At this point you should have the layout set and all of your Labels in place (of course no image yet)

Step 3 - Create The Fonts & Colors

Create a Font for the top Label. Use any font, weight and size you want

Use the setFont method for the top label to set the Font you just created

Use the setFillColor method on the top label to give the label some color. Use any Color you wish

Create a Font for the bottom Label. Use any font, weight and size you want

Use the setFont method for the bottom label to set the Font you just created

Use the setFillColor method on the bottom label to give the label some color. Use any Color you wish

At this point your application should look something like this:

Step3.png

Step 4 - Load The Image

Make sure you have imported javafx.scene.image.*. You will need this to create and load an image. Also make sure you have imported java.util.Random. You will need this to generate a random number

The cards are named 101.gif to 155.gif. You will need to generate a random number in this range.

Once you have a random number in the range of 101 to 155 you will need this to construct the name and path to the image. You will need to construct a String that looks something like this

file:img\\101.gif

Using file tells Java to get the image from a file and img is the folder to get if from. Notice the use of \\ this is an escape character that allows the use of \ in the path (which is needed by Windows). Finally, .gif is the file extension

The above can easily be done with String concatenation simply use the random number where 101 is.

Create a new Image called imgCard pass the String that contains the file path to the constructor.

Call the setGraphic method on lblCard pass to the constructor new ImageView(imgCard)

At this point you should have a working application. Each time you start the program a new card image should appear.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program screeshot:

Code to copy:

// Header files

import javafx.scene.image.*;

import java.util.Random;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.StackPane;

import javafx.scene.paint.Color;

import javafx.scene.text.Font;

import javafx.stage.Stage;

// Create a class named "RandomImage"

public class RandomImage extends Application {

     @Override

          public void start(Stage primaryStage) {

          primaryStage.setTitle("Assignment 3");

          BorderPane root = new BorderPane();

          StackPane middle = new StackPane();

          Label lblTop = new Label("Random Card");

          //set font style for the label and size

          lblTop.setFont(new Font("Arial", 20));

          //to set the color to the label font

          lblTop.setTextFill(Color.ROYALBLUE);

          //set label to the border pane

          root.setTop(lblTop);

          Label lblCard = new Label();

          middle.getChildren().add(lblCard);

          //genrating the path and the name of the image

          String imgName = randomName();

          //creating the image

          Image imgCard = new Image(imgName);

          lblCard.setGraphic(new ImageView(imgCard));

          root.setCenter(middle);

          Label lblBottom = new Label("Nothing Up My Sleeve.");

          //set font style for the label and size

          lblBottom.setFont(Font.font("Cambria", 22));

          // to set the color to the label font

          lblBottom.setTextFill(Color.CHOCOLATE);

          //set label to the border pane

          root.setBottom(lblBottom);

          Scene scene = new Scene(root, 300, 250);

          primaryStage.setScene(scene);

          primaryStage.show();

     }

     public String randomName() {

          Random rdm = new Random();

          int low = 101;

          int high = 156;

          int result = rdm.nextInt(high - low) + low;

          System.out.println("file:img\\" + result + ".gif");

          return "file:img\\" + result + ".gif";

     }

     /**

     * @param args the command line arguments

     */

     public static void main(String[] args) {

          launch(args);

     }

}


Sample output screenshot:

Add a comment
Know the answer?
Add Answer to:
Java FX Application Purpose The purpose of this assignment is to get you familiar with the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java program that creates a photo album application.    Which will load a collection of images...

    Java program that creates a photo album application.    Which will load a collection of images and displays them in a album layout. The program will allow the user to tag images with metadata: •Title for the photo .      Limited to 100 characters. •Description for the photo . Limited to 300 characters. •Date taken •Place taken Functional Specs 1.Your album should be able to display the pictures sorted by Title, Date, and Place. 2.When the user clicks on a photo,...

  • How do you do this using visual studio on a Mac? You are tasked with creating...

    How do you do this using visual studio on a Mac? You are tasked with creating an application with five PictureBox controls. In the Poker Large folder, you will find JPEG image files for a complete deck of poker cards. Feel free to choose any card image that you like or be creative with this application. Each PictureBox should display a different card from the set of images. When the user clicks any of the PictureBox controls, the name of...

  • Write java program The purpose of this assignment is to practice OOP with Array and Arraylist,...

    Write java program The purpose of this assignment is to practice OOP with Array and Arraylist, Class design, Interfaces and Polymorphism. Create a NetBeans project named HW3_Yourld. Develop classes for the required solutions. Important: Apply good programming practices Use meaningful variable and constant names. Provide comment describing your program purpose and major steps of your solution. Show your name, university id and section number as a comment at the start of each class. Submit to Moodle a compressed folder of...

  • For this project your card class must include an Image of the card. Here is a...

    For this project your card class must include an Image of the card. Here is a zip filecontaining 52 images you can use for your cards. Please note that the name for each card can be created by the concatenation of the first character of the suit, the value of the card, and the ".png" suffix. When you construct a card, you should have it load its image. Alternatives exist regarding how to draw() the card. One way to do...

  • create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines...

    create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines and using try-catch-finally blocks in your methods that read from a file and write to a file, as in the examples in the lesson notes for reading and writing text files. Input File The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions...

  • in java : Create Java Application you are to create a project using our designated IDE...

    in java : Create Java Application you are to create a project using our designated IDE (which you must download to your laptop). Create the code to fulfill the requirements below. Demonstrate as stipulated below. Create a Main Application Class called AddressBookApplication (a counsole application / command line application). It should Contain a Class called Menu that contains the following methods which print out to standard output a corresponding prompt asking for related information which will be used to update...

  • in JAVA please and please show output!! Create a JavaFX application that simulates the rolling of...

    in JAVA please and please show output!! Create a JavaFX application that simulates the rolling of a pair of dice. When the user clicks a button, the application should generate two random numbers, each in the range of 1 through 6, to represent the value of the dice. Use ImageView component to display the dice. Six images are included in the project folder for you to use. For example, the first picture below is the initial window, after clicking the...

  • Java Thank you!! In this assignment you will be developing an image manipulation program. The remaining...

    Java Thank you!! In this assignment you will be developing an image manipulation program. The remaining laboratory assignments will build on this one, allowing you to improve your initial submission based on feedback from your instructor. The program itself will be capable of reading and writing multiple image formats including jpg, png, tiff, and a custom format: msoe files. The program will also be able to apply simple transformations to the image like: Converting the image to grayscale . Producing...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • In this assignment, you will create an application that holds a list of contact information. You ...

    In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list. MUST BE IN PYTHON FORMAT Create the folllowing objects: ContactsItem - attributes hold the values for the contact, including FirstName - 20 characters LastName - 20 characters Address...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT