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

Click For Summary
SUMMARY

The forum discussion focuses on a Java program designed to visualize the trajectory of a projectile based on user input for position, initial velocity, and elevation. The code provided includes classes such as Player, PlayerRect, Projectile, and ProjectileGUI. The main issue highlighted is that the trajectory is not displayed correctly in the GUI, despite the calculations for projectile motion being implemented. The program utilizes Java's AWT and Swing libraries for graphical representation.

PREREQUISITES
  • Java programming knowledge, specifically with AWT and Swing libraries
  • Understanding of basic physics concepts related to projectile motion
  • Familiarity with object-oriented programming principles in Java
  • Experience with graphical rendering in Java applications
NEXT STEPS
  • Debug the ballComponent method in ProjectileGUI to ensure the trajectory is rendered correctly
  • Learn about Java's Graphics2D class for advanced rendering techniques
  • Explore the use of animation in Java Swing to visualize the projectile's motion over time
  • Investigate the implementation of event listeners to enhance user interaction with the GUI
USEFUL FOR

Java developers, game developers, and educators looking to implement physics simulations in graphical applications will benefit from this discussion.

demensia
Messages
2
Reaction score
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
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
 
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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
12K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 8 ·
Replies
8
Views
13K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K