|
|
Okay so, here is what I have so far, I am constructing a program that will eventually take a user input angle and velocity and display the ballistic path of a notional projectile, sans friction and whatnot.
I have yet to build the user defined variables, I am just trying to get the polyline to display in the frame with a pre-defined angle and velocity. Somewhere down the road I would like to animate this, but for right now I am just trying to make it display on a Canvas.
I have built each these sections as separate classes in a package and I am having some difficulty getting the graphics out of its class and onto my JFrame. Any help, pointer, or slaps upside the head would be most useful.
The specific error I am wrestling right now is a Null Pointer Exception that traces back to the line "g2.drawPolyline(x, y,points);" in my BalisticGraphics class.
package aBalisticLine;
public class BalisticMath {
int timeCounter;
int initialVelocity;
int xDisplacement;
int yDisplacement;
double radAngleOfTravel;
double gravity = -9.8;
private double v0x;
private double v0y;
//-------------------- DEFINE AND GET COUNTER VARIABLE TO BE USED TO MARK TIME------------
public void setCounter(int counter){
timeCounter = counter;
}
public double getCounter(){
return timeCounter;
}
//-------------------- DEFINE AND GET VALUE OF INITIAL VELOCITY----------------------------
public void setInitialV(int iVelocity){
initialVelocity = iVelocity;
}
public double getInitialV(){
return initialVelocity;
}
//--------------------- SET ANGLE ABOVE HORIZONTAL -----------------------------------------
public void setAngleOfTravel(int degAngleOfTravel){
radAngleOfTravel = degAngleOfTravel * (Math.PI / 180);
}
//--------------------- GET ANGLE OF TRAVEL ------------------------------------------------
public double getAngleOfTravel(){
return radAngleOfTravel;
}
//--------------------- CALCULATE X AXIS VELOCITY ------------------------------------------
public double calcXVelocity(){
v0x = getInitialV() * Math.cos(getAngleOfTravel());
return v0x;
}
//--------------------- CALCULATE Y AXIS VELOCITY ------------------------------------------
public double calcYVelocity(){
v0y = getInitialV() * Math.sin(getAngleOfTravel());
return v0y;
}
//-------------------- CALCULATE AND RETURN THE POSITION OF THE PROJECTILE ON THE X AXIS AT A GIVEN TIME (IN SECONDS)
//-------------------- AFTER LAUNCH
public int calcXDisplacement(int counter){
Long xDisplace = Math.round(calcXVelocity() * counter);
xDisplacement = xDisplace.intValue();
return xDisplacement;
}
//-------------------- CALCULATE AND RETURN THE POSITION OF THE PROJECTILE ON THE Y AXIS AT A GIVEN TIME (IN SECONDS)
//-------------------- AFTER LAUNCH
public int calcYDisplacement(int counter){
Long yDisplace = Math.round((calcYVelocity() * counter) +
(gravity * 0.5 * (getCounter() * getCounter())));
yDisplacement = yDisplace.intValue();
return yDisplacement;
}
}
package aBalisticLine;
import static java.lang.System.out;
public class BalisticArrays {
static int xDis;
static int yDis;
static int z;
static int vel = 80;
static int ang = 45;
int iterationCount(){
z = 0;
do{
BalisticMath bal = new BalisticMath();
bal.setInitialV(vel);
bal.setAngleOfTravel(ang);
bal.setCounter(z);
yDis = bal.calcYDisplacement(z);
z++;
}while (yDis >= 0);
return z;
}
public int[] xArray(){
int[] xArr = new int[iterationCount() + 1];
for (int n=0; n <= iterationCount(); n++){
BalisticMath bal = new BalisticMath();
bal.setInitialV(vel);
bal.setAngleOfTravel(ang);
int x = bal.calcXDisplacement(n);
xArr[n] = x;
}
return xArr;
}
public int[] yArray(){
int[] yArr = new int[iterationCount() + 1];
for (int n=0; n <= iterationCount(); n++){
BalisticMath bal = new BalisticMath();
bal.setInitialV(vel);
bal.setAngleOfTravel(ang);
int x = bal.calcYDisplacement(n);
yArr[n] = x;
}
return yArr;
}
public int xMax() {
// Validates input
int[] array1 = xArray();
if (array1 == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array1.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
int max = array1[0];
for (int j1 = 1; j1 < array1.length; j1++) {
if (array1[j1] > max) {
max = array1[j1];
}
}
return max;
}
public int yMax() {
// Validates input
int[] array2 = yArray();
if (array2 == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array2.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
int max = array2[0];
for (int j2 = 1; j2 < array2.length; j2++) {
if (array2[j2] > max) {
max = array2[j2];
}
}
return max;
}
}
package aBalisticLine;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import static java.lang.System.out;
public class BalisticGraphics extends JPanel{
BalisticArrays ba = new BalisticArrays();
int[] xArray = ba.xArray();
int[] yArray = ba.yArray();
/**
*
*/
private static final long serialVersionUID = 1L;
public void paint(Graphics g){
update(g);
}
public void update(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
JPanel p = new JPanel();
//Timer t = new Timer(1000,a);
Canvas c = new Canvas();
out.print(ba.xMax());
out.print(" ");
out.println(ba.yMax());
int x[] = ba.xArray();
int y[] = ba.yArray();
int points = ba.iterationCount();
g2.drawPolyline(x, y,points);
g2.rotate(180);
c.paint(g2);
p.add(c);
}
}
package aBalisticLine;
import static java.lang.System.out;
import java.awt.Canvas;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BalisticMain {
public static void main(String args[]){
JFrame f = new JFrame();
BalisticUI bpanel = new BalisticUI();
BalisticGraphics bg = new BalisticGraphics();
BalisticArrays ba = new BalisticArrays();
Graphics g = bg.getGraphics();
Canvas c = new Canvas();
JPanel j2 = new JPanel();
c.getGraphics();
c.setSize(ba.xMax() + 20, ba.yMax() + 20);
bg.paint(g);
j2.add(bg);
//f.add(bpanel.balisticUI());
f.setLayout(new FlowLayout(FlowLayout.LEFT));
f.setSize(350,150);
f.add(bpanel.balisticUI());
f.add(j2);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}