Purpose: The purpose of this assignment is to solve real world problem using Jav
ID: 3724125 • Letter: P
Question
Purpose: The purpose of this assignment is to solve real world problem using Java. Write an object-oriented Java program to draw trajectories of rockets using some values from file. The file contains several lines of data, the first of which is a number telling us how many rockets North Korea wants to launch. The next n lines contain the data for each launch and each line consists three numbers. The first number is the velocity of the rocket right after launch and the fuel source is cut oft (assume that the height above ground is negligible). The next number is the angle at which the will be launched. And the last number is the distance to the target. Velocity and distance are in meters per second and kilometers respectively (so remember to convert units!!!). rocket Your task is to calculate whether each rocket launch is capable of reaching its' target, draw trajectory (using arches), and if the rocket will not reach its' target draw a happy face, and if the rocket is capable of reaching its' target, draw a sad face (you can assume that the rockets have a gyroscope in them, so if the distance it can travel is greater than the distance to the target, that is considered on target). Your program should plot each trajectory similar to the one shown below Use the formulas below to calculate the necessary trajectories, flight paths, etc. Time of flight, t = sin20 2g Maximum height reached, H = Horizontal range, R vesin2e avaFX Output Dr. RahmanExplanation / Answer
***************************************************************************
Class to draw rocket trajectories
import java.awt.*; // for Graphics
import java.io.*; // For reading file
public class RocketTrajectory {
public static final double GRAVITY = -9.81;
public static void main(String[] args) {
// draw the graph of the projectile for the given input
DrawingPanel panel = new DrawingPanel(500, 500);
Graphics g = panel.getGraphics();
try{
FileInputStream fstream = new FileInputStream("rocketdata.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
boolean is_first = true;
while ((strLine = br.readLine()) != null) {
String[] tokens = strLine.split(" ");
if(is_first) {
is_first = false;
} else {
projectile(g, Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]));
}
}
in.close();
}catch (Exception e){
System.err.println("Error: " + e.toString());
}
}
// Plots the motion of a single projectile with the given
// initial velocity v0, angle, and number of steps to plot.
public static void projectile(Graphics g, double v0, double angle, int steps) {
// v0y = v0 sin theta, v0x = v0 cos theta
double v0y = v0 * Math.sin(Math.toRadians(angle));
double v0x = v0 * Math.cos(Math.toRadians(angle));
// t = -2v0 / a
double totalTime = -2 * v0y / GRAVITY;
double dt = totalTime / steps;
System.out.println(" x y time");
for (int i = 0; i <= steps; i++) {
double t = i * dt;
double x = v0x * t;
double y = displacement(v0y, GRAVITY, t);
System.out.printf("%8.2f %8.2f %8.2f ", x, y, t);
g.fillOval( (int) x, (int) (300 - y), 5, 5);
}
}
// Computes and returns the change in position of a projectile
// given its initial velocity v0, acceleration a, and time t.
public static double displacement(double v0, double a, double t) {
return (v0 * t + 0.5 * a * Math.pow(t, 2));
}
}
***************************************************************************
Class DrawingPanel
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;
public class DrawingPanel implements ActionListener {
public static final int DELAY = 250; // delay between repaints in millis
private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save";
private static String TARGET_IMAGE_FILE_NAME = null;
private static final boolean PRETTY = true; // true to anti-alias
private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file
private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse position
private long createTime;
static {
TARGET_IMAGE_FILE_NAME = System.getProperty(DUMP_IMAGE_PROPERTY_NAME);
DUMP_IMAGE = (TARGET_IMAGE_FILE_NAME != null);
}
// construct a drawing panel of given width and height enclosed in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
this.statusBar = new JLabel(" ");
this.statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
this.panel.setBackground(Color.WHITE);
this.panel.setPreferredSize(new Dimension(width, height));
this.panel.add(new JLabel(new ImageIcon(image)));
// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
DrawingPanel.this.statusBar.setText("(" + e.getX() + ", " + e.getY() + ")");
}
public void mouseExited(MouseEvent e) {
DrawingPanel.this.statusBar.setText(" ");
}
};
this.panel.addMouseListener(listener);
this.panel.addMouseMotionListener(listener);
this.g2 = (Graphics2D)image.getGraphics();
this.g2.setColor(Color.BLACK);
if (PRETTY) {
this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
this.g2.setStroke(new BasicStroke(1.1f));
}
this.frame = new JFrame("CS307 Drawing Panel");
this.frame.setResizable(false);
this.frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (DUMP_IMAGE) {
DrawingPanel.this.save(TARGET_IMAGE_FILE_NAME);
}
System.exit(0);
}
});
this.frame.getContentPane().add(panel);
this.frame.getContentPane().add(statusBar, "South");
this.frame.pack();
this.frame.setVisible(true);
if (DUMP_IMAGE) {
createTime = System.currentTimeMillis();
this.frame.toBack();
} else {
this.toFront();
}
// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}
// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
this.panel.repaint();
if (DUMP_IMAGE && System.currentTimeMillis() > createTime + 4 * DELAY) {
this.frame.setVisible(false);
this.frame.dispose();
this.save(TARGET_IMAGE_FILE_NAME);
System.exit(0);
}
}
// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return this.g2;
}
// set the background color of the drawing panel
public void setBackground(Color c) {
this.panel.setBackground(c);
}
// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
this.frame.setVisible(visible);
}
// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}
// take the current contents of the panel and write them to a file
public void save(String filename) {
String extension = filename.substring(filename.lastIndexOf(".") + 1);
// create second image so we get the background color
BufferedImage image2 = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
Graphics g = image2.getGraphics();
g.setColor(panel.getBackground());
g.fillRect(0, 0, this.width, this.height);
g.drawImage(this.image, 0, 0, panel);
// write file
try {
ImageIO.write(image2, extension, new java.io.File(filename));
} catch (java.io.IOException e) {
System.err.println("Unable to save image: " + e);
}
}
// makes drawing panel become the frontmost window on the screen
public void toFront() {
this.frame.toFront();
}
}
***************************************************************************
My sample data - rocketdata.txt
5
60 50 60
50 80 50
70 90 42
40 30 50
65 35 45
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.