Run Listing 14.9, p. 550 (in zipped NB project for Chapter 14). You\'ll need to
ID: 3845310 • Letter: R
Question
Run Listing 14.9, p. 550 (in zipped NB project for Chapter 14). You'll need to download the image file us.gif and add it to your project OR use the URL that you find in the code. Take a screenshot of the window and paste it into your document. Which method did you use to display the flag? Listing 14.9 displays an image in three image views, as shown in Figure 14.14. Listing 14.9 ShowImage. Java import javafx.application.Application: import javafx.scene.Scene: import javafx.scene.layout.HBox: import javafx.scene.layout.Pane: import javafx.geometry.Insets: import javafx.stage.Stage: import javafx.scene.image.Image: import javafx.scene.image.ImageView: public class ShowImage extends Application { @Override//Override the start method in the Application class public void start(Stage primaryStage) {//Create a pane to hold the image views creatw an HBox Pane pane = new HBox(10): pane.setPadding(new Insets(5, 5, 5, 5)): create an image Image image = new Image("image/us. gif"): add an image view to pane pane getChildren().add(new ImageView(image)): create an image view ImageView imageView2 = new ImageView(image): set image view properties imageView2.setFitHeight(100): imageView2.setFitWidth(100): pane.getChi1dren().add(i mageView2): add an image to ImageView imageView3 = new ImageView(image): create an image imageView3.setRotate(90): rotate an image pane.getChi1dren().add(imageView3): add an image to//Create a scene and place it in the stage Scene scene = new Scene(pane): primaryStage.setTitle("ShowImage");//Set the stage title primaryStage.setScene(scene);//Place the scene in the stage primaryStage.show();//Display the stage } } Modify Listing 14.14 to use three different styles. Change the text that is displayed to something of your own creation. Copy and paste a screenshot of the results into your document. The program creates an HBox (line 14). An HBox is a pane that places all nodes horizontally in one row. The program creates an Image, and then an ImageView for displaying the iamge, and places the ImageView in the HBox (line 17). The program creates the second ImageView (line 19), sets its fitHeight and fitWidth properties (lines 20-21) and places the ImageView into the HBox (line 22). The program creates the third ImageView (line 24), rotates it 90 degrees (line 25), and places it into the HBox (line 26). The setRotate method is defined in the Node class and can be used for any node. Note that an Image object can be shared by multiple nodes. In this case, it is shared by three ImageView. However, a node such as ImageView cannot be shared. You cannot place an ImageView multiple times into a pane or scene. Note that you must place the image file in the same directory as the class file, as shown in the following figure. If you use the URL to locate the image file, the URL protocal http://must be present. So the following code is wrong. new Image ("www.cs.armstrong.edu/liang/image/us.gif"): It must be replaced by new Image ("http;//www.cs.armstrong.edu/liang/image/us.gif");Explanation / Answer
//ShowImage.java
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class ShowImage extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new HBox();
pane.setPadding(new Insets(5, 5, 5, 5));
Image image = new Image("http://www.cs.armstrong.edu/liang/image/us.gif");
pane.getChildren().add(new ImageView(image));
ImageView imageView2 = new ImageView(image);
imageView2.setFitHeight(100);
imageView2.setFitWidth(100);
pane.getChildren().add(imageView2);
ImageView imageView3 = new ImageView(image);
imageView3.setRotate(90);
pane.getChildren().add(imageView3);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setTitle("ImageView Example");
primaryStage.show();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.