mostrar mundo dia y noche en java con la hora mundial en tiempo real

Technology

mostrar mundo dia y noche en java con la hora mundial en tiempo real



bueno asi es como dice el titulo ó como la imagen de arriba este apple muestra la linea del sol a traves del mundo en dia y noche y muestra el horario de cada pais ó mejor dicho horario continental el apple a traves de las horas ira moviendo la linea de luz que se plasma en el mundo, para crear el apple primero creamos un nuevo proyecto y dentro de ese proyecto un nuevo pakete yo le puse  nochedia dentro del pakete nochedia
creamos una nueva clase y la llamamos SunClock  dentro de esa clase ponemos el siguiente codigo:




package nochedia;
/**
 * @(#)SunClock.java
 */

//package es.fcocascales.worldwatch;

import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;

/**
 * Sun astronomical routines.
 * ----------------------------------------------
 *
 * Sun clock.  X11 version by John Mackin.
 *
 * This program was derived from, and is still in part identical with, the
 * Suntools Sun clock program whose author's comment appears immediately
 * below.  Please preserve both notices.
 *
 * The X11R3/4 version of this program was written by John Mackin, at the
 * Basser Department of Computer Science, University of Sydney, Sydney,
 * New South Wales, Australia; .  This program, like
 * the one it was derived from, is in the public domain: `Love is the
 * law, love under will.'
 *
 * ----------------------------------------------
 * 
 * Sun clock
 * 
 * Designed and implemented by John Walker in November of 1988.
 * 
 * Version for the Sun Workstation.
 * 
 *     The algorithm used to calculate the position of the Sun is given in
 *    Chapter 18 of:
 *
 *    "Astronomical  Formulae for Calculators" by Jean Meeus, Third Edition,
 *    Richmond: Willmann-Bell, 1985.  This book can be obtained from:
 *
 *       Willmann-Bell
 *       P.O. Box 35025
 *       Richmond, VA  23235
 *       USA
 *       Phone: (804) 320-7016
 *
 *    This program was written by:
 *
 *       John Walker
 *       Autodesk, Inc.
 *       2320 Marinship Way
 *       Sausalito, CA  94965
 *       USA
 *       Fax:   (415) 389-9418
 *       Voice: (415) 332-2344 Ext. 2829
 *       Usenet: {sun,well,uunet}!acad!kelvin
 *	   or: kelvin@acad.uu.net
 *
 *    modified for interactive maps by
 *
 *	Stephen Martin
 *	Fujitsu Systems Business of Canada
 *	smartin@fujitsu.ca
 *
 *    This  program is in the public domain: "Do what thou wilt shall be the
 *    whole of the law".  I'd appreciate  receiving  any  bug  fixes  and/or
 *    enhancements,  which  I'll  incorporate  in  future  versions  of  the
 *    program.  Please leave the original attribution information intact	so
 *    that credit and blame may be properly apportioned.
 *
 *    Revision history:
 *
 *	1.0  12/21/89  Initial version.
 *	      8/24/89  Finally got around to submitting.
 *
 *	1.1   8/31/94  Version with interactive map.
 *	1.2  10/12/94  Fixes for HP and Solaris, new icon bitmap
 *	1.3  11/01/94  Timezone now shown in icon
 *	1.4  03/29/98  Fixed city drawing, added icon animation
 *
 * ----------------------------------------------
 *
 * Adapted to Java
 *
 * @version 9-X-1999 
 * @author Francisco Cascales  
 */
public class SunClock 
{
  private final static double sgn (double x) { return (x < 0) ? -1 : (x > 0)? 1 : 0; } // Extract sign
  private final static double fixAngle (double a) { return a - 360d * Math.floor (a / 360d); } // Fix angle

  private final static double TERMINC = 100d; // Circle segments for terminator
  private final static double PROJINT = 60d * 10d; // Frequency of seasonal recalculation

  private static GregorianCalendar gregorian = new GregorianCalendar ();
  static { // Normalize to GMT
    gregorian.set (Calendar.ZONE_OFFSET, 0);
    gregorian.set (Calendar.DST_OFFSET, 0); 
  }

