/**************************************************** * Joseph Snow, Wayne Witzel and Wes Wariner * ASPIRE, University of Utah * March 2001 * * NSkySunEarthView class */ import java.awt.*; import java.awt.image.*; //import com.sun.java.swing.*; import javax.swing.*; public class NSkySunEarthView extends JComponent { private Image earthImage; private NSkySunEarthSystem sunEarthSystem; private Rectangle sunbounds; private Rectangle earthbounds; double xdistance; double ydistance; int xorigin; int yorigin; double alpha = Math.PI / 180.0; /* (2/360) */ int mar21 = 366-80; /* 0=>360, March 21->March 20*, with 0 degrees => East the 80 is the approximate number of days from Jan 1-> Mar 21 */ public NSkySunEarthView(NSkySunEarthSystem sunEarthSystem, Image earthGlobeImage) { int sunRadius = 15; earthbounds = new Rectangle(); sunbounds = new Rectangle(getBounds().width/2 - sunRadius, getBounds().height/2 - sunRadius, sunRadius*2, sunRadius*2); this.sunEarthSystem = sunEarthSystem; this.earthImage = earthGlobeImage; xdistance = getBounds().width * 0.325; ydistance = getBounds().height * 0.325; xorigin = (int)(getBounds().width/2); yorigin = (int)(getBounds().height/2); } public void setBounds(int x, int y, int width, int height) { super.setBounds(x, y, width, height); // set the sun and earth bounds as well sunbounds = new Rectangle(width/2 - sunbounds.width/2, height/2 - sunbounds.height/2, sunbounds.width, sunbounds.height); xdistance = getBounds().width * 0.325; ydistance = getBounds().height * 0.325; xorigin = (int)(getBounds().width/2); yorigin = (int)(getBounds().height/2); } public void refresh() { // This is done here instead of within paintComponent directly // for a good reason. If it is done in paintComponent and then // drawn, it tends to cause it to repaint over and over again // and slow everything down. /* nothing to do */ } public void paintComponent(Graphics g) { if (earthImage == null) { return; } g.setColor(Color.black); g.fillRect(0, 0, this.getWidth(), this.getHeight()); try { /* draw the sun oval */ g.setColor(Color.white); g.fillOval(sunbounds.x, sunbounds.y, sunbounds.width, sunbounds.height); //g.setColor(Color.blue); //g.drawString("SUN", sunbounds.x, sunbounds.y+ (int)(sunbounds.height*.75)); /* draw the earthglobe */ int earth_angle = (int)((sunEarthSystem.getCalendar().get(java.util.Calendar.DAY_OF_YEAR) + mar21) * 360.0 / 365.0)%360; earthbounds.x = (int)(Math.cos(alpha*earth_angle)*xdistance + xorigin - earthImage.getWidth(this)/2.0); earthbounds.y = -(int)(Math.sin(alpha*earth_angle)*ydistance - yorigin + earthImage.getHeight(this)/2.0); g.drawImage(earthImage, earthbounds.x, earthbounds.y, this); } catch (Exception e) { } } };