Question

1) ADD a button that says "Change Pictures" 2) WRITE some code to handle the button...

1) ADD a button that says "Change Pictures"

2) WRITE some code to handle the button event (in other words, when you press the button, this is the code that it goes to). In that code, change the pictures.

JAVA CODE:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
* This program demonstrates the GridPane layout container.
*/

public class GridPaneImages extends Application
{

//THESE ARE global (vs local) variables
// which means they can be used ANYWHERE in the program -- you want to use
// these in your button press code so that is why you are making them global!
ImageView pic1View, pic2View, pic3View, pic4View;


public static void main(String[] args)
{
// Launch the application.
launch(args);
}

@Override
public void start(Stage primaryStage)
{
// Create the Image components.
Image pic1 = new Image("file:Lemon.bmp");
Image pic2 = new Image("file:Banana.bmp");
Image pic3 = new Image("file:Pear.bmp");
Image pic4 = new Image("file:Strawberry.bmp");
  
// Create the ImageView components.
pic1View = new ImageView(pic1);
pic2View = new ImageView(pic2);
pic3View = new ImageView(pic3);
pic4View = new ImageView(pic4);

  
// Resize the moon image, preserving its aspect ratio.
pic1View.setFitWidth(200);
pic1View.setPreserveRatio(true);
  
// Resize the ship image, preserving its aspect ratio.
pic2View.setFitWidth(200);
pic2View.setPreserveRatio(true);

// Resize the sunset image, preserving its aspect ratio.
pic3View.setFitWidth(200);
pic3View.setPreserveRatio(true);
  
// Resize the flower image, preserving its aspect ratio.
pic4View.setFitWidth(200);
pic4View.setPreserveRatio(true);
  
// Create a GridPane.
GridPane gridpane = new GridPane();
  
// Add the ImageViews to the GridPane.
gridpane.add(pic1View, 0, 0); // Col 0, Row 0
gridpane.add(pic2View, 1, 0); // Col 1, Row 0
gridpane.add(pic3View, 0, 1); // Col 0, Row 1
gridpane.add(pic4View, 1, 1); // Col 1, Row 1
  
// Set the gap sizes.
gridpane.setVgap(10);
gridpane.setHgap(10);
  
// Set the GridPane's padding.
gridpane.setPadding(new Insets(30));
  
// Create a Scene with the GridPane as its root node.
Scene scene = new Scene(gridpane);
  
// Add the Scene to the Stage.
primaryStage.setScene(scene);
  
// Set the stage title.
primaryStage.setTitle("Images");
  
// Show the window.
primaryStage.show();
}


//TO DO :
// ADD A BUTTON AND THEN A BUTTON HANDLER that handles when you click on button
// In that handler code , CHANGE ALL THE PICTURES to a different fruit
// for instance, to change picture 4 to an apple:
// pic4View.setImage(new Image("file:Apple.bmp"));
}

Homework Answers

Answer #1

//Java Code

import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
* This program demonstrates the GridPane layout container.
*/

