Results 1 to 3 of 3
  1. #1
    Shea6892 is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Help with DrawingPanel

    For a homework assignment I have to alter some code to plot points of the calculations of a projectile's trajectory in the Drawing Panel but I can't seem to get any ideas on how to do this as I am reading around and in my book. I would appreciate anybody who can help thanks. Here is the code at the moment:

    Java Code:
    import java.awt.*;
    import java.util.*;
    
    public class ProjectileGraph {
    	public static final double ACCELERATION = -9.81;
    	public static void main(String[] args){
    		DrawingPanel panel = new DrawingPanel(420, 220);
    		Graphics g = panel.getGraphics();
    		Scanner console = new Scanner(System.in);
    		
    	System.out.println("Velocity (meters/second)? ");
    		double velocity = console.nextDouble();
    	System.out.println("Angle (degrees)? ");
    		double angle = Math.toRadians(console.nextDouble());
    	System.out.println("Number of steps to display? ");
    		int steps = console.nextInt();
    	System.out.println();
    	
    	double xVelocity = velocity * Math.cos(angle);
    	double yVelocity = velocity * Math.sin(angle);
    	double totalTime = -2.0 * yVelocity / ACCELERATION;
    	double timeIncrement = totalTime / steps;
    	double xIncrement = xVelocity * timeIncrement;
    	
    	double x = 0.0;
    	double y = 0.0;
    	double t = 0.0;
    	
    	for(int i = 1; i <= steps; i++){
    		t += timeIncrement;
    		x += xIncrement;
    		y = yVelocity * t + 0.5 * ACCELERATION * t * t;
    		System.out.println(i + "\t" + round2(x) + "\t" + round2(y) + "\t" + round2(t));
    	
    	}
    	}
    	public static double round2(double n) {
    		return Math.round(n * 100.0) / 100.0;
    	
    
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with DrawingPanel

    We have no idea what your DrawingPanel class is like, so I'm not sure how we can be able to help you. Also, if this is Swing or AWT, have you been told specifically by your instructor to call the getGraphics() method?

  3. #3
    Shea6892 is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help with DrawingPanel

    Quote Originally Posted by Fubarable View Post
    We have no idea what your DrawingPanel class is like, so I'm not sure how we can be able to help you. Also, if this is Swing or AWT, have you been told specifically by your instructor to call the getGraphics() method?
    Ahhh sorry still very new to java and adding source files and what not. It is AWT and the reference book which the code is out of calls for the getGraphics() method in all the similar example programs but none of them are quite like this where it has to input the calculations as the plotted points, and am not sure what to use(as far as the drawLine,drawRect, etc. methods) because I didn't see a plot point method. But this is the DrawingPanel class I have been told to use and if you can help thanks a ton but if not thanks for the reply.


    Edit: mistake in code. I guess the java class is too many characters to post even in code tags. It is from http://www.buildingjavaprograms.com/

    Link to code is: www.buildingjavaprograms.com/DrawingPanel.java
    Last edited by Shea6892; 01-20-2012 at 04:33 AM.

Similar Threads

  1. problem in DrawingPanel
    By Nes_java in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •