Question

In: Computer Science

Modify the AlienDirection program from this chapter so that the image is not allowed to move...

Modify the AlienDirection program from this chapter so that the image is not allowed to move out of the visible area of the window. Ignore any key that would allow this to happen.

*Ask if you have any questions about the assignment I will try to clarify

Textbook - JAVA FOUNDATIONS: INTRODUCTION TO PROGRAM DESIGN AND DATA STRUCTURES 5TH EDITION

Starter Code Provided :

import javafx.application.Application;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.input.KeyEvent;

import javafx.scene.paint.Color;

import javafx.stage.Stage;

public class AlienDirection extends Application
{
public final static int JUMP = 10;
  
private ImageView imageView;

//--------------------------------------------------------------------
// Displays an image that can be moved using the arrow keys.
//--------------------------------------------------------------------
public void start(Stage primaryStage)
{
Image alien = new Image("alien.png");
  
imageView = new ImageView(alien);
imageView.setX(20);
imageView.setY(20);
  
Group root = new Group(imageView);

Scene scene = new Scene(root, 400, 200, Color.BLACK);
scene.setOnKeyPressed(this::processKeyPress);

primaryStage.setTitle("Alien Direction");
primaryStage.setScene(scene);
primaryStage.show();
}
  
//--------------------------------------------------------------------
// Modifies the position of the image view when an arrow key is
// pressed.
//--------------------------------------------------------------------
public void processKeyPress(KeyEvent event)
{
switch (event.getCode())
{
case UP:
imageView.setY(imageView.getY() - JUMP);
break;
case DOWN:
imageView.setY(imageView.getY() + JUMP);
break;
case RIGHT:
imageView.setX(imageView.getX() + JUMP);
break;
case LEFT:
imageView.setX(imageView.getX() - JUMP);
break;
default:
break; // do nothing if it's not an arrow key
}
}
  
public static void main(String[] args)
{
launch(args);
}
}

Solutions

Expert Solution

Just comment or remove code from processKeyPress method so that image will not move


import javafx.application.Application;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.input.KeyEvent;

import javafx.scene.paint.Color;

import javafx.stage.Stage;

public class AlienDirection extends Application
{
    public final static int JUMP = 10;

    private ImageView imageView;

    //--------------------------------------------------------------------
// Displays an image that can be moved using the arrow keys.
//--------------------------------------------------------------------
    public void start(Stage primaryStage)
    {
        Image alien = new Image("alien.png");

        imageView = new ImageView(alien);
        imageView.setX(20);
        imageView.setY(20);

        Group root = new Group(imageView);

        Scene scene = new Scene(root, 400, 200, Color.BLACK);
        scene.setOnKeyPressed(this::processKeyPress);

        primaryStage.setTitle("Alien Direction");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    //--------------------------------------------------------------------
// Modifies the position of the image view when an arrow key is
// pressed.
//--------------------------------------------------------------------
    public void processKeyPress(KeyEvent event)
    {
        switch (event.getCode())
        {   
            case UP:
                
                break;
            case DOWN:
                break;
            case RIGHT:
                
                break;
            case LEFT:
                break;
            default:
                break; // do nothing if it's not an arrow key
        }
    }

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

Related Solutions

Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
~IMPORTANT: you are NOT allowed to modify public static void main.~ Complete and fix program, by...
~IMPORTANT: you are NOT allowed to modify public static void main.~ Complete and fix program, by defining a countOccurrences function, and by modifying the existing mostFrequentCharacter function, so as to satisfy the following specs: Function countOccurrences takes two arguments, a string and a character. It returns the number of times the character occurs in the string. Function mostFrequentCharacter takes a string as an argument. It returns the character that occurs the most times in the string. If multiple characters tie...
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
Modify the program so that after it reads the line typed on the keyboard, it replaces...
Modify the program so that after it reads the line typed on the keyboard, it replaces the ‘\n’ character with a NUL character. Now you have stored the input as a C-style string, and you can echo it with: Explain what you did. #include <unistd.h> #include <string.h> int main(void) { char aString[200]; char *stringPtr = aString; write(STDOUT_FILENO, "Enter a text string: ", strlen("Enter a text string: ")); // prompt user read(STDIN_FILENO, stringPtr, 1); // get first character while (*stringPtr !=...
C program 1.// rotate the values pointed to by three pointers// so values move from xp...
C program 1.// rotate the values pointed to by three pointers// so values move from xp to yp, yp to zp and zp to xpvoid rotate(int *xp, int *yp, int *zp)​ { return;} 2.// Write a function that returns 0 if x is 0, returns -1// if x < 0, returns 1 if x > 0// Your code must follow the Bit-Level Integer Coding Rules// on the textbook (located between hw 2.60 and 2.61).// You can assume w = 32.//...
[PYTHON] Modify the examaverages.py program included with this assignment so it will also compute the overall...
[PYTHON] Modify the examaverages.py program included with this assignment so it will also compute the overall average test grade. E.g if there are 3 test each student must take and the user enters the following set of test scores for the two students…: 30, 40, 50 for the first student 50, 60, 70 for the second student …then program will print the average for each student (i.e. 40 for the first student and 60 for the second student – the...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
In python....Modify the recursive Fibonacci program given in the textbook so that it prints tracing information....
In python....Modify the recursive Fibonacci program given in the textbook so that it prints tracing information. Specifically, have the function print a message when it is called and when it returns. For example, the output should contain lines like these: Computing fib(4) OR Leaving fib(4) returning 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT