import java.applet.*; import java.awt.*; import java.awt.image.*; import java.lang.Math; import java.awt.event.*; import java.net.*; import java.util.*; import java.lang.*; // use the old event model to be compatible with Java 1.1.5 public class SatWorks extends Applet implements Runnable, ActionListener, ItemListener, AdjustmentListener, KeyListener, MouseListener, MouseMotionListener { Button button; Checkbox checkbox; Checkbox leoCheck, geoCheck; Label label; Choice choice; Graphics gb; Scrollbar scrollbar; Image buffer; Image satImage, laloImage, worldImage; RotateGlobe rg; String param; int longi = 90; double Altitude = 5.; boolean isRotating = true; int R = 90; double satAng = 0.; double declination = 15. * .0174533; double swathWidth = 10.; int ratioMax = 23; // ratio of sat orbits to earth rotations int spotSize = 3; int numSpots = 5; public void init() { laloImage = loadImage("latlon.gif"); worldImage = loadImage("world.gif"); satImage = loadImage("satellite.jpg"); rg = new RotateGlobe(laloImage, worldImage,R); rg.setSwath(declination, swathWidth, spotSize, numSpots); buffer = createImage(500,500); gb = buffer.getGraphics(); // defining widgets button = new Button("Button"); button.addActionListener(this); checkbox = new Checkbox("checkbox"); checkbox.addItemListener(this); choice = new Choice(); choice.add("one"); choice.add("two"); choice.addItemListener(this); // orientation, initial value, size of bubble, minimum, maximum scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 110); scrollbar.addAdjustmentListener(this); label = new Label("The Label",Label.CENTER); // parsing parameters param = getParameter("parameter"); if (param != null) { StringTokenizer st = new StringTokenizer(param, ","); int n = st.countTokens(); for (int i=0; i= 180.) satAng = -satAng; int ysat = (int) Math.round((R+Altitude)*Math.cos(satAng*.0174533) * Math.cos(declination)); int xsat = (int) Math.round(ysat*Math.tan(declination)); double rr = Math.sqrt((double)ysat*ysat + (double)xsat*xsat); if ((Math.abs(rr) < R+4) && satAng < 0.0) { } else { gb.setColor(Color.black); gb.drawRect(center+xsat-2,center-ysat-2,5,5); gb.setColor(Color.white); gb.drawRect(center+xsat-1,center-ysat-1,3,3); if (Math.abs(rr) < R) rg.mapSatellite(xsat,ysat); } gb.setColor(Color.orange); g.drawImage(buffer,0,0,this); } // Listeners public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { isRotating = !isRotating; System.out.println("#### The button was pressed"); } } public void itemStateChanged(ItemEvent e) { if (e.getSource() == checkbox) { System.out.println("#### Checkbox set to "+checkbox.getState()); } if (e.getSource() == choice) { System.out.println("#### Choice selected "+choice.getSelectedItem()); } } public void adjustmentValueChanged(AdjustmentEvent e) { if (e.getSource() == scrollbar) { System.out.println("#### Scrollbar value = "+scrollbar.getValue()); } } // for MouseListener public void mouseClicked(MouseEvent e) { int mx = e.getX(); int my = e.getY(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} //for MouseMotionListener public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} //for KeyListener public void keyPressed(KeyEvent e) { int k = e.getKeyCode(); char ck = e.getKeyChar(); } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} // next, handle running a Thread to automatically repaint // the display every "refreshRate" milliseconds private Thread animator_thread = null; private boolean isRunning; int refreshRate = 20; int ratio = 0; public void start() { if (animator_thread == null) { isRunning = true; animator_thread = new Thread(this); animator_thread.start(); } } public void stop() { if ((animator_thread != null) && animator_thread.isAlive()) { isRunning = false; animator_thread.stop(); } animator_thread = null; } public void run () { while (isRunning) { try { Thread.sleep(refreshRate); if (isRotating) { ratio = ratio + 1; if (ratio > ratioMax) { longi = longi - 1; if (longi < 0) longi = 360; ratio = 0; } } repaint(); } catch (Exception re) { System.out.println("#### "+re); } } } // Utilities for getting images and audio clips from JAR or URL public Image loadImage(String name) { // first try the URL Image img = null; URL u = getClass().getResource( (String) name); if (u == null) { u = getDocumentBase(); img = getImage(u,name); } else { img = getImage(u); } MediaTracker mt = new MediaTracker(this); mt.addImage(img, 0); try { mt.waitForAll(); } catch ( Exception ex ) {img = null; } return img; } } class RotateGlobe extends Component { byte[] pixels, pixWorld; int LINES=181; int ELEMS=361; ColorModel cm; int centerlon = 0; double declination, swathWidth; int spotSize, preferredSpots; int R, N; int[] line2lat; int[][] ele2lon; int[] swathX, swathY; Image img; MemoryImageSource mis = null; byte[] pixs; // long domain of image 0:360 public RotateGlobe(Image lalo, Image world, int size) { R = size; N = 2 * R + 1; line2lat = new int[R+1]; ele2lon = new int[R+1][R+1]; try { PixelGrabber pg = new PixelGrabber(lalo,0,0,ELEMS,LINES,false); pg.grabPixels(); pixels = (byte[]) pg.getPixels(); cm = pg.getColorModel(); int stat = pg.status(); PixelGrabber pgw = new PixelGrabber(world,0,0,ELEMS,LINES,false); pgw.grabPixels(); pixWorld = (byte[]) pgw.getPixels(); stat = pgw.status(); } catch (Exception e) { System.out.println("#### grabbing:"+e); } pixs = new byte[N * N]; // compute lats & lons for quadrant 1 only since others // can be derived from that... for (int y=0; y Math.abs(xe -xb) ) n = (int) Math.abs(ye - yb); if (n > preferredSpots) n = preferredSpots; double xstep = (xe - xb)/n; double ystep = (ye - yb)/n; double lat, lon; int z; for (int i=0; i 1.0) continue; lon = Math.asin(lon)/.0174533 + centerlon; lat = lat/.0174533; if (lon < 0.) lon = 360.+lon; if (lon > 360.) lon = lon - 360.; z = ELEMS * (int)Math.round(90-lat) + (int)Math.round(lon); if (spotSize == 1) { pixWorld[z] = pixels[z]; } else { int zz; for (int m=-spotSize/2; m= 0 && zz < (LINES*ELEMS)) pixWorld[zz] = pixels[zz]; } } } //System.out.println("#### x="+x+" y="+y+" lat="+lat+" lon="+lon); } /* for (int i=z-3; i R) { yi = N - 1 -y; yp = -1; } else { yi = y; yp = +1; } for (int x=0; x R) { xi = x - R; xp = 1; } else { xi = R - x; xp = -1; } if (ele2lon[xi][yi] == -999) { pixs[z] = (byte)0; // should be black } else { try { pixs[z] = pixWorld[ ((longitude+xp*ele2lon[xi][yi]) % ELEMS) + (ELEMS * (90 - yp*line2lat[yi])) ]; } catch (Exception e2) { //System.out.println("#### yp, yi, l2l = "+yp+" "+yi+" "+line2lat[yi]); } } z = z + 1; } } if (mis == null) { mis = new MemoryImageSource(N,N,cm,pixs,0,N); mis.setAnimated(true); img = createImage(mis); } else { mis.newPixels(0,0,N,N,false); } return img; } }