javafx Modify the the following source code so that when the mouse is clicked wi
ID: 3695530 • Letter: J
Question
javafx
Modify the the following source code so that when the mouse is clicked within 30 pixels of the middle of the smiley face it changes color. It will alternate between the yellow and red smiley faces.
two images have been given to you , redSmiley.gif and happyFace.gif to get started.
Just for fun:
Create more than 2 colors of smiley faces and rotate through the images changing each time the mouse is clicked within the threshold.
Use different images other than smiley faces, change images each time the mouse is clicked within the threshold.
Add a sound each time the image changes.
Have the image slow down, if it has been more than x number of seconds since the image has changed.
Explanation / Answer
import javafx.application.Application; import javafx.stage.Stage; import javafx.animation.AnimationTimer; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Rectangle2D; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.Lighting; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; import javafx.util.Duration; public class ReboundImageOriginal extends Application { //main timeline private AnimationTimer timer; private ImageView image; private int x,y,moveX,moveY; final int HEIGHT = 300; final int WIDTH = 500; final int IMAGE_SIZE = 35; //variable for storing actual frame private Integer i=0; @Override public void start(Stage stage) { Group p = new Group(); Scene scene = new Scene(p); stage.setScene(scene); stage.setWidth(550); stage.setHeight(400); p.setTranslateX(0); p.setTranslateY(20); x = 0; y = 0; moveX=moveY=3; //create an ImageView object Rectangle rectangle = new Rectangle(0,0,550,350); image = new ImageView(new Image("happyFace.gif")); //create a layout for the image final StackPane stack = new StackPane(); stack.getChildren().addAll(image); stack.setLayoutX(30); stack.setLayoutY(30); p.getChildren().add(rectangle); p.getChildren().add(stack); stage.show(); //You can add a specific action when each frame is started. timer = new AnimationTimer() { @Override public void handle(long l) { x += moveX; y += moveY; if (x = WIDTH) moveX = moveX * -1; if (y = HEIGHT) moveY = moveY * -1; stack.setLayoutX(x); stack.setLayoutY(y); } }; timer.start(); } public static void main(String[] args) { Application.launch(args); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.