Question

Use the Circle class to draw each of the planets of our solar system. Draw the...

Use the Circle class to draw each of the planets of our solar system. Draw the sun first, then each planet according to distance from the sun. (Mercury, Venus, Earth, Mars, Jupiter Saturn, Uranus, Neptune, and the dwarf planet, Pluto.) Label each planet using the Text shape class. JAVA CODE PLEASE

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SolarSystem extends JComponent
        implements ActionListener, KeyListener {
    private static final int DEFAULT_WIDTH = 600;
    private static final int DEFAULT_HEIGHT = 400;

    private Body[] bodies;
    private Spaceship ship;
    private int framesUntilRepaint = 1;

    // Constructs the component for representing the solar system
    public SolarSystem() {
        setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
        bodies = new Body[] {
            addBody(1.9891e30,   0.000, 0.0000,  Color.YELLOW, "Sun"),
            addBody(3.3022e23,  57.909, 0.4787,  Color.CYAN,   "Mercury"),
            addBody(4.8685e24, 108.209, 0.3502,  Color.GREEN,  "Venus"),
            addBody(5.9736e24, 149.598, 0.29783, Color.BLUE,   "Earth"),
            addBody(6.4185e23, 227.939, 0.24077, Color.RED,    "Mars"),
            null,
        };
        resetShip();
    }

    // Returns the number of bodies in this solar system
    public int getElementCount() {
        return bodies.length;
    }

    // Returns the body in the solar system indexed as given.
    // The index should range between 0 and getElementCount() - 1
    public Body getElement(int index) {
        return bodies[index];
    }

    // You won't need to use this method: It handles processing
    // each frame
    public void actionPerformed(ActionEvent event) {
        for(Body b : bodies) b.updateDirection(this);
        for(Body b : bodies) b.step();
        framesUntilRepaint--;
        if(framesUntilRepaint <= 0) {
            framesUntilRepaint = 5;
            repaint(20);
        }
    }

    // You won't need to use this method: It is invoked whenever
    // the screen needs to be redrawn.
    public void paintComponent(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());
        for(Body b : bodies) b.paint(g);
    }
 
    // You won't need to use this method: It is invoked whenever the user
    // presses a keyboard key
    public void keyPressed(KeyEvent e) {
        int c = e.getKeyCode();
        if(c == KeyEvent.VK_UP || c == KeyEvent.VK_KP_UP
                || c == KeyEvent.VK_8 || c == KeyEvent.VK_NUMPAD8) {
            ship.setThrust(0.01);
        } else if(c == KeyEvent.VK_LEFT || c == KeyEvent.VK_KP_LEFT
                || c == KeyEvent.VK_4 || c == KeyEvent.VK_NUMPAD4) {
            ship.setRotation(-Math.PI / 200);
        } else if(c == KeyEvent.VK_RIGHT || c == KeyEvent.VK_KP_RIGHT
                || c == KeyEvent.VK_6 || c == KeyEvent.VK_NUMPAD6) {
            ship.setRotation(Math.PI / 200);
        } else if(c == KeyEvent.VK_SPACE) {
            resetShip();
        }
    }

    // You won't need to use this method: It is invoked whenever the user
    // releases a keyboard key
    public void keyReleased(KeyEvent e) {
        int c = e.getKeyCode();
        if(c == KeyEvent.VK_UP || c == KeyEvent.VK_KP_UP
                || c == KeyEvent.VK_8 || c == KeyEvent.VK_NUMPAD8) {
            ship.setThrust(0.0);
        } else if(c == KeyEvent.VK_LEFT || c == KeyEvent.VK_KP_LEFT
                || c == KeyEvent.VK_4 || c == KeyEvent.VK_NUMPAD4) {
            ship.setRotation(0.0);
        } else if(c == KeyEvent.VK_RIGHT || c == KeyEvent.VK_KP_RIGHT
                || c == KeyEvent.VK_6 || c == KeyEvent.VK_NUMPAD6) {
            ship.setRotation(0.0);
        }
    }

    // You won't need to use this method: It is invoked whenever the user
    // types a regular keyboard key. There is nothing to do here, since
    // all such typing is handled by the keyPressed and keyReleased methods.
    // But it is required by the KeyListener interface.
    public void keyTyped(KeyEvent e) { }

    // Creates a body with the given parameters.
    private Body addBody(double mass, double dist, double vel,
            Color hue, String name) {
        return new Body(DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 2 - dist,
            hue, mass, name, vel, 0);
    }

    // This is called whenever the spaceship is to be reset
    private void resetShip() {
        ship = new Spaceship(DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 2 - 49.397,
            0.24077, 0);
        bodies[bodies.length - 1] = ship;
    }

    // Creates a window with a SolarSystem component inside it.
    public static void main(String[] args) {
        SolarSystem system = new SolarSystem();

        JFrame frame = new JFrame("Solar System");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(system);
        frame.pack();
        frame.setVisible(true);

        system.addKeyListener(system);
        system.requestFocus();
        Timer tim = new Timer(5, system);
        tim.start();
    }
}
Add a comment
Know the answer?
Add Answer to:
Use the Circle class to draw each of the planets of our solar system. Draw the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I want to have a code that can pick whichever two planets in the solar system and find the distance of them no matter where in their orbit they are, but I keep getting errors. Can someone please help...

    I want to have a code that can pick whichever two planets in the solar system and find the distance of them no matter where in their orbit they are, but I keep getting errors. Can someone please help me fix it? f rom scipy import exp, pi, absolute, linspace import matplotlib. Ryplot as plt planet-I input ('Which planet do you want to pick for planet 1?") planet_2 input ('which planet do you want to pick for planet 27') distance...

  • Which pair of planets have semimajor axen that are the most dissimilar? O Earth and Mars...

    Which pair of planets have semimajor axen that are the most dissimilar? O Earth and Mars O Mercury and Earth O Venus and Mars O Mars and Jupiter QUESTION 2 Which object has the largest angular size as seen from Earth? O Sun Mercury O Venus O Mars O Jupiter O Saturn O Uranus O Neptune QUESTION 3 If you were flying through the solar system and your guidance system failed with your spacecraft moving in a random direction, what...

  • A well-known feature of our solar system is the asteroid belt between Mars and Jupiter. One...

    A well-known feature of our solar system is the asteroid belt between Mars and Jupiter. One theory about the asteroid belt is that it's made of primordial material that was prevented from forming another planet by the gravitational pull of Jupiter when the solar system was formed. One of the largest asteroids is 951 Gaspra. Its distance from the Sun is 207.16 million miles. Use your linear regression equation to interpolate the length of its sidereal year. Please take the...

  • Can someone help me with this question, I do not know how to do the long-distance...

    Can someone help me with this question, I do not know how to do the long-distance in excel. If you could show me the Excel data as well that would be great. retention 10.18 The following are the average distances of the planets in the solar system from the 25 Distance (millions of miles) 47.163 67.235 92.960 141.61 313.00 483.60 886.70 1783.0 2794.0 3666.0 Planet No. Planet Mercury Venus Earth Mars Asteroids Jupiter Saturn Uranus Neptune Pluto 4 10 (a)...

  • acair ay four significant digits 5-1. Verify the value given in Table 5-3-1 for rsoi of...

    acair ay four significant digits 5-1. Verify the value given in Table 5-3-1 for rsoi of the following planets with respect to the Sun (a) Mercury (b) Neptune (c) Jupiter -(d) Venus TABLE 5-3-1 RADIUS OF SPHERE OF INFLUENCE (SOI) Planet AU Meters Nautical miles Mercury 0.000747 .117x 10* 6.163 x 108 5.781 108 5.627 x 10° 8.664 x 1010 60,710 334,900 502,500 314,200 x I01 26,200,000 30,580,000 5.172 x 10°28,110,000 47,090,000 1,804,000 0.00411 Venus Earth Mars Jupiter Saturn Uranus...

  • VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE &...

    VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE & ANSWER/FOLLOW ALL THE QUESTIONS IN THE ASSIGNMENT! Planet Info Help When planning this project, review the concept of accessing object properties on another form. Form2.IblMessage.Text = "1122.25" or you can put a string or object instead of numbers You can then create one form for the Planets information and set all the properties before you display the Planet Form For the pictures of the...

  • So i need help with this program, This program is supposed to ask the user to...

    So i need help with this program, This program is supposed to ask the user to input their weight on earth so I would put "140" then it would ask "enter planet name, Min, Max or all" and if the user typed in all it would display all the planets and your weight on them. Right now Im suck on the part where im required to do a switch statement with a linear search. Below this im going to put...

  • Distance from Sun SQ of Orbital Orbital Period (Yrs) 0.2 0.6 Distance from Sun CUBED (S3)...

    Distance from Sun SQ of Orbital Orbital Period (Yrs) 0.2 0.6 Distance from Sun CUBED (S3) 0.059319 0.37793307 Planet Period (P2) .04 .36 Mercury Venus Earth Mars 0.39 0.723 1 1.9 11.9 29.5 84 164.8 1 1.524 5.203 9.539 19.18 30.06 39.53 1 3.61 141.61 870.25 7,056 27,159.04 61,504 1 3.53960582 140.8515 867.977658 7,055.79263 27,162.3242 61,770.4042 Jupiter Saturn Uranus Neptune Pluto (Dwarf) 248 From the data above, make a plot of (Orbital Period)2 vs. (Distance from the Sun)3 andd paste...

  • Please explain step by step! Useful Planetary Data Mean Radius (m) 243 × 106 6.06 ×...

    Please explain step by step! Useful Planetary Data Mean Radius (m) 243 × 106 6.06 × 10° 6.38 × 10° 3.37 × 106 6.99×107 5.85 × 10' 2.33 × 107 221 × 107 1.14 × 10° 1.74 × 10° 696 × 108 Period (s) 7.60 × 106 1.94 × 107 3.156 × 10, 5.94 × 10' 3.74 × 108 9.35 × 108 2.64 × 109 5.22 × 109 782 × 109 Mean Distance from Sun (m) 579 × 100 1.08...

  • 1 (10 pts). In 1601 the German astronomer Johannes Kepler became director of the Prague Observatory....

    1 (10 pts). In 1601 the German astronomer Johannes Kepler became director of the Prague Observatory. Kepler had been helping Tycho Brache in collecting 13 years of observations on relative motion of the planet Mars. By 1609, Kepler had formulated his first two laws of planetary motion: i. Each planet moves on an ellipse with the sun at one focus. ii. For each planet, the line from the sun to the planet sweeps out equal areas is equal times. Kepler's...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT