I need help on this 5 and 6.
here is the code so far:
import javax.swing.*;
import java.awt.*;
import java.util.*;
/**
* !!! Put in description of your program here !!!
*
* @author !!! your name here !!!
*
*/
public class AngryBirds extends JPanel
{
/**
* Here are some variables you are likely to find useful. I have created
* this variables outside of any method, so that they are available to all
* the methods.
*
* (Typically instance variables like this should be labeled as "public" or
* "private" but I have omitted this designation since you are not familiar
* with the meanings of these terms yet.)
*
* Show care not to create local variables with the same names as these
* ones!
*/
double timeInterval;
double velocity;
double angle;
/**
* paint
*
* Draws the Angry Birds simulation on the graphics window
*
* @param g the Graphics object for the window
*/
public void paint(Graphics g)
{
super.paint(g); // do not edit
// Set background color
Color skyBlueDay = new Color(135, 206, 250);
setBackground(skyBlueDay);
// Draw horizon (ground)
g.setColor(new Color(34, 139, 34)); // Green color for the ground
g.fillRect(0, getHeight() - 50, getWidth(), 50);
// Draw structure (e.g., a wooden block)
g.setColor(new Color(139, 69, 19)); // Brown color for the structure
g.fillRect(200, getHeight() - 100, 80, 80);
// Draw pigs in the structure (green round pigs with happy faces)
g.setColor(Color.GREEN);
// Pig 1
g.fillOval(215, getHeight() - 85, 20, 20); // Head
g.setColor(Color.BLACK); // Pig's features
g.fillArc(220, getHeight() - 80, 6, 6, 180, 180); // Happy mouth
g.fillOval(222, getHeight() - 85, 2, 2); // Left eye
g.fillOval(226, getHeight() - 85, 2, 2); // Right eye
// Pig 2
g.setColor(Color.GREEN);
g.fillOval(235, getHeight() - 85, 20, 20); // Head
g.setColor(Color.BLACK); // Pig's features
g.fillArc(240, getHeight() - 80, 6, 6, 180, 180); // Happy mouth
g.fillOval(242, getHeight() - 85, 2, 2); // Left eye
g.fillOval(246, getHeight() - 85, 2, 2); // Right eye
// Pig 3
g.setColor(Color.GREEN);
g.fillOval(255, getHeight() - 85, 20, 20); // Head
g.setColor(Color.BLACK); // Pig's features
g.fillArc(260, getHeight() - 80, 6, 6, 180, 180); // Happy mouth
g.fillOval(262, getHeight() - 85, 2, 2); // Left eye
g.fillOval(266, getHeight() - 85, 2, 2); // Right eye
// Draw slingshot
g.setColor(new Color(139, 69, 19)); // Brown color for slingshot base
g.fillRect(50, getHeight() - 20, 20, 100); // Slingshot base
// Angry bird (Red bird)
g.setColor(new Color(255, 0, 0)); // Red color for the bird
g.fillOval(30, getHeight() - 50, 40, 40); // Body
g.setColor(Color.BLACK); // Bird's features
g.fillArc(40, getHeight() - 45, 10, 10, 0, -180); // Happy mouth
g.fillOval(35, getHeight() - 50, 8, 8); // Left eye
g.fillOval(45, getHeight() - 50, 8, 8); // Right eye
//put your code here
}//paint
/**
* init
*
* prompts user for information about the simulation
*/
public void init()
{
// prompt user for information
String time = ""; //used to store user input
do
{
time = JOpti.
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
I need help on this 5 and 6. here is the code so far import jav.pdf
1. I need help on this 5 and 6.
here is the code so far:
import javax.swing.*;
import java.awt.*;
import java.util.*;
/**
* !!! Put in description of your program here !!!
*
* @author !!! your name here !!!
*
*/
public class AngryBirds extends JPanel
{
/**
* Here are some variables you are likely to find useful. I have created
* this variables outside of any method, so that they are available to all
* the methods.
*
* (Typically instance variables like this should be labeled as "public" or
* "private" but I have omitted this designation since you are not familiar
* with the meanings of these terms yet.)
*
* Show care not to create local variables with the same names as these
* ones!
*/
double timeInterval;
double velocity;
double angle;
/**
* paint
*
* Draws the Angry Birds simulation on the graphics window
*
* @param g the Graphics object for the window
*/
2. public void paint(Graphics g)
{
super.paint(g); // do not edit
// Set background color
Color skyBlueDay = new Color(135, 206, 250);
setBackground(skyBlueDay);
// Draw horizon (ground)
g.setColor(new Color(34, 139, 34)); // Green color for the ground
g.fillRect(0, getHeight() - 50, getWidth(), 50);
// Draw structure (e.g., a wooden block)
g.setColor(new Color(139, 69, 19)); // Brown color for the structure
g.fillRect(200, getHeight() - 100, 80, 80);
// Draw pigs in the structure (green round pigs with happy faces)
g.setColor(Color.GREEN);
// Pig 1
g.fillOval(215, getHeight() - 85, 20, 20); // Head
g.setColor(Color.BLACK); // Pig's features
g.fillArc(220, getHeight() - 80, 6, 6, 180, 180); // Happy mouth
g.fillOval(222, getHeight() - 85, 2, 2); // Left eye
g.fillOval(226, getHeight() - 85, 2, 2); // Right eye
// Pig 2
g.setColor(Color.GREEN);
g.fillOval(235, getHeight() - 85, 20, 20); // Head
g.setColor(Color.BLACK); // Pig's features
g.fillArc(240, getHeight() - 80, 6, 6, 180, 180); // Happy mouth
g.fillOval(242, getHeight() - 85, 2, 2); // Left eye
g.fillOval(246, getHeight() - 85, 2, 2); // Right eye
// Pig 3
g.setColor(Color.GREEN);
g.fillOval(255, getHeight() - 85, 20, 20); // Head
3. g.setColor(Color.BLACK); // Pig's features
g.fillArc(260, getHeight() - 80, 6, 6, 180, 180); // Happy mouth
g.fillOval(262, getHeight() - 85, 2, 2); // Left eye
g.fillOval(266, getHeight() - 85, 2, 2); // Right eye
// Draw slingshot
g.setColor(new Color(139, 69, 19)); // Brown color for slingshot base
g.fillRect(50, getHeight() - 20, 20, 100); // Slingshot base
// Angry bird (Red bird)
g.setColor(new Color(255, 0, 0)); // Red color for the bird
g.fillOval(30, getHeight() - 50, 40, 40); // Body
g.setColor(Color.BLACK); // Bird's features
g.fillArc(40, getHeight() - 45, 10, 10, 0, -180); // Happy mouth
g.fillOval(35, getHeight() - 50, 8, 8); // Left eye
g.fillOval(45, getHeight() - 50, 8, 8); // Right eye
//put your code here
}//paint
/**
* init
*
* prompts user for information about the simulation
*/
public void init()
{
// prompt user for information
String time = ""; //used to store user input
do
{
time = JOptionPane.showInputDialog("Enter the time interval (>0).");
try
{
timeInterval = Double.parseDouble(time);
}
catch (NumberFormatException e)
4. {
// Handle invalid input (non-numeric)
timeInterval = -1; // Set to a negative value to trigger the loop
}
} while (timeInterval <= 0);
String velocityStr = "";
boolean validVelocityInput = false;
while (!validVelocityInput)
{
velocityStr = JOptionPane.showInputDialog("Enter the velocity (m/s) (>0).");
if (velocityStr != null && !velocityStr.isEmpty())
{
try
{
velocity = Double.parseDouble(velocityStr);
if (velocity > 0)
{
validVelocityInput = true;
}
else
{
JOptionPane.showMessageDialog(null, "Velocity must be greater than 0.");
}
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric
value.");
}
}
else
{
JOptionPane.showMessageDialog(null, "Input cannot be empty. Please enter a value.");
}
}
5. String angleStr = "";
boolean validAngleInput = false;
while (!validAngleInput)
{
angleStr = JOptionPane.showInputDialog("Enter the angle (radians) (0 <= angle <=
?/2).");
if (angleStr != null && !angleStr.isEmpty())
{
try
{
angle = Double.parseDouble(angleStr);
if (angle >= 0 && angle <= Math.PI / 2)
{
validAngleInput = true;
}
else
{
JOptionPane.showMessageDialog(null, "Angle must be between 0 and ?/2.");
}
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric
value.");
}
}
else
{
JOptionPane.showMessageDialog(null, "Input cannot be empty. Please enter a value.");
}
}
}
/**
* This constructor method creates the window for you.
6. * Do not modify it.
*/
public AngryBirds()
{
super();
}
/**
* main
* This method starts the application. It creates a new AngryBirds object.
* Do not modify this except that you may increase the window size
* up to a maximum of 1000x1000.
*/
public static void main(String[] args)
{
//Create the window
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Gather input from the user
AngryBirds ab = new AngryBirds();
frame.add(ab);
ab.init();
//Display the window
frame.setVisible(true);
frame.repaint();
}
}//class AngryBirds
[30%] Your paint method should use a loop to draw the position of the angry bird at each time
increment while the bird is in the air (y position >=0 ). Your code should honor the user's inputs
in this regard. You may assume no wind resistance. Important: This is not an animation; you'll be
drawing a fixed image. For example, the image at right statically depicts the motion of a circle
moving through an arc. [10%] Your loop should stop drawing the bird once it hits the ground or
the structure. It is okay if it draws the bird at a position that is outside of the window boundary
7. because it is too high or too far to the right.
ogistics and Hints - The starter code is just a .java file. To use it, you will need to create a new
BlueJ project then create a class named AngryBirds in the project. Finally, replace BlueJ's
default code for the AngryBirds class with the starter code. - Your graphics should be
recognizable but need not be particularly well drawn. They do not need to match the quality of
the actual Angry Birds game. - Since you will need to draw the angry bird in multiple locations,
you will probably want to create a method that can draws the bird at a specified x,y location. -
Below is pseudocode for drawing the arc of the bird's path. This code assumes Cartesian
coordinates (i.e., the y-axis goes up as normal). When painting the bird on the screen, you'll have
to convert the y-value to graphics coordinates (y-axis inverted). 0 Assign x and y-position to the
starting coordinate of the angry bird (the slingshot). o Assign time =0. o Main Loop: While bird
is in the air (y-position is above the ground): - Calculate x-position at the current time:
velocitycos(angle)time - Calculate y-position at the current time: velocity sin( angle) time 0.59.8
time 2 - Update the time by adding the timeInterval to it. - Draw the bird. Don't forget to invert
the y-axis. - If the bird has hit the structure (something you shall have to determine based on
your structure's position and design) then exit the loop. - The sin and cos functions are available
in the Math class. Note that these functions expect angles in radians (not degrees). - Your bird
position calculations should be using doubles, but you will need to convert these to ints to use as
coordinates for drawing. Use a type cast to do this conversion. - Put all your dialog windows in
the init() constructor and all your drawing in the paint() method.