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

In summary, the problem is that the trajectory won't show. It is supposed to show the player's rectangles and the trajectory of the projectile per second steps.
  • #1
demensia
2
0
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:
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!
 
Physics news on Phys.org
  • #2
demensia said:
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:
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!
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
 
  • #3
Mark44 said:
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

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
 

1. How can I count the trajectory of a projectile in a Java GUI?

To count the trajectory of a projectile in a Java GUI, you will need to use mathematical equations and algorithms to calculate the trajectory based on the initial velocity, angle, and gravity. You can then use graphics to display the trajectory on the GUI.

2. Can I use pre-existing libraries or functions to count the trajectory of a projectile in Java GUI?

Yes, there are various libraries and functions available in Java that can help you calculate and display the trajectory of a projectile in a GUI. Some examples include the Java2D library and the Math class in Java.

3. Is it possible to customize the appearance of the trajectory in a Java GUI?

Yes, you can customize the appearance of the trajectory in a Java GUI by changing the color, thickness, and style of the line or shape used to represent the trajectory. You can also add labels or markers to indicate important points on the trajectory.

4. Can I simulate different scenarios and see the trajectory in real-time in a Java GUI?

Yes, you can simulate different scenarios by manipulating the initial parameters and have the trajectory update in real-time on the Java GUI. This can be achieved by using loops and updating the graphics continuously.

5. How can I use user input to calculate and display the trajectory of a projectile in a Java GUI?

You can use text fields or sliders on the Java GUI to allow the user to input the initial parameters for the projectile, such as initial velocity and angle. You can then use these values in your calculations to display the trajectory on the GUI.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top