  private static SunPosition sun = new SunPosition ();

  /** 
   * Project illuminated area on the map.
   *
   * @param wtab   
   * @param xDots  
   * @param yDots  
   * @param dec    
   */
  final static void projectIlluminatedArea (int[] wtab, int xDots, int yDots, double dec)
  {
    boolean ftf = true;
    int ilat, ilon;
    int lilon=0, lilat=0;

    // Clear unoccupied cells in width table 
    
    for (int i = 0; i < yDots; i++)
      wtab[i] = -1;

    // Build transformation for declination 
    
    double s = Math.sin (-Math.toRadians (dec));
    double c = Math.cos (-Math.toRadians (dec));

    // Increment over a semicircle of illumination 

    for (double th = -(Math.PI / 2); th <= Math.PI / 2 + 0.001; th += Math.PI / TERMINC) 
    {
      // Transform the point through the declination rotation. 
      
      double x = -s * Math.sin (th);
      double y = Math.cos (th);
      double z = c * Math.sin (th);

      // Transform the resulting co-ordinate through the
      //   map projection to obtain screen co-ordinates. 

      double lon = (y == 0 && x == 0) ? 0d : Math.toDegrees (Math.atan2 (y, x));
      double lat = Math.toDegrees (Math.asin (z));

      ilat = (int)(yDots - (lat + 90) * (yDots / 180d));
      ilon = (int)(lon * (xDots / 360d));

      if (ftf) 
      {
        // First time.  Just save start co-ordinate. 

        lilon = ilon;
        lilat = ilat;
        ftf = false;

      } 
      else 
      {
        // Trace out the line and set the width table. 

        if (lilat == ilat) 
        {
          wtab[(yDots - 1) - ilat] = ilon == 0 ? 1 : ilon;
        } 
        else 
        {
          double m = ((double) (ilon - lilon)) / (ilat - lilat);
          
          for (int i = lilat; i != ilat; i += sgn(ilat - lilat)) 
          {
            int xt = (int)(lilon + Math.floor ((m * (i - lilat)) + 0.5));
            wtab[(yDots - 1) - i] = xt == 0 ? 1 : xt;
          }
        }
        lilon = ilon;
        lilat = ilat;
      }
    }

    // Now tweak the widths to generate full illumination for
    //   the correct pole. 

    if (dec < 0d) 
    {
      ilat = yDots - 1;
      lilat = -1;
    } 
    else 
    {
      ilat = 0;
      lilat = 1;
    }

    for (int i = ilat; i != yDots / 2; i += lilat) 
    {
      if (wtab[i] != -1) 
      {
        while (true) 
        {
          wtab[i] = xDots / 2;

          if (i == ilat)
            break;
          
          i -= lilat;
        }
        break;
      }
    }

  } // projectIlluminatedArea


  /**
   * Calcule JD (Julian Date) for a date in the Gregorian calendar
   * @version 1
   */
  /*final static long julianDateNumber (Date date) 
  {
    gregorian.setTime (date);
    int y = gregorian.get (Calendar.YEAR);
    int m = gregorian.get (Calendar.MONTH) + 1;
    int d = gregorian.get (Calendar.DAY_OF_MONTH);

    if (m > 2)
       m = m - 3;
    else {
       m = m + 9;
       y--;
    }
    int c = y / 100; // Compute century 
    y -= 100 * c;

    int jdn = d + (c * 146097) / 4 + (y * 1461) / 4 + (m * 153 + 2) / 5 + 1721119;
    System.out.println ("Gregorian="+year+"-"+month+"-"+day+" JDN="+jdn);
    return jdn;
  }*/

