Results 1 to 2 of 2
  1. #1
    af476 is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Exclamation Projectile Motion Problem

    So I have created this Jframe and Jpanel that contains an image(which is an imageicon for a JLabel), and this image is supposed to fly in a parabolic-projectile trajectory by clicking on it, dragging , and releasing it somewhere on the screen. Then it will simply continue its track in a parabola with only its weight taken into consideration(no air resistance)
    Here is the code:

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    import java.awt.BorderLayout;
    import java.awt.Cursor;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.lang.Math;
    import java.lang.System;
    import java.util.Timer;
    
    
    public class Frame extends JFrame implements ActionListener {
    
    	private JPanel mainPanel;
    	private JLabel title;
    	private JLabel speedyholder;
    	private int xpos=50,ypos=300,width=40,height=40;
    	private int p1x,p1y,p2x,p2y;
    	double dp1p2;
    	double timepressed,timereleased;
    	double mtopixels= 3779.527559;
    	double g= 9.8*mtopixels;
    	// 1 pixel = 0.026458333 cm = 0.0002645833 m
    	
    	public Frame(){
    		Simulation();
    		mainPanel.addMouseListener(new MouseHandler());
    		mainPanel.addMouseMotionListener(new MouseHandler());
    	}
    	private void Simulation(){
    		
    		
    		
    		mainPanel = new JPanel();
    		title = new JLabel();
    		speedyholder = new JLabel();
    		speedyholder.setBounds(xpos, ypos, width, height);
    		mainPanel.setLayout(null);
    		mainPanel.setBackground(new java.awt.Color(255, 255, 255));
    		
    		title.setFont(new java.awt.Font("Rockwell", 1, 18));
    		title.setText("Speedy Training Session");
    		title.setBounds(250,10,230,40);
    		speedyholder.setIcon(new javax.swing.ImageIcon(getClass().getResource("speedy.png")));
    		mainPanel.add(title);
    		mainPanel.add(speedyholder);
    		add(mainPanel);
    		
    		
    	
    		
    		pack();
    	}
    	private double getRange() {
    		double R=(2*getSpeed()*getSpeed()*Math.sin(getTheta())*Math.cos(getTheta()))/g;
    		return R;
    		
    	}
    	private double getMaximumHeight() {
    		double H= (getSpeed()*getSpeed()*Math.sin(getTheta())*Math.sin(getTheta()))/(2*g);
    		return H;
    		
    	}
    	private double getTheta() {
    		
    		double thetaRadians= Math.atan((double)(Math.abs(p1y-p2y))/(double)(p2x-p1x));
    		return thetaRadians;
    		
    	}
    	private double getSpeed() {
    		dp1p2=  (long)(Math.sqrt(Math.pow(p2x - p1x,2) +  Math.pow(p1y-p2y, 2)));
    		double speed= (dp1p2)/((timereleased-timepressed)*Math.pow(10, -3));
    		return speed;
    		
    	}
    	private class MouseHandler implements MouseListener, MouseMotionListener {
    
    		@Override
    		public void mousePressed(MouseEvent arg0) {
    			timepressed= System.currentTimeMillis();
    			
    			if(arg0.getX()>=xpos && arg0.getX()<xpos+width && arg0.getY()>ypos && arg0.getY()<ypos+height)
    				System.out.println("You clicked on Speedy at " + arg0.getX() + "," + arg0.getY());
    				p1x=arg0.getX();
    				p1y=arg0.getY();
    				
    		}
    
    		@Override
    		public void mouseReleased(MouseEvent arg0) {
    			timereleased= System.currentTimeMillis();
    			System.out.println("You released at " + arg0.getX() + "," + arg0.getY());
    			p2x=arg0.getX();
    			p2y=arg0.getY();
    			System.out.println(getSpeed()+ " " + getTheta() + " " + getMaximumHeight() + " " + getRange());
    			}
    
    		@Override
    		public void mouseDragged(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void mouseMoved(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void mouseClicked(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void mouseEntered(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void mouseExited(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    		
    	}
    	
    	public static void main(String[] args) {
    		Frame mainFrame = new Frame();
    		mainFrame.setVisible(true);
    		mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		mainFrame.setLocationRelativeTo(null);
    		mainFrame.setSize(800, 400);
    		mainFrame.setResizable(false);
    
    	}
    
    }
    I have all the equations for a correct projectile motion, for example in this case y= tan(theta).x - g.x^2/(2.V^2.cos^2(theta) )
    Do I use a while loop to get this animation? If yes how do I redraw on the Jpanel? Of course forget about the system.out.println statements.
    Last edited by af476; 12-15-2012 at 09:20 PM.

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Projectile Motion Problem

    Do I use a while loop to get this animation?
    No.

    It's all about threads. The graphical interface is updated on a specific thread whose most salient feature is that it is fast. If your monitor is redrawing at 60Hz the gui has about 0.017s to do its thing: to get the application's onscreen appearance uptodate. Any logic that takes an appreciable time to perform should not take place on this this thread. Certainly a while loop is quite out of place. Where wold you put it? The mouseWhatever() methods are called on the event dispatch thread (the thread in question) they are obliged to do their thing and get out of the way quickly.

    Use a timer.

    The link is to Oracle's Tutorial. Basically the plan boils down to this: when (in the mouseWhatever() methods) you see the need for an animation, start and/or create a timer. Its job is to call back into your code on a regular basis. It does so in the event dispatch thread. You give it a method to get called and this method should calculate and set the new coordinates for the label - a job it can do well within its share of that golden 0.017s.

    ---

    Another place to be conscious of threads - and the event dispatch thread (EDT) is the only one a simple program need worry about - is the main() method. The "rule" is that everything that alters the graphical user interface should execute (quickly!) on the event dispatch thread. Your main() breaks this rule by doing things with the gui even though it doesn't execute on the EDT.

    The HelloWorldSwing.java example shows a better way to start a graphical program. The main() method doesn't do any work itself. Rather it sets up a task and calls invokeLater() which causes the task to actually run on the EDT.

    ---

    Threads will take some work. A simpler impovement to your program would be to use javaCodingStandards for your variables: descriptive names, starting with a lowercase letter and using camelCase where necessary.
    Last edited by pbrockway2; 12-16-2012 at 01:07 AM.

Similar Threads

  1. Replies: 1
    Last Post: 12-06-2011, 10:29 PM
  2. JPanel motion
    By batyuska87 in forum New To Java
    Replies: 2
    Last Post: 03-08-2011, 06:42 PM
  3. JPanel motion
    By batyuska87 in forum AWT / Swing
    Replies: 0
    Last Post: 03-06-2011, 10:17 AM
  4. help on motion detection
    By MarkWilson in forum Advanced Java
    Replies: 4
    Last Post: 12-07-2009, 07:45 AM
  5. Projectile Trajectory
    By shuks in forum New To Java
    Replies: 3
    Last Post: 11-27-2009, 03:07 AM

Tags for this Thread

Posting Permissions

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