1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[JAVA GUI] Counting the trajectory of a projectile not shown

  1. Nov 30, 2015 #1
    Hello PFers,
    I'm trying to make a program in java which process user's position, initial velocity of the projectile and the elevation of the projectile and show the trajectory in GUI. The language is in java.

    The problem is, the trajectory won't show. It is supposed to show the player's rectangles and the trajectory of the projectile per second steps

    Here's my compilable code:
    Code (Java):
    import java.awt.Graphics2D;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.geom.Rectangle2D;

    import javax.swing.JComponent;
    import javax.swing.JFrame;

    public class Player
    {
       
        private double healthPoint = 0;
        private double position = 0;
       
        private String name = "";
       
        public Player(String name, double initialPosition)
        {
            this.name=name;
            this.position=initialPosition;
            this.healthPoint=100;

        }
       
        public void setPosition(double position)
        {
            this.position = position;
        }
       
        public double getPosition()
        {
            return this.position;
        }
       
        public String getName()
        {
            return this.name;
        }
    }

    class PlayerRect extends JComponent
    {
        private final static double HEIGHT = 35;
        private final static double WIDTH = 10;
        private double x = 0;
        private double y = 0;
        private Player player;
       
        public PlayerRect(JFrame f, String name, double initialPosition)
        {
            player = new Player (name, initialPosition);
            this.x = player.getPosition();
            this.y = f.getHeight();
        }
       
        public void paintPlayer(Graphics g) {  
            Graphics2D player = (Graphics2D)g;
            player.draw(new Rectangle2D.Double(x, y, WIDTH, HEIGHT));
            player.setColor(Color.RED);
        }
        public Player getPlayer(){
            return this.player;
        }
    }
    import java.lang.Math;

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.geom.Ellipse2D;

    import javax.swing.JFrame;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;

    public class Projectile
    {
        private final double GRAV = 9.81;
        private final double HALF = 0.5;
       
        private double velocity = 0;
        private double angle = 0;
        private double v0x = 0;
        private double vx = 0;
        private double x = 0;
        private double v0y = 0;
        private double vy = 0;
        private double y = 0;
        private double endTime = 0;
        private double endX = 0;
         public Projectile(double initialVelocity, double angle)
        {
            this.velocity = initialVelocity;
            this.angle = Math.toRadians(angle);
            this.v0x = velocity*(Math.cos(this.angle));
            this.v0y = velocity*(Math.sin(this.angle));
            this.endTime = 2 * (this.v0y / GRAV);
            this.endX = v0x * endTime;
        }
        public void count (double time)
        {
            this.vx = v0x;
            this.vy = v0y-(HALF*GRAV*time);
            this.x = vx*time;
            this.y = (HALF*GRAV*time+vy)*time;
        }
       
         public double getVx()
        {
            return this.vx;
        }
         public double getVy()
         {
            return this.vy;
        }
         public double getX()
         {
            return this.x;
        }
       
         public double getY()
        {
            return this.y;
        }
         public double getEndTime()
        {
            return this.endTime;
        }
        public double getEndX()
        {
            return this.endX;
        }
       
    }


    class ProjectileGUI extends JComponent
    {
        private static final double RAD = 15;
       
        private double x = 0;
        private double y = 0;
        private Color pColor = Color.BLUE;
        private PlayerRect player;
        private Projectile pj;
        private JFrame f;
       
        public ProjectileGUI(JFrame f, PlayerRect player, double initialVelocity, double angle)
        {
            pj = new Projectile(initialVelocity, angle);
            this.player = player;
            this.x = x;
            this.y = f.getWidth();
            this.f = f;
        }
       
        private void ballComponent(Graphics b){
            Graphics2D g2 = (Graphics2D) b;
            g2.draw(new Ellipse2D.Double(x, y, RAD, RAD));
            //g2.drawOval.Double(x, y, RAD, RAD);
            g2.setPaint(pColor);
            for (int time = 0; time < pj.getEndX(); time++) {
                pj.count(time);
                g2.fill(new Ellipse2D.Double(player.getPlayer().getPosition() + (int) pj.getX(), f.getWidth() - (int) pj.getY(), RAD, RAD));
                String comma=" , ";
                System.out.println(player.getPlayer().getPosition() + (int) pj.getX() + f.getWidth() - (int) pj.getY() +  RAD +  RAD);
            }
           
            pj.count(pj.getEndTime());
            g2.fill(new Ellipse2D.Double(player.getPlayer().getPosition() + (int) pj.getX(), f.getWidth() - (int) pj.getY(), RAD, RAD));
        }
        public Projectile getProjectile(){
            return this.pj;
        }
    }



    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JOptionPane;


    public class GUIMain
    {
        private static final int FRAME_WIDTH=1000;
        private static final int FRAME_HEIGHT=750;
       
        public static void main(String[] args)
        {
            //User Input
            String p1Name = JOptionPane.showInputDialog( null, "Name of player 1: ");
            Double p1Position = Double.parseDouble(JOptionPane.showInputDialog( null, "x " + p1Name + " (metre): "));
            String p2Name = JOptionPane.showInputDialog( null, "Masukkan nama player 2: " );
            Double p2Position = Double.parseDouble(JOptionPane.showInputDialog( null, "x " + p2Name + " (metre): "));
            Double p1Velocity = Double.parseDouble(JOptionPane.showInputDialog( null, "initial veocity "+ p1Name +" (m/s): " ));
            Double p1Angle = Double.parseDouble(JOptionPane.showInputDialog( null, "elevation " + p1Name + " (derajat): " ));
           
            JFrame f = new JFrame();
            JPanel p = new JPanel();
            f.setSize(FRAME_WIDTH, FRAME_HEIGHT);
            PlayerRect p1 = new PlayerRect(f, p1Name, p1Position);
            PlayerRect p2 = new PlayerRect(f, p2Name, p2Position);
            ProjectileGUI ball = new ProjectileGUI(f, p1, p1Velocity, p1Angle);
           
           
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setTitle("Projectile Trajectory");
            p.add(p1);
            p.add(p2);
            p.add(ball);
           
            f.add(p);
            System.out.println(ball.getProjectile().getEndTime());
            System.out.println(ball.getProjectile().getEndX());
            System.out.println(ball.getProjectile().getY());
            if ( ( ( ball.getProjectile().getEndX() ) >= (p2.getPlayer().getPosition() - 5 ) ) &&
                                            ( ( ball.getProjectile().getEndX() ) < ( p2.getPlayer().getPosition()+5 ) ) ){
                JOptionPane.showMessageDialog(null, "Success" );
            }else{
                JOptionPane.showMessageDialog(null, "Failed" );
            }
        }
    }
     
    *note: they have multiple imports because they are from separate .java files
    I really appreciate your help, thank you!
     
  2. jcsd
  3. Nov 30, 2015 #2

    Mark44

    Staff: Mentor

    What sort of output do you get? That would give us a clue as to where to look.

    Also, are you using a debugger? If not, take a look at this documentation about jdb, Java debugger -- http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html
     
  4. Dec 1, 2015 #3
    That is the problem, there's no output. The frame shows, but it's blank and won't show the graphic. Thank you for the debugger, I will check it
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: [JAVA GUI] Counting the trajectory of a projectile not shown
  1. Java counting program. (Replies: 9)

Loading...