3D Astronomy with JAVA

3D Astronomy with JAVA

  • $30.95
    Unit price per 

By Randall S. Fairman.

Product Information: 6.00" by 9.00", 374 pages, Softbound.

This book is for people who want to learn about astronomy, how to generate three-dimensional computer graphics and about the programming language Java.

Full Table of Contents (PDF)

JAVA files and their relationships (PDF)

Astronomy and computer graphics are naturally complementary. Graphics programming is an excellent way to get a feel for “what’s happening” in the sky. It's difficult to gain an intuitive feel for complex phenomena, like precession, without seeing some examples. A graphics program literately shows you what's happening. Moreover, computer graphics and positional astronomy are both based on applied geometry. There's a lot of overlap in the mathematics used in the two subjects.

The graphics techniques presented are the foundation on which many popular three-dimensional games and computer-generated animations are based. The extensive bibliography provides a selection of books on computer graphics for those wishing to delve deeper into this subject.

Roughly half of the book consists of an introduction to Java, the rudiments of graphics programming, and the most fundamental ideas of astronomy. More than a dozen small programs are developed along the way that illustrate these ideas.

The second half of the book continues by applying the code developed earlier to larger-scale projects. Most of the programs in the later half of the book are based on data from the Jet Propulsion Laboratory (JPL). The JPL provides files giving very precise position data for the planets over a period of roughly 6,000 years.

The book goes on to explain the ideas on which commercial planetarium programs are based by developing a program with similar basic features. This final project displays an animation showing what an observer on the surface of the earth would see in the sky from a given time and place.


My aim has been to write a book that can be read profitably by anyone from a high school student (with strong computer skills and access to a knowledgeable teacher) to a person with a great deal of computer experience and knowledge of astronomy. ""Profitably"" will mean different things for different people. A high school student might take away a broader understanding of computer programming and better knowledge of astronomy. A college professor or high school teacher might glean some ideas for pedagogical tools to be used in the classroom.

As evidence that a high school student can read and understand this book, I acknowledge that my daughter, Evelyn, who was then in ninth grade, found some rather embarrassing mathematical mistakes. Thanks, Evelyn!

In terms of mathematical prerequisites, the reader needs to have a good grasp of algebra and trigonometry. In addition, an understanding of matrix multiplication will make the learning curve easier to climb. To make these topics more accessible, they are covered in an Appendix; in theory, anyone who has had a year of high school algebra could pick up the necessary background from the Appendix. But learning math is a funny thing; an exposition that's nearly incomprehensible to a novice is seen by someone with more experience as painfully detailed. The Appendix is complete, but the ideas found there will take time and practice to fully digest.

To make the book more accessible, I've gone to some pains to avoid using calculus, although there are a few places where non-rigorous arguments that have the flavor of calculus are used. Look up ""calculus'' in the index to find these points. The golden hammer provided by calculus could have been used to shorten the discussion at a few points, but looking at a problem from a calculus-free point of view can be more informative.

Although no calculus is used, the typical way in which math and science courses are sequenced means that a reader with no need to refer to the Appendix will probably have had calculus, along with a course in physics, multi-variable calculus or linear algebra. In my view, waiting to introduce the material covered by the Appendix until the first or second year of college is unfortunate. This material could be taught in high school immediately after trigonometry. It's not difficult, and the students would see an immediate and concrete use for algebra and trigonometry.

While I've done my best to avoid bringing in more mathematics than is strictly necessary, it's also true that this text has plenty of detailed formulas and derivations. The basic mathematical ideas may be elementary, but they are put to some fairly sophisticated uses. Readers who find that their mathematical skills are occasionally stretched should feel free to skip ahead when their interest flags. The computer programs are the heart of the book, and mathematical ideas, especially those as concrete as the geometrical ideas used here, are easiest to grasp in the context of a physical problem.

The computer-related prerequisites were harder to reduce. In many ways Java is an ideal language for someone with limited programming experience, but providing all of the background necessary for a reader without any programming experience would have made the book too long. Java is explained here in enough detail to allow this book to be used as a starting point in learning about Java, but readers without any programming experience should read this book in conjuction with something more introductory. Several books that could be read for additional programming background can be found in the Bibliography. Unfortunately, as Java has grown in power and scope over the years, introductory books have become thicker to keep up; it can be very difficult for a novice to know where to start. Moreover, introductory programming books often leave the reader wondering what to do with their new knowledge. I hope that this book serves as a starting point for those who want to learn more about Java, while providing some meaty and rewarding applications.


The astronomy is approached from a naive point of view. Under relativity, time and position are affected by the observer's motion, position and proximity to massive bodies. All of these effects are neglected; the treatment is entirely classical. The measurement of time is also treated naively. Not only is relativity neglected, but only three measures of time are introduced: time relative to the stars (sidereal time), time as defined by the earth's rotation relative the sun, and time relative to the fictitious mean sun. The later is used to define ""clock time''. People with a more sophisticated knowledge of astronomy may find this to be simplistic. If you know enough to disagree with the presentation, then it should be easy enough for you to recast it.

The approach taken to the treatment of time is consistent with the approach to accuracy. I have chosen clarity of presentation over extreme accuracy. Astronomers have been working for centuries to determine highly accurate formulas for various quantities. To simply state a formula is of no use in improving understanding, no matter how accurate the formula may be. For example, only the first few terms of the equations describing nutation are given. If you want to calculate the exact moment of eclipses of the moon over many years, then additional terms of the formula are needed. Once you understand what's going on, a more accurate version is relatively easy to produce and you'll know what you're doing rather than blindly running data through a program.

Other topics that are necessary to obtain the greatest accuracy are covered only briefly, aberration for instance. In summary, what is presented is accurate, though greater accuracy could be obtained by extending certain calculations. Points at which the programs' accuracy could be improved are pointed out in the text, along with references to books that discuss these points.

If this discussion leaves you scratching your head, don't worry about it. The aim of the book is to develop programs that produce a faithful display of celestial phenomena. None of the simplifications or omissions made in the pursuit of clarity have any visible effect on the graphics output.

On the other hand, certain topics that are frequently passed over are treated in great detail. See the discussion of change of coordinates in Section 6.1 or the discussion of the equation of time in Section 6.3. Very often these topics are treated by stating the chief points and leaving the details to be worked out by the reader.

The Code

The code is an integral part of the text. Writing computer programs off and on professionally over many years has shown me the value of well-commented code. I have aimed to make the text clear enough so that the code can be treated as a black-box, but reading the code will certainly improve your understanding of the underlying ideas.

The programs were written with the goal of making them easy to understand, rather than focusing on elegance, using them to show off my cleverness or how cool Java is. This means, for example, that some of the code is repetitive. Each program should be understandable in isolation without requiring that the reader unravel a long chain of inter-relationships among files. See also the Section, ""Files and their Relationships'', which follows this Preface. I expect that, no matter how much thought I put into the code, readers with ideas for programs of their own will need to hack up my work to implement their own ideas. I've tried to make that as easy as possible.

Each line of code falls into one of two categories: code related to the user-interface and code that implements an idea from mathematics or astronomy. In either case, the code may not be fully understood the first time around. There are many suggestions for modifications and improvements to the programs, so play around with the code; that's the best way to figure out what it does. One of the nice things about graphics programming, compared to straight numerical programming, is that if there's a mistake, then it's usually obvious visually.

If you don't understand exactly how a program works, don't worry about it. If you ran the program, then you learned something, and the repetitive nature of much of the code means that the same troublesome topic is likely to come up again in a slightly different context. After you've seen enough examples, it will sink in.

In practice, the degree to which the code given here is unique to Java is limited. Mostly, this has to do with such things as creating windows, menus, and the user-interface in general. This is a limited amount of the code and could easily be taken as simply given. The overwhelming bulk of the code is indistinguishable from C/C++.

Java, like most event-driven object-oriented programming languages, can be hard to follow at first because so much of the code is plumbing. That's what makes it so effective. The language provides a huge array of tools with various ways to link them together. A program is the infrastructure that makes these tools work together. This is especially true of the user-interface aspects of a program.

For readers who have never worked with Java, the learning curve will be steeper, but it's worth the effort. Java is real; Java programs can include the same user-interface bells and whistles as a language like C, and such a C program requires libraries for a particular operating system. The same Java program will run on most common operating systems: Linux, Microsoft Windows, Macintosh and others. Another reason that I chose Java for this book is that Java is freely available over the internet, and it's open source.


Chapter 1 gives a quick (about 50 pages) overview of Java. If you're in a hurry to run the programs and are less interested in understanding how they work, then the first two sections of Chapter 1 explain how to install and use Java. If you've already done some Java programming, then you can skip this Chapter, though you should take a quick look at Section 1.6, which describes a few commonly used utility classes.

Chapter 2 is primarily mathematical. It lays the groundwork needed by the various graphics programs. The most important topics of the Chapter are rotation and reflection, and a discussion of the ways in which a three-dimensional scene can be rendered on a two-dimensional computer monitor.

Chapter 3 explains how to draw a shaded sphere with a single light source. How Java handles windows and interaction with the keyboard and mouse are also discussed. Most of the programs developed later use the ideas and code developed in this Chapter. The programs begin with a static image of a shaded sphere, and end with a program that shows a sphere with surface details, as seen by a mobile observer. The program developed in Section 3.2 is the foundation for many of the programs later in the book. Section 3.4 is primarily for fun, and could be skipped.

Chapter 4 discusses Kepler's laws of planetary motion and ellipses. Section 4.2, which discusses lines which are tangent to an ellipse, is a side issue and could be skipped. The most important topic is Kepler's equation, which is treated in Section 4.4.2. Kepler's equation is needed primarily in Section 6.3, where the equation of time is discussed. The Chapter closes with an animation of the motion of a planet around the sun; the program displays the time, mean anomaly, true anomaly and eccentric anomaly of the planet over time.

Chapter 5 states the most elementary facts about astronomy and includes several animation programs to illustrate them.

Chapter 6 continues by discussing the most important geometrical formulas used in astronomy, and implementing them in Java. One of the programs developed in Chapter 6 can be used to animate the variation of an arbitrary function over time. That program is then applied to the equation of time.

Chapter 7 explains the JPL data and develops two classes that are used to access it. The Chapter closes with a program that can be used to determine the astronomical coordinates of a planet relative to an observer at any given time and place. Most of this Chapter is devoted to data-jockeying, and could certainly be skipped; there are only two essential classes. Section 7.5 describes a class that must be used to convert the JPL data to a format that's more compatible with Java. The class given in Section 7.3 is used to access the JPL data once it's been converted to Java format. Understanding exactly how these classes work is not important, though they are fully explained; you only need to know how to use them.

Chapter 8 uses the JPL data to develop two different animations of the solar system. This is the first program that uses menus.

Chapter 9 adds the stars to the animation of the planets that appears in the previous Chapter. An animation of the proper motion of the stars is also given.

Chapters 10 and 11 develop a program that animates a view of the sky from the perspective of an observer on the surface of the earth at a particular time and place. These last three chapters are relatively brief, and consist primarily of source-code.

Appendix A includes a discussion of spherical trigonometry. Even those with a strong background in mathematics may never have studied this topic. If the mathematics is already overwhelming, then this could be skipped; it's needed primarily in Section 6.1. The Appendix includes a class that implements many of the formulas that appear there.