Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am confused on how I would set up 6 different images. I have to stick to the 9

ID: 3727527 • Letter: I

Question

I am confused on how I would set up 6 different images. I have to stick to the 9 import classes I have. I need to set up six images to be used in an algorithm that that will show dice roll results. Basically, if a 3 and 5 are rolled then I will need my program to show a pic of 3 dice and 5 dice. How would I line up the images in the this driver program??? I am not asking for the algorithm, I just want help coding in the set up, if that makes since? The section of code I have in Bold is where I think I am to set up images, I am not sure if anything I have in bold is correct or on the right path?

Thank-you

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.text.Text;

import javafx.stage.Stage;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.FlowPane;

//----------------------------------------------------------------------

//

//----------------------------------------------------------------------

public class GUIDriver extends Application

{

private Text sumText;

private PigDice p;

private ImageView die1View, die2View;

//constructor

public void start(Stage primaryStage)

{

Image img = new Image("image1.jpg");

ImageView imgView = new ImageView(img);

  

  

Scene scene = new Scene(pane, 500, 350);

primaryStage.setTitle("Image Display");

primaryStage.setScene(scene);

primaryStage.show();

}

//Button Press

public void processButtonPress(ActionEvent event)

{

}

//

public void main(String args)

{

}

}

Explanation / Answer

I’ll explain you on how to set up all 6 images. As you have not provided PigDice class , I can just explain the logic to setup the images. First of all, you have to define an array of Image objects, outside all the methods, so that it can be accessed from anywhere within the program.

I have defined an array of Image objects under the ImageViews declaration.

private Text sumText;

private PigDice p;

private ImageView die1View, die2View;

//defining an array of images

private Image images[];

Then , inside the start method, initialize the array and fill with Image objects as shown below.

images=new Image[6];

images[0]=new Image("file:C:\Users\user\Documents\DieImages\die1.jpg");

images[1]=new Image("file:C:\Users\user\Documents\DieImages\die2.jpg ");

images[2]=new Image("file:C:\Users\user\Documents\DieImages\die3.jpg ");

images[3]=new Image("file:C:\Users\user\Documents\DieImages\die4.jpg ");

images[4]=new Image("file:C:\Users\user\Documents\DieImages\die5.jpg ");

images[5]=new Image("file:C:\Users\user\Documents\DieImages\die6.jpg ");

Note that it is important to specify the path of each images and keeping the prefix ‘file:’.

Also try to put two backslashes instead of one in the path name, or else the file may not be loaded.

After this, you can easily use these 6 images in your imageviews. I have provided an explanation in processButtonPress() method on how to set proper dice images in each draw

//Modified code for GUIDriver.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package dice;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.text.Text;

import javafx.stage.Stage;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.FlowPane;

//----------------------------------------------------------------------

//

//----------------------------------------------------------------------

public class GUIDriver extends Application {

    private Text sumText;

    private PigDice p;

    private ImageView die1View, die2View;

    //defining an array of images

    private Image images[];

    @Override

    public void start(Stage primaryStage) {

        /**

         * initializing the image array, and adding the URLs to each die images

         * from 1 to 6 Please note that it is important to keep the prefix

         * 'file:' before the file url

         */

        images = new Image[6];

        images[0] = new Image("file:C:\Users\user\Documents\DieImages\die1.jpg");

        images[1] = new Image("file:C:\Users\user\Documents\DieImages\die2.jpg ");

        images[2] = new Image("file:C:\Users\user\Documents\DieImages\die3.jpg ");

        images[3] = new Image("file:C:\Users\user\Documents\DieImages\die4.jpg ");

        images[4] = new Image("file:C:\Users\user\Documents\DieImages\die5.jpg ");

        images[5] = new Image("file:C:\Users\user\Documents\DieImages\die6.jpg ");

        die1View = new ImageView(images[3]);

        die2View = new ImageView(images[3]);

        FlowPane pane = new FlowPane();

        pane.getChildren().addAll(die1View, die2View);

        Scene scene = new Scene(pane, 500, 350);

        primaryStage.setTitle("Image Display");

        primaryStage.setScene(scene);

      primaryStage.show();

    }

//Button Press

    public void processButtonPress(ActionEvent event) {

        /**

         * You can write the logic to update the image views as per the dice

         * values

         */

        /**

         * if dice 1 rolls a 5, and dice 2 rolls a 3, you can update the

         * imageviews as die1View.setImage(images[5-1]); and

         * die2View.setImage(images[3-1]);

         */

    }

//

    public static void main(String[] args) {

        launch(args);

    }

}