Question

//No JPanel please: Write a javafx application that displays an image and //plays a sound effect...

//No JPanel please: Write a javafx application that displays an image and
//plays a sound effect with each mouse click. Rotate through four images and
//five sound effects, so the image/sound effect pairing is different each time.
//What my problem is on is actually getting the pictures to switch. The x variable
//works perfect for the sound change but I am unsure what to do with y for the
//image change. Also I dont know if I should put this in some sort of pane or just
//a group. Please help thank you

package lab82;

import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.Group;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;

public class Lab82 extends Application
{
private AudioClip[] tunes;
private AudioClip current;
private ImageView[] pics;
private int x = 0;
private int y=0;

@Override
public void start(Stage primaryStage)throws Exception
{
File[] audioFiles = {new File("westernBeat.wav"),
new File("classical.wav"), new File("jeopardy.mp3"),
new File("eightiesJam.wav"), new File("newAgeRythm.wav"),
new File("lullaby.mp3"), new File("hitchcock.wav")};

ImageView[] pics={new ImageView("cat.jpg"),
new ImageView("deer.jpg"),
new ImageView("dog.jpg"),
new ImageView("gull.jpg")};

tunes = new AudioClip[audioFiles.length];
for (int i = 0; i < audioFiles.length; i++)
tunes[i] = new AudioClip(audioFiles[i].toURI().toString());
current = tunes[0];




Group root=new Group();
root.setStyle("-fx-background-color: skyblue");

Scene scene = new Scene(root, 500, 500);
scene.setOnMouseClicked(this::processMouseClick);

primaryStage.setTitle("Sounds and Images");
primaryStage.setScene(scene);
primaryStage.show();
}
public void processMouseClick(MouseEvent event)
{
tunes[x].stop();
x++;
tunes[x].play();



}



public static void main(String[] args)
{
launch(args);
}
}

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

Here is the completed code for this problem. Modified code is highlighted in bold text. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Make sure you have the images and audio clips in proper directories, with proper names.

// Lab82.java

package lab82;

import java.io.File;

import javafx.application.Application;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.input.MouseEvent;

import javafx.scene.Group;

import javafx.scene.layout.HBox;

import javafx.scene.layout.Pane;

import javafx.scene.media.AudioClip;

import javafx.stage.Stage;

public class Lab82 extends Application {

    private AudioClip[] tunes;

    private AudioClip current;

    private Image[] pics; //Images array (not imageviews)

    private int x = 0;

    private int y = 0;

    //you only need one ImageView

    ImageView imageView;

    @Override

    public void start(Stage primaryStage) throws Exception {

        File[] audioFiles = {new File("westernBeat.wav"),

            new File("classical.wav"), new File("jeopardy.mp3"),

            new File("eightiesJam.wav"), new File("newAgeRythm.wav"),

            new File("lullaby.mp3"), new File("hitchcock.wav")};

        //initializing images array. make sure you have these files in same directory

        //dont remove the prefix 'file:'

        pics = new Image[]{new Image("file:cat.jpg"),

            new Image("file:deer.jpg"),

            new Image("file:dog.jpg"),

            new Image("file:gull.jpg")};

        tunes = new AudioClip[audioFiles.length];

        for (int i = 0; i < audioFiles.length; i++) {

            tunes[i] = new AudioClip(audioFiles[i].toURI().toString());

        }

        current = tunes[0];

        //using image at index 0 as current image

        y = 0;

        //initializing image view with first image

        imageView = new ImageView(pics[y]);

        //using 500*500 preferred size for the image view

        imageView.setFitHeight(500);

        imageView.setFitWidth(500);

       

        //adding the imageview to an HBox and aligning to center

        HBox root = new HBox(imageView);

        root.setAlignment(Pos.CENTER);

        root.setStyle("-fx-background-color: skyblue");

        Scene scene = new Scene(root, 500, 500);

        scene.setOnMouseClicked(this::processMouseClick);

        primaryStage.setTitle("Sounds and Images");

        primaryStage.setScene(scene);

        primaryStage.show();

    }

    public void processMouseClick(MouseEvent event) {

        tunes[x].stop();

        //incrementing x and wrapping around from 0 if goes out of range

        x = (x + 1) % tunes.length;

        tunes[x].play();

        //incrementing y and wrapping around from 0 if goes out of range

        y = (y + 1) % pics.length;

        //using image at index y as new image for the imageView

        imageView.setImage(pics[y]);

    }

