Program Plan:
- Import required packages.
- Declare a main class named “ch14_1” which extends the “Application” class.
- Declare a “start ()” method which overrides the “start ()” method in the “Application” class. Inside this method,
- Create a GridPane in order to display images.
- Place the pane in center position using “setAlignment ()” method.
- Set the horizontal and vertical gap for the pane using “setHgap ()” and “setVgap ()” functions respectively.
- Create four ImageView objects to display all the images from the file.
- Add all the four images on their respective position on the pane.
- Create a scene and place it on the stage.
- Set the title as “Exercise14_01”.
- Display the stage on the window using “primaryStage.show()” method.
- Declare a main method using “public static main”.
- Launch the method using “launch ()” method.
- Declare a “start ()” method which overrides the “start ()” method in the “Application” class. Inside this method,
The below program displays four images on the GridPane as per the given image.
Explanation of Solution
Program:
//Import required packages
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.ImageView;
//Main class extends Application
public class ch14_1 extends Application
{
//Overrides the start method in the application
@Override
//start method
public void start(Stage primaryStage)
{
//Create a GridPane to display images
GridPane GP = new GridPane();
//Place the pane in center position
GP.setAlignment(Pos.CENTER);
/*set the horizontal and vertical gap for the pane*/
GP.setHgap(5);
GP.setVgap(5);
/*Create 4 image views to display 4 images and pass the url from the file name*/
ImageView IV1 = new ImageView("snaps/germany.gif");
ImageView IV2 = new ImageView("snaps/china.gif");
ImageView IV3 = new ImageView("snaps/fr.gif");
ImageView IV4 = new ImageView("snaps/us.gif");
/*Add image to the pane in the first row, first column*/
GP.add(IV1, 0, 0);
/*Add image to the pane in the second row, first column*/
GP.add(IV2, 1, 0);
/*Add image to the pane in the first row, second column*/
GP.add(IV3, 0, 1);
/*Add image to the pane in the second row, second column*/
GP.add(IV4, 1, 1);
//Create a scene and place it on the stage
Scene scene = new Scene(GP);
//Setting title
primaryStage.setTitle("Exercise14_01");
//Place the scene on the stage
primaryStage.setScene(scene);
//Display the stage on the window
primaryStage.show();
}
//Main method
public static void main(String[] args)
{
//Launch the application
launch(args);
}
}
Screenshot of the output
Want to see more full solutions like this?
Chapter 14 Solutions
Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
- 8. A game is played by moving a game piece left or right along a horizontal game board. The board consists of spaces of various colors, as shown. The circle represents the initial location of the game piece. Yellow Black Green Green Red Yellow Black Black Yellow Black The following algorithm indicates how the game is played. The game continues until the game is either won by landing on the red space or lost when the piece moves off either end of the board. Step 1: Place a game piece on a space that is not red and set a counter to 0. Step 2: If the game piece is on a yellow space, move the game piece 3 positions to the left and go to step 3. Otherwise, if the game piece is on a black space, move the game piece 1 position to the left and go to step 3. Otherwise, if the game piece is on a green space, move the game piece 2 positions to the right and go to step 3. Step 3: Increase the value of the counter by 1. Step 4: If game piece is on the red space or moved off the end of the game…arrow_forwardjava scriptarrow_forwardChapter 5. PC #17. Rock, Paper, Scissors Game (page 317) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 2. When the program begins, a random number in the range of 0 through 2 is generated. If the number is 0, then the computer has chosen rock. If the number is 1, then the computer has chosen paper. If the number is 2, then the computer has chosen scissors. (Do not display the computer choice yet.) 3. The user enters his or her choice of "rock", "paper", or "scissors" at the keyboard. You should use 1 for rock, 2 for paper, and 3 for scissors. Internally, you can store 0, 1, and 2 as the user choice, to match with the above schema. 4. Both user and computer choices are displayed. 5. A winner is selected according to the following rules: If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.) If one player chooses scissors and…arrow_forward
- Revise Exercise 6.28 (Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows:Roll two dice. Each die has six faces representing values 1, 2, ..., and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win.Your program acts as a single player.)to run it 10,000 times and display the number of winning games.arrow_forward5.7 LAB: Palindrome A palindrome is a word or a phrase that is the same when read both forward and backward Examples are: bob, sees, or never odd or even (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome Ex: If the input is: bob the output is: bob is a palindrome Ex: If the input is: bobby the output is: bobby is not a palindrome Hint: Start by removing spaces. Then check if a string is equivalent to it's reverse. 450052196680037 LAB ACTIVITY 5.7.1: LAB: Palindrome 1 word=input("Enter a word or phrase: ").strip().lower 2 3 without_space= word.replace("","") 4 5 if without_space=without_space[::-1]: print (word, "is a palindrome") print (word, "is not a palindrome") 6 7 8 9 main.py else: I 0/10 Load default template...arrow_forwardJava scriptarrow_forward
- A1.2 Bean MachineThe bean machine is a device for statistical experiments. It consists of an upright board with evenly spaced nails (or pegs) in a triangular form,.Balls are dropped from the opening at the top of the board. Every time a ball hits a nail, it has a 50% chance of falling to the left or to the right. The piles of balls are accumulated in the slots at the bottom of the board.Write a program to simulate the bean machine that has 8 slots as shown in the figure. Your program should prompt the user to enter the number of balls to drop. Simulate the falling of each ball by printing its path. For example, the path for the ball in LLRRLLR and the path for the ball in RLRRLRR. Note that there are 7 levels of nails, so your path should be 7 letters (not 8).Create an array called slots. Each element in slots store the number of balls in a slot. Each ball falls into a slot via a path. The number of “R”s in a path is the position of the slot where the ball falls. For example, for the…arrow_forwardMoon effect. Some people believe that the Moon controls their activities. If the Moon moves from being directly on the opposite side of Earth from you to being directly overhead, by what percentage does (a) the Moon's gravitational pull on you increase and (b) your weight (as measured on a scale) decrease? Assume that the Earth-Moon (center-to-center) distance is 3.82 x 10° m, Earth's radius is 6.37 x 106 m, Moon's mass is 7.36 x 1022 kg, and Earth's mass is 5.98 x 1024 kg. (a) Number 6.89 Units percent (b) Number i 6.8713e-4 Units percentarrow_forward7.9 LAB: Palindrome A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome. Ex: If the input is: bob the output is: bob is a palindrome Ex: If the input is: bobby the output is: bobby is not a palindrome Hint: Start by removing spaces. Then check if a string is equivalent to it's reverse. Use Python, please.arrow_forward
- 7.5 LAB: Name format Many documents use a specific format for a person's name. Write a program whose input is: firstName middleName lastName and whose output is: lastName, firstInitial.middleInitial. Ex: If the input is: Pat Silly Doe the output is: Doe, P.S. If the input has the form: firstName lastName the output is: lastName, firstInitial. Ex: If the input is: Julia Clark the output is: Clark, J. I'm taking a class on python and the lab assignment asks... Many documents use a specific format for a person's name. Write a program whose input is: firstName middleName lastName and whose output is: lastName, firstInitial.middleInitial. Ex: If the input is: Pat Silly Doe the output is: Doe, P.S. If the input has the form: firstName lastName the output is: lastName, firstInitial. Ex: If the input is: Julia Clark the output is: Clark, J. So far I have... firstName = input() middleName = input() lastName = input() lastName2 = input() firstName2 = input() print(lastName+ ',',…arrow_forwardLASTarrow_forwardExercise 1.1 : Base and ASCII representation of character representation Print the decimal, octal and hexadecimal value of all characters between the start ad stop characters entered by a user. for example, if the user enters an 'A' and 'H', the program should print all the characters between 'A' and 'H' and their respective values in the different bases (decimal, octal, and hexadecimal) as follows ASCII Representation ofD is 68 in Decimal 104 in Octal and ASCII Representation of E is 69 in Decimal 105 in Octal and ASCII Representation of F is 70 in Decimal ASCII Representation of G is 71 in Decimal 107 in Octal and ASCII Representation of H is 72 in Decimal 110 in Octal and ASCII Representation of I is 73 in Decimal ASCII Representation of J is 74 in Decimal 112 in Octal and ASCII Representation of K is 75 in Decimal 113 in Octal and 44 in Hexadecimal 45 in Hexadecimal 106 in Octal and 46 in Hexadecimal 47 in Hexadecimal 48 in Hexadecimal 111 in Octal and 49 in Hexadecimal 4A in…arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr