Some Light Relief—Sky View Cafe: A High Quality Applet

For the end of this chapter, I want to introduce you to the Sky View Cafe program and its author, Kerry Shetline. Sky View Cafe is an astronomy applet, but even if you have no interest in astronomy, it's a terrific showcase for the very professional graphics effects that a careful programmer can achieve.

In case you are interested in astronomy, I'll mention that Sky View Cafe displays many types of astronomical information, and is particularly easy to use. It shows star charts, rise and set times for the Sun, Moon, and planets, Moon phases, orbital paths of the planets in 3-D (I love animating that one!), a perpetual calendar with astronomical events, lunar and solar eclipses, the moons of Jupiter and Saturn, and more. See Figure 22-12 for the main screen.

Figure 22-12. The Sky View Cafe astronomy applet

image

That screen shows the half of the world that is in darkness and the half that's in daylight at that moment. It's a funny shape because the world is round and flattened out into an unrolled cylinder on the screen. You can see that the Antarctic is enjoying 24 hours of daylight at the moment.

The first thing to do is to click on the map to tell the program where you are on the planet. That makes the night sky maps accurate for your position. Instead of clicking, you can look up your city name by clicking the Find button on the right hand side. Since ordinary applets can't write locally, it will tell the server to set a client side cookie, saving your location for future runs. Now you can select one of the other tabs on the tabbed pane. Printing is done in a very clever way: it causes a showDocument(), which brings up a new browser containing the thing you want printed. Then you use the browser's print command! Clever—and it avoids much tricky code.

We won't run through all the features of Sky View Cafe here. Part of the fun is exploring it for yourself. Kerry has a strong background in writing “what if” software that makes it easy to try things and easy to back out of them. He started professional programming writing programs for that excellent magazine, Creative Computing (long since defunct). Later, Kerry worked in C++ for seven years. He has used Java for the last six years, and points out that he can get more done faster in Java because his programs run on all computers and operating systems.

Following is a code snippet from the class that represents our solar system:

package org.shetline.astronomy;

import java.util.*;
import java.io.*;
import org.shetline.util.*;
import org.shetline.math.*;

public class SolarSystem extends MathUser implements AstroConstants
{
   protected HeliocentricPlanets    planets = null;
   protected EclipticBody           moon = null;
   protected Pluto                  pluto = null;
   protected String[]               planetNames;

   // Result in days per revolution.
   //
   public double getMeanOrbitalPeriod(int planet)
   {
      if (planet < MERCURY || planet > PLUTO)
         return 0.0;

      // Convert degrees per Julian century into days per revolution.
      return 100.0 * 365.25 * 360.0 / elems[planet - MERCURY][0][1];
   }

This code implements, in part, one of the Sky View Cafe features that I really like, which is the animated model of the solar system known as an Orrery. It's the third tabbed pane, “Orbits.” Clicking it brings up a display showing the paths of the nine planets, including Earth, that orbit our star. You can adjust the angle from which you're viewing the solar system by dragging on it (Figure 22-13 illustrates this).

Figure 22-13. Moving model of the solar system

image

If you highlight a single digit in the “Date” field at the top left and then press the spinner (one of a pair of small arrows to the right) to adjust the date, you can advance the model by that amount (minute, hour, day, month, year, decade, etc.). When you do this, the planets move in their orbits correspondingly. If you keep the spinner pressed, it keeps incrementing time, and keeps moving the planets around. Beautiful!

The name “Orrery” (for a moving model of the solar system) comes from the Earl of Orrery (a small place in Ireland) who paid a clock maker to build him one in 1712. The device was created by George Graham, so it really should be a “Graham.”

Kerry explained how he designed and coded Sky View Cafe. You have to have a strong interest in the subject area that you're programming. It helps to look for other programs that do related things. Other programs might have some good ideas you can re implement, or have some bad ideas that you can avoid.

Newsgroups can provide helpful information, and Sun hosts some Java-specific newsgroups at the Java Developer Connection at developer.java.sun.com. Kerry also urges programmers to read the javadoc-generated API documentation for each library class they want to use. There's lots of information in there, but it won't do you any good unless you read it.

You need to think ahead when you write any class. Try to imagine how it might be reused somewhere later, and write it accordingly. Don't build in knowledge about the current context, but pass it in as parameters. Perhaps the class can be generalized by making it abstract, and then subclassing it to provide more detail. Kerry is a firm supporter of early prototyping for GUI code. Get a basic framework running reliably so that you can see how your ideas work in practice. Add to this framework in gradual steps. Avoid a development style where you are coding for months without having enough to test or run.

An important feature of Sky View Cafe is the amount of effort that went into making it a well-behaved applet. Although it's big at approximately 375 Kbytes (one minute to download on a phone line), it has been crafted to run in just about any browser, and to avoid many known browser bugs. The code is all in JDK 1.1 for this reason. Kerry develops on a Macintosh using the CodeWarrior IDE, and tests on almost all platform/browser revision combinations.

Sky View Cafe was a labor of love, and it took Kerry about 18 months of hobby project time (evenings and weekends) to bring it to this state. Too many open source projects reach version 0.8, and the programmer loses interest. Sky View Cafe is now at version 3.0.4 with 4.0 under development, and it is open source shareware. Kerry has generously published the source so others can learn from his work, and he invites people to register if they find the program useful. The current version of Sky View Cafe is online at the website www.shetline.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.214.27