Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* * * JavaFX * * * * HELP So I need help with a couple things: I can't get the image from going off

***JavaFX****HELP So I need help with a couple things: I can't get the image from going off screen with the LEFT/RIGHT 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.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
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 src/Graphics
Cat:
Image: Black_Cat.png (download above)
Size: 100 X100
Starting Location: 100,100
Cat must stop at the edges of the screen or bottom of graphic panel. (Must not disappear)
Buttons:
Size: 100 X 50
Location: Bottom Right*/
public class MovableCat extends Application{
@Override
public void start(Stage primaryStage) throws FileNotFoundException{
InputStream catGraphic = new FileInputStream("src/Graphics/Black_Cat.png");
Image cat = new Image(catGraphic);
ImageView viewableCat = new ImageView(cat);
viewableCat.setFitHeight(100);
viewableCat.setFitWidth(100);
viewableCat.setX(100);
viewableCat.setY(100);
Button btUp = new Button("UP");
btUp.setPrefSize(100,50);
Button btDown = new Button("DOWN");
btDown.setPrefSize(100,50);
Button btLeft = new Button("LEFT");
btLeft.setPrefSize(100,50);
Button btRight = new Button("RIGHT");
btRight.setPrefSize(100,50);
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.BOTTOM_RIGHT);
gridPane.setPadding(new Insets(2,2,2,2));
gridPane.setHgap(2);
gridPane.setVgap(2);
gridPane.add(btUp,2,0);
gridPane.add(btDown,2,6);
gridPane.add(btLeft,0,5);
gridPane.add(btRight,5,5);
Pane pane = new Pane();
pane.getChildren().add(viewableCat);
//need to encapsulate eventhandler and create loop for continuous movement until button released
btUp.setOnAction( e->{
if(viewableCat.getFitHeight()!= pane.getHeight()){
viewableCat.setY(viewableCat.getY()-10);
}
else{
viewableCat.setY(viewableCat.getY()+10);
}
});
btDown.setOnAction( e->{
if(viewableCat.getFitHeight()!= pane.getHeight()){
viewableCat.setY(viewableCat.getY()+10);
}
else{
viewableCat.setY(viewableCat.getY()-10);
}
});
btLeft.setOnActiond(e ->{
if(viewableCat.getFitWidth()!= pane.getWidth()){
viewableCat.setX(viewableCat.getX()-10);
}
else{
viewableCat.setX(viewableCat.getX()+10);
}
});
btRight.setOnAction(e ->{
if(viewableCat.getFitWidth()!= pane.getWidth()){
viewableCat.setX(viewableCat.getX()+10);
}
else{
viewableCat.setX(viewableCat.getX()-10);
}
});
BorderPane borderPane = new BorderPane();
borderPane.setTop(pane);
borderPane.setBottom(gridPane);
Scene scene = new Scene(borderPane,800,600);
primaryStage.setTitle("Movable Cat");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago