// // DrawJupiter.java -- display positions of Jupiter's moons. // Copyright 1996 by Akkana Peck. // // package Akk.Astro; import java.awt.*; import java.applet.*; import java.util.Date; public class DrawJupiter extends Applet { Jupiter jupiter; // Size constants: final static int jsize = 10; // planet's radius in pixels final static int moonsize = 4; // moons' size in pixels final static int redSpotWidth = 7; final static int redSpotHeight = 5; // Copies of the UI components we maintain: Canvas drawingArea; TextField dateField; Choice invertMenu; Panel controlPanel; Color planetColor; Color moonColor; Color shadowColor; Color lablColor; Color bandColor; Color spotColor; public String getAppletInfo() { return "Jovian Moon Locator.\nCopyright 1996 by Akkana Peck"; } public String[][] getParameterInfo() { String[][] info = { { "time", "double", "Time of chart (optional, defaults to current)" }, }; return info; } public void init() { planetColor = null; StarDate now = null; String s = getParameter("time"); if (s == null) now = new StarDate(); else now = new StarDate(s); // // Set some layout info: // setLayout(new BorderLayout(0, 1)); // // Create the star chart object // drawingArea = new Canvas(); add("North", drawingArea); dateField = new TextField(now.toString(), 12); add("South", dateField); } public boolean handleEvent(Event event) { if (event.id == Event.ACTION_EVENT && event.target == dateField ) { try { StarDate newdate = new StarDate(dateField.getText()); // Might throw IllegalArgumentException if user // typed in the wrong format. } catch (IllegalArgumentException e) { dateField.setText(new StarDate().toString()); // should display errmsg ... } finally { repaint(); return true; } } return super.handleEvent(event); } public void paint(Graphics g) { StarDate date = new StarDate(dateField.getText()); if (jupiter == null) jupiter = new Jupiter(date); else jupiter.setDate(date); Dimension drawsize = new Dimension(size().width, size().height - dateField.size().height); int width = drawsize.width; int height = drawsize.height; // Do some color manipulation: if (planetColor == null) { drawingArea.setBackground(Color.black); drawingArea.setForeground(Color.white); planetColor = Color.yellow; moonColor = drawingArea.getForeground(); shadowColor = Color.black; lablColor = Color.white; bandColor = Color.orange; spotColor = Color.red; } g.setColor(drawingArea.getBackground()); g.fillRect(0, 0, width, height); // draw the planet itself: g.setColor(planetColor); int xcenter = width/2; int ycenter = height/2; g.fillOval(xcenter-jsize, ycenter-jsize, jsize*2, jsize*2); XYCoord coord; // Draw a couple of fake bands: g.setColor(bandColor); for (int i=0; i < 4; ++i) { g.drawLine(xcenter-jsize+i, ycenter-i-2, xcenter+jsize-i, ycenter-i-2); g.drawLine(xcenter-jsize+i, ycenter+i+2, xcenter+jsize-i, ycenter+i+2); } // Find the red spot: coord = jupiter.getRedSpotXY(); if (coord.x != Double.POSITIVE_INFINITY && coord.y != Double.POSITIVE_INFINITY) { g.setColor(spotColor); // We only want the spot to draw to the edge of the disk, // so adjust the width if necessary: // (approx) width/2 of the planet (pixels) at the spot's latitude: int j2width = (int)(jsize - Math.abs(coord.y * jsize)/3); int x = xcenter - (int)(coord.x * jsize); int y = ycenter + (int)(coord.y * jsize) - redSpotHeight/2; int spotwidth = redSpotWidth; if (x + redSpotWidth > xcenter + j2width) { spotwidth = xcenter + j2width - x; x = xcenter + j2width - spotwidth; } else if (x - redSpotWidth/2 < xcenter - j2width) { spotwidth = x + redSpotWidth/2 - xcenter + j2width; x = xcenter - j2width; } else { x -= redSpotWidth/2; } g.fillOval(x, y, spotwidth, redSpotHeight); } // draw each of the moons: int whichmoon; for (whichmoon=0; whichmoon < jupiter.getNumMoons(); ++whichmoon) { coord = jupiter.getMoonXY(whichmoon); if ((coord.x != Double.POSITIVE_INFINITY) && (coord.y != Double.POSITIVE_INFINITY)) { int x = xcenter - (int)(coord.x * jsize) - moonsize/2; int y = ycenter + (int)(coord.y * jsize) - moonsize/2; g.setColor(drawingArea.getBackground()); g.drawOval(x-1, y-1, moonsize+2, moonsize+2); g.setColor(moonColor); g.fillOval(x, y, moonsize, moonsize); g.setColor(lablColor); g.drawString(jupiter.getMoonLabel(whichmoon), x - moonsize, ycenter - 3*moonsize); } // See if this moon is casting a shadow: coord = jupiter.getMoonShadowXY(whichmoon); if (coord.x != Double.POSITIVE_INFINITY && coord.y != Double.POSITIVE_INFINITY) { g.setColor(shadowColor); g.fillOval(xcenter - (int)(coord.x * jsize) - moonsize/2, ycenter + (int)(coord.y * jsize) - moonsize/2, moonsize, moonsize); } } // end loop over moons } }