![]() |
DrJava Accel/Velocity
Hello sorry if this is a stupid question, but I am a beginner programmer learning the basics im taking intro to comp sci. One of our assignements is to make a ball in a window go up to the top of the frame and then down to the bottom right.
The only part of my code I need to change is adjusting the velocity and acceleration of the ball. The values he gives us to start(which are wrong on purpose) were 0.001 for accel and -.5 for velocity. You don't have to do my homework but I would appreciate getting a little hint on how to start to find the values. It seems as if it should be simple but I dont know the way of easily getting those values corrected. Here is the code if interested in looking(I made the part I need to change in bold/underlined: import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.text.*; import java.util.*; import java.util.List; // resolves problem with java.awt.List and java.util.List /** * A class that represents a picture. This class inherits from * SimplePicture and allows the student to add functionality to * the Picture class. * * Copyright Georgia Institute of Technology 2004-2005 * @author Barbara Ericson ericson@cc.gatech.edu */ public class Picture extends SimplePicture { ///////////////////// constructors ////////////////////////////////// /** * Constructor that takes no arguments */ public Picture () { /* not needed but use it to show students the implicit call to super() * child constructors always call a parent constructor */ super(); } /** * Constructor that takes a file name and creates the picture * @param fileName the name of the file to create the picture from */ public Picture(String fileName) { // let the parent class handle this fileName super(fileName); } /** * Constructor that takes the width and height * @param width the width of the desired picture * @param height the height of the desired picture */ public Picture(int width, int height) { // let the parent class handle this width and height super(width,height); } /** * Constructor that takes a picture and creates a * copy of that picture */ public Picture(Picture copyPicture) { // let the parent class do the copy super(copyPicture); } /** * Constructor that takes a buffered image * @param image the buffered image to use */ public Picture(BufferedImage image) { super(image); } ////////////////////// methods /////////////////////////////////////// /** * Method to return a string with information about this picture. * @return a string with information about the picture such as fileName, * height and width. */ public String toString() { String output = "Picture, filename " + getFileName() + " height " + getHeight() + " width " + getWidth(); return output; } public static void main(String[] args) { //String fileName = FileChooser.pickAFile(); //Picture pictObj = new Picture(fileName); //pictObj.explore(); Picture ballFlight = new Picture(800,600); //Purpose: Make a new Picture to draw the ball flight upon, and store //a reference to that picture in a new variable ballFlight, so Picture //methods may be called on that Picture in the future. Also, specify //the new Picture's width and height in Pixel units. double yInitVel;//Purpose: Make a variable for the initial velocity. double yInitPos;//(1)Purpose: Make a variable for the initial position. double yAccel;//Purpose: Make a variable for the downward acceleration. yAccel = .001; //Purpose:Specify the value of acceleration to use. yInitVel = -.5;//Purpose:Specify the value of velocity in the down direction. yInitPos = 300;//Purpose:Specify the initial position in the down direction. double time;//Purpose:Make a variable to keep track of time. time = 0.0;//Purpose:Program that time starts at zero. while(time < 800)//purpose:Plot images of the ball as long as time is //less than the simulation period. { double xPos;//Purpose:Make a variable for calculating current x position. double yPos;//Purpose:(2)Make a variable for calcuating the current y position. xPos = 1.0*time; //Purpose:(3)Makes the current xPos variable equal to the current time. yPos = (0.5*yAccel*time*time) + yInitVel*time + yInitPos; //Purpose:(4)Makes the current y position equal to half the acceleration multiplied by the //current time squared added to the sum of initial velocity multiplied by time and //the initial position value System.out.println("xPos=" + xPos + "yPos=" + yPos); //Purpose:Enable you to see the current positions in numeric form. Pixel pix = ballFlight.getPixel((int)xPos, (int) yPos); //Purpose:Retrieve a Pixel near the current position, and make a variable //you can use to refer to it, so you can call that Pixel's methods. pix.setRed(0); //Purpose:Set that Pixel's red intensity to zero. pix.setGreen(0); //Purpose:Set that Pixel's green intensity to zero. pix.setBlue(0); //Purpose: Set that Pixel's blue intensity to zero. //(5)The purpose of the above statements is to make //the pixels color equal to black since black. time = time + 1.0; //Purpose:AFTER plotting the ball's position at the current time, calculate //and write in the time variable //(6)Calculate and write in the time variable AFTER plotting the ball's position at the current time. } ballFlight.explore(); //Purpose:(7)Look up the file "ballFlight" } } // this } is the end of class Picture, put all new methods before this |
| All times are GMT +1. The time now is 01:41 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.