Question: i want plane Adopting appropriate Java code and using shape construction functions along with animation ` ` ` import javax.swing. * ; import java.awt. *

i want plane
Adopting appropriate Java code and using shape construction functions along with animation
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PlaneAnimation extends JPanel implements ActionListener {
private Timer timer;
private int planeX =0; // Starting X position for the plane
private final int planeY =150; // Y position (constant)
private final int planeSpeed =5; // Speed of the plane
private final int screenWidth =800; // Width of the window
private final int screenHeight =400; // Height of the window
public PlaneAnimation(){
setPreferredSize(new Dimension(screenWidth, screenHeight));
setBackground(Color.WHITE); // Set background color
timer = new Timer(50, this); // Timer to refresh animation every 50ms
timer.start();
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D) g;
// Anti-aliasing for smoother drawing
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Set the color and thickness of the plane's outline
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(10)); // Set thickness to 10 for all lines except dashed lines
// Draw the plane
drawPlane(g2d, planeX, planeY);
// Draw the long black vertical line as shown in your image
g2d.setColor(Color.BLACK); // Set color to black for the vertical line
g2d.setStroke(new BasicStroke(10)); // Make the line even thicker
}
g2d.drawLine(planeX, planeY -130, planeX, planeY +90); // Vertical black line
private void drawPlane(Graphics2D g2d, int x, int y){
int scaleFactor =3;
int heightIncrease =10; // Adjust height to make the plane flatter
int bodyLength =100; // Length of the plane's body
// Adjusted body with a rounded front
Polygon body = new Polygon();
body.addPoint(x, y); // Back of the plane
body.addPoint(x + bodyLength * scaleFactor, y); // Top of the plane (extended)
```
// Adding more points to create a rounded front
body.addPoint(x +(bodyLength +5)* scaleFactor, y +2* scaleFactor); // Start of the curve
body.addPoint(x +(bodyLength +7)* scaleFactor, y +5* scaleFactor); // Mid-curve
body.addPoint(x +(bodyLength +9)* scaleFactor, y +8* scaleFactor); // Rounded middle
body.addPoint(x +(bodyLength +7)* scaleFactor, y +11* scaleFactor); // Mid-curve
body.addPoint(x +(bodyLength +5)* scaleFactor, y +14* scaleFactor); // End of the curve
body.addPoint(x + bodyLength * scaleFactor, y +(20+ heightIncrease)* scaleFactor); // Bottom of the plane (extended)
body.addPoint(x, y +(20+ heightIncrease)* scaleFactor); // Back of the plane
g2d.drawPolygon(body);
// Front wing, smaller and angled backward
Polygon wingTop = new Polygon();
int wingTopPoint1X = x +20* scaleFactor;
int wingTopPoint1Y = y; // Top of the body
int wingTopPoint2X = x -10* scaleFactor;
int wingTopPoint2Y = y -30* scaleFactor; // Smaller extension backward and upward
int wingTopPoint3X = x +40* scaleFactor;
int wingTopPoint3Y = y; // Connecting back to the body
wingTop.addPoint(wingTopPoint1X, wingTopPoint1Y);
wingTop.addPoint(wingTopPoint2X, wingTopPoint2Y);
wingTop.addPoint(wingTopPoint3X, wingTopPoint3Y);
g2d.drawPolygon(wingTop);
// Bottom wing, smaller and angled backward
Polygon wingBottom = new Polygon();
int wingBottomPoint1X = x +20* scaleFactor;
int wingBottomPoint1Y = y +(20+ heightIncrease)* scaleFactor; // Bottom of the body
int wingBottomPoint2X = x -10* scaleFactor;
int wingBottomPoint2Y = y +(35+ heightIncrease)* scaleFactor; // Smaller extension backward and downward
int wingBottomPoint3X = x +40* scaleFactor;
int wingBottomPoint3Y = y +(20+ heightIncrease)* scaleFactor; // Connecting back to the body
wingBottom.addPoint(wingBottomPoint1X, wingBottomPoint1Y);
wingBottom.addPoint(wingBottomPoint2X, wingBottomPoint2Y);
wingBottom.addPoint(wingBottomPoint3X, wingBottomPoint3Y);
g2d.drawPolygon(wingBottom);
// Store original stroke
Stroke originalStroke = g2d.getStroke();
// Set dashed stroke
g2d.setStroke(new BasicStroke(6f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{10,10},0));
// Draw dashed line along the trailing edge of the top wing
g2d.drawLine(wingTopPoint2X, wingTopPoint2Y, wingTopPoint3X, wingTopPoint3Y);
// Draw dashed line along the trailing edge of the bottom wing
g2d.drawLine(wingBottomPoint2X, wingBottomPoint2Y, wingBottomPoint3X, wingBottomPoint3Y);
// Reset to original stroke
}
g2d.setStroke(originalStroke);
@Override
public void actionPerformed(ActionEvent e){
planeX += planeSpeed;
// Loop the plane back to the start if it moves out of the screen
if (planeX > screenWidth){
}
planeX =-140*3; // Reset the plane to start from the left
repaint(); // Redraw the screen
}
public static void main(String[] args){
JFrame frame = new JFrame("2D Plane Animation with Dashes Aligned to Wings");
PlaneAnimation planeAnimation = new PlaneAnimation();
frame.add(planeAnimation);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setL
i want plane Adopting appropriate Java code and

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!