Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the code for drawing an ellipse as according to the instructions. For now

ID: 3681078 • Letter: W

Question

Write the code for drawing an ellipse as according to the instructions.

For now we will define a to be the semimajor axis, and b to be the semiminor axis. Using the semimajor and semiminor axis values (a and b), we can redefine the x and y locations as a function of an angle. X = acos Write a function called drawellipse() that takes in two inputs. The inputs represent the a and b values respectively. Then, animate a plot that draws an ellipse. The animation should show the ellipse being drawn from an angle of 0, all the way through 360 degrees. There should also be a line from the center to the ellipse that will also be animated, along with a point on the ellipse that's also animated

Explanation / Answer

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;

public class DrawEllipse extends JApplet {
    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
       
        g2.setPaint(Color.RED);
        g2.setStroke(new BasicStroke(5.0f));
        g2.draw(new Ellipse2D.Double(50, 50, 250, 250));

        g2.setPaint(Color.BLUE);
        g2.fill(new Ellipse2D.Double(10, 10, 40, 40));

        g2.setPaint(Color.YELLOW);
        g2.fill(new Ellipse2D.Double(10, 300, 40, 40));

        g2.setPaint(Color.GREEN);
        g2.fill(new Ellipse2D.Double(300, 300, 40, 40));

        g2.setPaint(Color.ORANGE);
        g2.fill(new Ellipse2D.Double(300, 10, 40, 40));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Ellipse Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JApplet applet = new DrawEllipse();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setSize(new Dimension(400, 400));
        frame.setVisible(true);
    }
}