public class GridPaneImages extends Application
{

//THESE ARE global (vs local) variables
// which means they can be used ANYWHERE in the program -- you want to use
// these in your button press code so that is why you are making them global!
ImageView pic1View, pic2View, pic3View, pic4View;

Button btnAdd;
public static void main(String[] args)
{
// Launch the application.
launch(args);
}

@Override
public void start(Stage primaryStage)
{
// Create the Image components.
Image pic1 = new Image("file:Lemon.bmp");
Image pic2 = new Image("file:Banana.bmp");
Image pic3 = new Image("file:Pear.bmp");
Image pic4 = new Image("file:Strawberry.bmp");

// Create the ImageView components.
pic1View = new ImageView(pic1);
pic2View = new ImageView(pic2);
pic3View = new ImageView(pic3);
pic4View = new ImageView(pic4);


// Resize the moon image, preserving its aspect ratio.
pic1View.setFitWidth(200);
pic1View.setPreserveRatio(true);

// Resize the ship image, preserving its aspect ratio.
pic2View.setFitWidth(200);
pic2View.setPreserveRatio(true);

// Resize the sunset image, preserving its aspect ratio.
pic3View.setFitWidth(200);
pic3View.setPreserveRatio(true);

// Resize the flower image, preserving its aspect ratio.
pic4View.setFitWidth(200);
pic4View.setPreserveRatio(true);

// Create a GridPane.
GridPane gridpane = new GridPane();

// Add the ImageViews to the GridPane.
gridpane.add(pic1View, 0, 0); // Col 0, Row 0
gridpane.add(pic2View, 1, 0); // Col 1, Row 0
gridpane.add(pic3View, 0, 1); // Col 0, Row 1
gridpane.add(pic4View, 1, 1); // Col 1, Row 1

// Set the gap sizes.
gridpane.setVgap(10);
gridpane.setHgap(10);
//TO DO :
// ADD A BUTTON AND THEN A BUTTON HANDLER that handles when you click on button

// In that handler code , CHANGE ALL THE PICTURES to a different fruit
// for instance, to change picture 4 to an apple:
// pic4View.setImage(new Image("file:Apple.bmp"));
btnAdd = new Button("Change Pictures");
gridpane.add(btnAdd,1,2);
//Add handler
btnAdd.setOnAction(event -> {
pic1View.setImage(new Image("file:Orange.bmp"));
pic2View.setImage(new Image("file:Melon.bmp"));
pic3View.setImage(new Image("file:Grapes.bmp"));
pic4View.setImage(new Image("file:Apple.bmp"));
});

// Set the GridPane's padding.
gridpane.setPadding(new Insets(30));

// Create a Scene with the GridPane as its root node.
Scene scene = new Scene(gridpane);

// Add the Scene to the Stage.
primaryStage.setScene(scene);

// Set the stage title.
primaryStage.setTitle("Images");

// Show the window.
primaryStage.show();
}

}

//Output

//If you need any help regarding this solution... please leave a comment... thanks

Know the answer?
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
Similar Questions
package Week7_Quiz; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.control.Label;
package Week7_Quiz; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.control.Label; import javafx.stage.Stage; public class Week7_Quiz extends Application { private TextField tf1; private Label stckr; private int revrsdNum; private Button btn; int num = 0; int reverse; public static void main(String[] args) {       launch (args); } @Override public void start(Stage stage) throws Exception { tf1 = new TextField(); tf1.setLayoutX(10); tf1.setLayoutY(50);    stckr =new Label ("Result: "); stckr.setLayoutX(12); stckr.setLayoutY(100);    btn = new Button("Reverse");...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
Coding in Python Add radio button options for filing status to the tax calculator program of...
Coding in Python Add radio button options for filing status to the tax calculator program of Project 1. The user selects one of these options to determine the tax rate. The Single option’s rate is 20%. The Married option is 15%. The Divorced option is 10%. The default option is Single. Be sure to use the field names provided in the comments in your starter code. ================== Project 1 code: # Initialize the constants TAX_RATE = 0.20 STANDARD_DEDUCTION = 10000.0...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
1) add toString method to BankAccount and SavingsAccount classes 2) Override the withdraw() in SavingsAccount so...
1) add toString method to BankAccount and SavingsAccount classes 2) Override the withdraw() in SavingsAccount so that it will not withdraw more money than is currently in the account. 3) Provide constructors for SavingsAccount 4) Add this feature to SavingsAccount: If you withdraw more than 3 times you are charged $10 fee and the fee is immediately withdrawn from your account.once a fee is deducted you get another 3 free withdrawals. 5) Implement the Comparable Interface for SavingsAccount based on...
1. Paste the code below into your Java IDE and make sure it runs correctly. 2....
1. Paste the code below into your Java IDE and make sure it runs correctly. 2. Create a program that receives numeric data from the user and then adds it to a queue. 3. Create a program that receives text based data and then adds it to a queue. 4. Create a program that automatically populates a queue on start up with 5 pieces of data and then displays each entry in the queue after receiving a command from the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT