// starter file for Solar System assignment // Kees Huizing // 16 October 2012 import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class SolarSystem extends JPanel implements ActionListener, AdjustmentListener, ChangeListener { double timestep = 0.5; // simulated time that passes between frames; in days int delay = 20; // interval between frames; in ms Timer timer; CelestialBodyExtra sun; // the center around which everything revolves // GUI components JFrame frame; JPanel buttonPanel; // holds buttons JScrollBar jscb; // determines animations speed JSlider speedSlider; JButton startButton; // starts animation JButton stopButton; // stops animation public static final double PI2 = 2 * Math.PI; public void paintComponent(Graphics g) { super.paintComponent(g); // background int x = getWidth() / 2; // draw in middle of panel int y = getHeight() / 2; sun.draw(g, x, y); // from here all other bodies are drawn } public SolarSystem() { this.buildGUI(); this.buildBodies(); } // make planets and satellites and connect them void buildBodies( ) { CelestialBody mercury = new CelestialBody(/*fill in*/); CelestialBody[] sunSats = {mercury}; //this.sun = new CelestialBodyExtra(sunSats, /* fill in*/); } void buildGUI() { this.frame = new JFrame("SolarSystem"); this.frame.add(this, BorderLayout.CENTER); this.speedSlider = new JSlider(); this.frame.add(this.speedSlider, BorderLayout.SOUTH); this.speedSlider.addChangeListener(this); this.buttonPanel = new JPanel(); this.frame.add(buttonPanel, BorderLayout.NORTH); this.startButton = new JButton("Start"); this.startButton.addActionListener(this); this.buttonPanel.add(startButton); this.stopButton = new JButton("Stop"); this.stopButton.addActionListener(this); this.buttonPanel.add(stopButton); this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.frame.setSize(600, 500); this.frame.setVisible(true); } // sets timer interval, and with that the speed of the planets // 1 timer step moves everything the equivalent of timestep days public void setDelay(int delay) { this.timer.setDelay(delay); } public void actionPerformed(ActionEvent e) { // } public void adjustmentValueChanged(AdjustmentEvent e) { System.out.println( e.getValue() ); } // default slider runs from 0 to 100 public void stateChanged(ChangeEvent e) { // get the position of the slider int position = this.speedSlider.getValue(); // update timestep } public static void main(String[] a) { new SolarSystem(); } } // Simple celestial body, without satellites class CelestialBody { //instance variables: size etc. // constructor // draw celestial body and trajectory // x, y central pivot point (position "sun") void draw(Graphics g, double x, double y) { // draw trajectory g.setColor( Color.gray ); // etc. // calculate position // draw body } // move the body for a distance corresponding to time t void step(double t) { // update angle } } // Using inheritance a simple celestial body is extended with an array of satellities // NB Satellites are themselves also celestial bodies, so here the subclass // extends the superclass with an array of elements that are superclass objects. class CelestialBodyExtra extends CelestialBody { CelestialBody[] satellites; // or use an ArrayList /* public CelestialBodyExtra(...) { super(...); // set satellites } */ void draw(Graphics g, double x, double y) { // draw CelestialBody super.draw(g, x, y); // draw satellites // fill in // calculate own position // call draw on satellites } void step(double t) { // similar to draw } }