  /**
   * Calcule JD (Julian Date) for a date in the Gregorian calendar
   * @version 2
   *  
   * Scaliger's Julian period starts on 1 January 4713 BC (Julian calendar)
   * and lasts for 7980 years. AD 1998 is thus year 6711 in the Julian
   * period. After 7980 years the number starts from 1 again.
   *
   * Astronomers have used the Julian period to assign a unique number to
   * every day since 1 January 4713 BC. This is the so-called Julian Day
   * (JD). JD 0 designates the 24 hours from noon UTC on 1 January 4713 BC
   * to noon UTC on 2 January 4713 BC.
   *
   * Extracted from "FAQ about Calendars" 
   */
  final static long julianDateNumber (Date date) 
  {
    gregorian.setTime (date);
    int year = gregorian.get (Calendar.YEAR); 
    int month = gregorian.get (Calendar.MONTH) + 1;
    int day = gregorian.get (Calendar.DAY_OF_MONTH); 

    int a = (14-month)/12;
    int y = year+4800-a;
    int m = month + 12*a - 3;
    
    int jdn = day + (153*m+2)/5 + y*365 + y/4 - y/100 + y/400 - 32045;
    //System.out.println ("Gregorian="+year+"-"+month+"-"+day+" JDN="+jdn);
    return jdn;
  }

  /**
   * Convert internal GMT  date  and	time  to  astronomical
   * Julian  time  (i.e.   Julian  date  plus  day fraction,
   * expressed as a double).	
   */
  final static double julianDate (Date date) // struct tm *t;
  {
    gregorian.setTime (date);
    int sec = gregorian.get (Calendar.SECOND); // t->tm_sec
    int min = gregorian.get (Calendar.MINUTE); // t->tm_min
    int hour = gregorian.get (Calendar.HOUR_OF_DAY); // t->tm_hour

    double jd = (julianDateNumber (date) - 0.5) + (sec +  60d * (min + 60d * hour)) / 86400d;
    //System.out.println ("Gregorian="+date+" JD="+jd);
    return jd;
  }

  /**
   * Sometimes a modified Julian day number (MJD) is used which is
   * 2,400,000.5 less than the Julian day number. This brings the numbers
   * into a more manageable numeric range and makes the day numbers change
   * at midnight UTC rather than noon.
   *
   * Extracted from "FAQ about Calendars"
   */
  final static double modifiedJulianDay (double jd)  {
    return jd - 2400000.5;
  }

  /**
   * Convert Julian Date to Gregorian Date
   * Based on "FAQ about Calendars"
   */
  final static Date gregorianDate (int jdn)
  {     
     int a = jdn + 32045;
     int b = (4*(a+36524))/146097 - 1;
     int c = a - (b*146097)/4;

     int d = (4*(c+365))/1461 - 1;
     int e = c - (1461*d)/4;
     int m = (5*(e-1)+2)/153;

     int day   = e - (153*m+2)/5;
     int month = m + 3 - 12*(m/10);
     int year  = b*100 + d - 4800 + m/10;

     gregorian.set (year, month-1, day);
     return gregorian.getTime ();
  }

  /**
   * Solve the equation of Kepler.  
   * @param m
   * @param ecc
   */
  final static double kepler (double m, double ecc)
  {
    double e, delta;
    final double EPSILON = 1E-6;

    e = m = Math.toRadians (m);
    do {
       delta = e - ecc * Math.sin (e) - m;
       e -= delta / (1 - ecc * Math.cos (e));
    } while (Math.abs (delta) > EPSILON);

    return e;
  }

