Question

Java...Stay on the Screen! Animation in video games is just like animation in movies – it’s...

Java...Stay on the Screen! Animation in video games is just like animation in movies – it’s drawn image by image (called “frames”). Before the game can draw a frame, it needs to update the position of the objects based on their velocities (among other things). To do that is relatively simple: add the velocity to the position of the object each frame. For this program, imagine we want to track an object and detect if it goes off the left or right side of the screen (that is, it’s X position is less than 0 and greater than the width of the screen, say, 100). Write a program that asks the user for the starting X and Y position of the object as well as the starting X and Y velocity, then prints out its position each frame until the object moves off of the screen. Design Java(source code) for this program.

Sample run 1: Enter the starting X position: 50

Enter the starting Y position: 50

Enter the starting X velocity: 4.7

Enter the starting Y velocity: 2

X:50 Y:50

X:54.7 Y:52

X:59.4 Y:54

X:64.1 Y:56

X:68.8 Y:58

X:73.5 Y:60

X:78.2 Y:62

X:82.9 Y:64

X:87.6 Y:66

X:92.3 Y:68

X:97 Y:70

X:101.7 Y:72

Sample run 2: Enter the starting X position: 20

Enter the starting Y position: 45

Enter the starting X velocity: -3.7

Enter the starting Y velocity: 11.2

X:20 Y:45

X:16.3 Y:56.2

X:12.6 Y:67.4

X:8.9 Y:78.6

X:5.2 Y:89.8

X:1.5 Y:101

X:-2.2 Y:112.2  

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

ScreenPositionXY.java :

//import package
import java.util.*;
//java class
public class ScreenPositionXY {
   public static void main(String[] args) {
       //creating object of Scanner class
       Scanner sc=new Scanner(System.in);
       //starting X position
       System.out.print("Enter the starting X position: ");
       double startingX=sc.nextDouble();//reading X
       //starting Y position
       System.out.print("Enter the starting Y position: ");
       double startingY=sc.nextDouble();//reading Y
       //starting X velocity
       System.out.print("Enter the starting X velocity: ");
       double vcX=sc.nextDouble();//X velocity
       //starting y velocity
       System.out.print("Enter the starting Y velocity: ");
       double vcY=sc.nextDouble();//reading Y velocity
       //using while loop
       while(startingX>0 && startingX<=100)
       {
           //print X position
           System.out.printf("X:%.1f",startingX);
           //print X position
           System.out.printf(" Y:%.1f\n",startingY);
           //increment startingX by vcX
           startingX=startingX+vcX;
           //increment startingY by vcY
           startingY=startingY+vcY;
       }
       System.out.printf("X:%.1f",startingX);//print X position
       System.out.printf(" Y:%.1f",startingY);//print Y position
          
   }


   }
=========================

Output 1:

Console X Debug Shell 4 x 8 E F G H E • F <terminated> Screen Position XY [Java Application] C:\Program FilesVava\jdk1.8.0_18

Output 2:

Console X Debug Shell | x | = b = = = = = = = <terminated> ScreenPositionXY [Java Application] C:\Program Files Vava\jdk1.8.0

Add a comment
Know the answer?
Add Answer to:
Java...Stay on the Screen! Animation in video games is just like animation in movies – it’s...
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
  • IN JAVA PLEASE!!! Program 1: Stay on the Screen! Animation in video games is just like...

    IN JAVA PLEASE!!! Program 1: Stay on the Screen! Animation in video games is just like animation in movies – it’s drawn image by image (called “frames”). Before the game can draw a frame, it needs to update the position of the objects based on their velocities (among other things). To do that is relatively simple: add the velocity to the position of the object each frame. For this program, imagine we want to track an object and detect if...

  • ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*;...

    ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { public static final int DELAY = 50; // delay between repaints in millis private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save"; private static String TARGET_IMAGE_FILE_NAME = null; private static final boolean PRETTY = true; // true to anti-alias private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file private int width, height; // dimensions...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

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
ADVERTISEMENT