    public static void main(String[] args) {

        launch(args);

    }

}

Add a comment
Know the answer?
Add Answer to:
//No JPanel please: Write a javafx application that displays an image and //plays a sound effect...
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
  • Write a JavaFX application that displays 10,000 very small circles (radius of 1 pixel) in random...

    Write a JavaFX application that displays 10,000 very small circles (radius of 1 pixel) in random locations within the visible area. Fill the dots on the left half of the scene red and the dots on the right half of the scene green. Use the getWidth method of the scene to help determine the halfway point. This is what I have so far: import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.shape.Circle; import javafx.scene.paint.Color; import javafx.stage.Stage; import java.util.Random; import javafx.scene.Group; public class Class615...

  • 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,...

  • / Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import...

    / Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.media.AudioClip; import javafx.stage.Stage; public class JukeBox extends Application { private ChoiceBox<String> choice; private AudioClip[] tunes; private AudioClip current; private Button playButton, stopButton;    //----------------------- // presents an interface that allows the user to select and play // a tune from a drop down box //-----------------------   ...

  • JavaFX! Just need to fill several lanes of code please!!! CSE205 OOP and Data Structure Quiz #15 Last Name (print) First Name (print) Write a JavaFX GUI application program that simulates a timer. T...

    JavaFX! Just need to fill several lanes of code please!!! CSE205 OOP and Data Structure Quiz #15 Last Name (print) First Name (print) Write a JavaFX GUI application program that simulates a timer. The timer should show "count: the beginning and increase the number by 1 in every one second, and so on. o" at .3 A Timer - × A Timer Count: 0 Count: 1 (B) After 1 second, the GUI Window (A) Initial GUI Window According to the...

  • WRITE A JavaFX APPLICATION THAT DRAWS A PICTURE OF A FLOWER. Use thick lines for stalks...

    WRITE A JavaFX APPLICATION THAT DRAWS A PICTURE OF A FLOWER. Use thick lines for stalks and rotated ellipses for petals. Put the sky and the ground as background. Don’t worry artistic quality, it should look simple. It should take about 45 to 75 statements. Also, have some line-by-line documentation that says what is going on, e.g., // draw petal // draw ground // draw leaf The snowman program should be used as a GUIDE. Snowman code : import javafx.application.Application;...

  • Using JAVA FX how would I combine the two programs below into one interface that allows...

    Using JAVA FX how would I combine the two programs below into one interface that allows the user to pick which specific program they would like to play. They should be able to choose which one they want to play and then switch between them if necesary. All of this must be done in JAVA FX code Black jack javafx - first game program package application; import java.util.Arrays; import java.util.ArrayList; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.application.Application; import javafx.geometry.Pos; import javafx.geometry.Insets;...

  • ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button...

    ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button to a frame in the FXBookQuote2 program. When the user clicks the button, display the title of the book that contains the opening sentence or two from your favorite book in the label. FXBookQuote2.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...

  • The JavaFX framework provides more than one way to handle events. For event handlers, we could...

    The JavaFX framework provides more than one way to handle events. For event handlers, we could use inner classes, anonymous inner classes, or the new Java 8 feature of lambda expressions. In this discussion, you will explore these different ways of writing event handles in JavaFX. To prepare for this discussion, you must unzip the attached NetBeans project zip file (U4D1_HandleEvents.zip) and load it into your NetBeans IDE. The project uses an inner class to handle the click event on...

  • JavaFX program - Add a second level to the game. //Main game object/class package main; import java.util.ArrayList; impo...

    JavaFX program - Add a second level to the game. //Main game object/class package main; import java.util.ArrayList; import javafx.animation.AnimationTimer; import javafx.beans.binding.Bindings; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Label; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class Game extends Pane implements Runnable {       // instance variables    private ArrayList<MOB> mobs;    private ArrayList<Rectangle> hitBoxes;    private ArrayList<Treasure> treasure;    private Player player;    private final ImageView background = new ImageView();    private Level level;    private SimpleIntegerProperty score;    private...

  • please help me debug this Create a Future Value Calculator that displays error messages in labels.GUISpecificationsStart...

    please help me debug this Create a Future Value Calculator that displays error messages in labels.GUISpecificationsStart with the JavaFX version of the Future Value application presented in chapter 17. Create error message labels for each text field that accepts user input. Use the Validation class from chapter 17 to validate user input.Format the application so that the controls don’t change position when error messages are displayed. package murach.business; public class Calculation {        public static final int MONTHS_IN_YEAR =...

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