  /**
   * Calculate position of the Sun. JD is the Julian  date
   * of  the  instant for which the position is desired and
   * APPARENT should be nonzero if  the  apparent  position
   * (corrected  for  nutation  and aberration) is desired.
   *
   * The Sun's co-ordinates are returned  in  RA  and  DEC,
   * both  specified  in degrees (divide RA by 15 to obtain
   * hours). The radius vector to the Sun in  astronomical
   * units  is returned in RV and the Sun's longitude (true
   * or apparent, as desired) is  returned  as  degrees  in
   * SLONG.	
   *
   * @param jd        Julian date
   * @param apparent  Apparent position (corrected for nutation and aberration)
   * @return ra, dec, rv, slong in object SunPosition
   */
  final static SunPosition sunPosition (double jd, boolean apparent) 
  {
    // Time, in Julian centuries of 36525 ephemeris days,
    //   measured from the epoch 1900 January 0.5 ET. 

    double t = (jd - 2415020d) / 36525d;
    double t2 = t * t;
    double t3 = t2 * t;

    // Geometric mean longitude of the Sun, referred to the
    //   mean equinox of the date. 

    double l = fixAngle (279.69668 + 36000.76892 * t + 0.0003025 * t2);

    // Sun's mean anomaly. 

    double m = fixAngle (358.47583 + 35999.04975*t - 0.000150*t2 - 0.0000033*t3);

    // Eccentricity of the Earth's orbit.

    double e = 0.01675104 - 0.0000418 * t - 0.000000126 * t2;

    // Eccentric anomaly. 

    double ea = kepler (m, e);

    // True anomaly 

    double v = fixAngle (2 * Math.toDegrees (Math.atan (Math.sqrt ((1 + e) / (1 - e))  * Math.tan (ea / 2))));

    // Sun's true longitude. 

    double theta = l + v - m;

    // Obliquity of the ecliptic. 

    double eps = 23.452294 - 0.0130125 * t - 0.00000164 * t2 + 0.000000503 * t3;

    // Corrections for Sun's apparent longitude, if desired. 

    if (apparent) 
    {
       double omega = fixAngle(259.18 - 1934.142 * t);
       theta = theta - 0.00569 - 0.00479 * Math.sin (Math.toRadians (omega));
       eps += 0.00256 * Math.cos (Math.toRadians (omega));
    }

    // Return Sun's longitude and radius vector 

    sun.slong = theta;
    sun.rv = (1.0000002 * (1 - e * e)) / (1 + e * Math.cos (Math.toRadians (v)));

    // Determine solar co-ordinates. 

    sun.ra =  fixAngle (Math.toDegrees (Math.atan2 (Math.cos (Math.toRadians (eps)) * Math.sin (Math.toRadians (theta)), Math.cos (Math.toRadians (theta)))));
    sun.dec = Math.toDegrees (Math.asin (Math.sin (Math.toRadians(eps)) * Math.sin (Math.toRadians (theta))));

    return sun;
  }

  /**
   * Calculate Greenwich Mean Siderial Time for a given
   * instant expressed as a Julian date and fraction.	
   *
   * @param jd   Julian Date
   */
  final static double gmst (double jd)
  {
    // Time, in Julian centuries of 36525 ephemeris days,
    //   measured from the epoch 1900 January 0.5 ET. 

    double t = ((Math.floor (jd + 0.5) - 0.5) - 2415020d) / 36525d;

    double theta0 = 6.6460656 + 2400.051262 * t + 0.00002581 * t * t;

    t = (jd + 0.5) - (Math.floor (jd + 0.5));

    theta0 += (t * 24d) * 1.002737908;

    theta0 = (theta0 - 24d * (Math.floor (theta0 / 24d)));

    return theta0;
  }

} // SunClock

// End of file



luego creamos otra clase llamada SunPosition y dentro de esta clase ponemos el siguiente codigo:




package nochedia;
/**
 * @(#)SunPosition
 */

//package es.fcocascales.worldwatch;

/** 
 * Sun position return values 
 *
 * @author Francisco Cascales 
 * @version 4-XI-2000
 */
class SunPosition {
  double ra;    // Rect Ascension (Sun co-ordinate)
  double dec;   // Declination (Sun co-ordinate)
  double rv;    // Radius Vector to the Sun in astronomical units
  double slong; // Sun longitude
}

luego crearemos una nueva clase llamada WorldWatch que va a hacer el apple  y ponemos el siguiente codigo:


bueno luego de esto agregamos las siguientes imagenes uno es WorldWatch.gif


y la otra la llamaremos world.gif

bueno y eso es todo ahora solo lo ejecutan :)


Post a Comment

1 Comments

  1. Buen día. Estoy interesado en obtener la hora mundial de un país especifico y poder manipularlo en una aplicación java. Espero me pueda ayudar, gracias

    ReplyDelete