Answered step by step
Verified Expert Solution
Question
1 Approved Answer
* * * JavaFX * * * * HELP So I need help with a couple things: I can't get the image from going off
JavaFXHELP So I need help with a couple things: I can't get the image from going off screen with the LEFTRIGHT buttons, but using the UP button is fine; The DOWN button pushes the GridPane down with the image once they meet; and it doesn't move continually with pressing the buttons even with the setOnMousePressed handler. I know I could encapsulate the EventHandler somehow but everything I've tried keeps giving me errors. Thanks!
import java.ioFileInputStream;
import java.ioFileNotFoundException;
import java.ioInputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
Modify your movecat assignment so that it moves the cat continually
when you hold down the directional buttons.
Images Must be stored in srcGraphics
Cat:
Image: BlackCat.png download above
Size: X
Starting Location:
Cat must stop at the edges of the screen or bottom of graphic panel. Must not disappear
Buttons:
Size: X
Location: Bottom Right
public class MovableCat extends Application
@Override
public void startStage primaryStage throws FileNotFoundException
InputStream catGraphic new FileInputStreamsrcGraphicsBlackCat.png;
Image cat new ImagecatGraphic;
ImageView viewableCat new ImageViewcat;
viewableCat.setFitHeight;
viewableCat.setFitWidth;
viewableCat.setX;
viewableCat.setY;
Button btUp new ButtonUP;
btUp.setPrefSize;
Button btDown new ButtonDOWN;
btDown.setPrefSize;
Button btLeft new ButtonLEFT;
btLeft.setPrefSize;
Button btRight new ButtonRIGHT;
btRight.setPrefSize;
GridPane gridPane new GridPane;
gridPane.setAlignmentPosBOTTOMRIGHT;
gridPane.setPaddingnew Insets;
gridPane.setHgap;
gridPane.setVgap;
gridPane.addbtUp;
gridPane.addbtDown;
gridPane.addbtLeft;
gridPane.addbtRight;
Pane pane new Pane;
pane.getChildrenaddviewableCat;
need to encapsulate eventhandler and create loop for continuous movement until button released
btUp.setOnAction e
ifviewableCatgetFitHeight pane.getHeight
viewableCat.setYviewableCatgetY;
else
viewableCat.setYviewableCatgetY;
;
btDown.setOnAction e
ifviewableCatgetFitHeight pane.getHeight
viewableCat.setYviewableCatgetY;
else
viewableCat.setYviewableCatgetY;
;
btLeft.setOnActionde
ifviewableCatgetFitWidth pane.getWidth
viewableCat.setXviewableCatgetX;
else
viewableCat.setXviewableCatgetX;
;
btRight.setOnActione
ifviewableCatgetFitWidth pane.getWidth
viewableCat.setXviewableCatgetX;
else
viewableCat.setXviewableCatgetX;
;
BorderPane borderPane new BorderPane;
borderPane.setToppane;
borderPane.setBottomgridPane;
Scene scene new SceneborderPane;
primaryStage.setTitleMovable Cat";
primaryStage.setScenescene;
primaryStage.show;
public static void mainString args
launchargs;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started