Results 1 to 2 of 2
Thread: Projectile Motion Problem
- 12-15-2012, 08:17 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 1
- Rep Power
- 0
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:
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) )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); } }
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 08:20 PM.
- 12-16-2012, 12:04 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Projectile Motion Problem
No.Do I use a while loop to get this animation?
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 12:07 AM.
Similar Threads
-
Questions on KeyListener, ActionListener & Projectile Motion
By wpark in forum New To JavaReplies: 1Last Post: 12-06-2011, 09:29 PM -
JPanel motion
By batyuska87 in forum New To JavaReplies: 2Last Post: 03-08-2011, 05:42 PM -
JPanel motion
By batyuska87 in forum AWT / SwingReplies: 0Last Post: 03-06-2011, 09:17 AM -
help on motion detection
By MarkWilson in forum Advanced JavaReplies: 4Last Post: 12-07-2009, 06:45 AM -
Projectile Trajectory
By shuks in forum New To JavaReplies: 3Last Post: 11-27-2009, 02:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks