//package tideCalendar; import java.io.*; import javax.swing.ImageIcon; import java.awt.Image; public class Moon { public static final byte UP = 0; public static final byte DOWN = 1; private MoonStructure moonLine = new MoonStructure(); private Image[][] moonImages = new Image[2][]; public Moon() { ImageIcon myIcon; moonImages[UP] = new Image[11]; moonImages[DOWN] = new Image[13]; myIcon = Shared.loadImage("moonPics/moon0.gif", 1500, this); moonImages[UP][0] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon10u.gif", 1500, this); moonImages[UP][1] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon25u.gif", 1500, this); moonImages[UP][2] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon35u.gif", 1500, this); moonImages[UP][3] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon50u.gif", 1500, this); moonImages[UP][4] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon55u.gif", 1500, this); moonImages[UP][5] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon65u.gif", 1500, this); moonImages[UP][6] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon70u.gif", 1500, this); moonImages[UP][7] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon80u.gif", 1500, this); moonImages[UP][8] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon90u.gif", 1500, this); moonImages[UP][9] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon100.gif", 1500, this); moonImages[UP][10] = myIcon.getImage(); //new moon in up direction is same as down direction moonImages[DOWN][0] = moonImages[UP][0]; myIcon = Shared.loadImage("moonPics/moon5d.gif", 1500, this); moonImages[DOWN][1] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon15d.gif", 1500, this); moonImages[DOWN][2] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon30d.gif", 1500, this); moonImages[DOWN][3] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon40d.gif", 1500, this); moonImages[DOWN][4] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon45d.gif", 1500, this); moonImages[DOWN][5] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon50d.gif", 1500, this); moonImages[DOWN][6] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon55d.gif", 1500, this); moonImages[DOWN][7] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon65d.gif", 1500, this); moonImages[DOWN][8] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon70d.gif", 1500, this); moonImages[DOWN][9] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon80d.gif", 1500, this); moonImages[DOWN][10] = myIcon.getImage(); myIcon = Shared.loadImage("moonPics/moon90d.gif", 1500, this); moonImages[DOWN][11] = myIcon.getImage(); //full moon in up direction is the same as down direction moonImages[DOWN][12] = moonImages[UP][10]; } public void moonReader(String file) { try{ byte moonProgression; BufferedReader input = new BufferedReader(new InputStreamReader(new BufferedInputStream((this.getClass()).getResourceAsStream(file)))); int count = 0; String storedInput; String[] splitInput; while (input.readLine() != null) {count++;} moonLine = new MoonStructure(count); input.close(); input = new BufferedReader(new InputStreamReader(new BufferedInputStream((this.getClass()).getResourceAsStream(file)))); splitInput = (input.readLine()).split("!"); //[0] is offset, [1] is last height, [2] is new Height moonLine.offset = (Float.parseFloat(splitInput[0])); moonLine.startingHeight[0] = Byte.parseByte(splitInput[1]); moonLine.endingHeight[0] = Byte.parseByte(splitInput[2]); if (moonLine.endingHeight[0] == 1) moonProgression = 1; else if (moonLine.endingHeight[0] == 3) moonProgression = -1; else //2 { if (moonLine.startingHeight[0] == 1) moonProgression = 1; else //3 moonProgression = -1; } count = 1; while((storedInput=input.readLine()) != null) { moonLine.xDistance[count] = Float.parseFloat(storedInput); moonLine.startingHeight[count] = moonLine.endingHeight[count-1]; moonLine.endingHeight[count] = (byte)(moonLine.startingHeight[count] + moonProgression); if (moonLine.endingHeight[count] != 2) moonProgression *= -1; count++; } }catch (Exception e) {System.out.println(e);} } public ImageIcon getMoon(float offset) { if (offset < 0) return null; double xDistance = -moonLine.offset, startingHeight = 0.0; double endingHeight = 0.0, slope = 0.0; int percent; byte dir; int i = 0; do { xDistance += moonLine.xDistance[i++]; }while (xDistance < offset); xDistance -= moonLine.xDistance[--i]; double x = offset - xDistance; xDistance = moonLine.xDistance[i++]; startingHeight = 100*moonLine.startingHeight[i]; endingHeight = 100*moonLine.endingHeight[i]; slope = (endingHeight - startingHeight) / xDistance; percent = (int)((slope * x + startingHeight - 100)/2); dir = (slope > 0)?UP:DOWN; if (dir == UP) { String str = Integer.toString(percent) + "% up"; return new ImageIcon(upMoon(percent), str); } else //direction = down { String str = Integer.toString(percent) + "% down"; return new ImageIcon(downMoon(percent), str); } } /** * returns a byte representing the type of moon that occurs on that day * -1: New Moon * 0: Nothing Special * 1: Full Moon * @param day the day of the year that the moon is being requested for, in * the form 0 ≤ day ≤ 364 (or 365 in a leap year) * @return the type of moon in the form of a byte */ public byte moonType (int day) { final byte NOTHING = 0; final byte FULL = 1; final byte NEW = -1; byte type = NOTHING; int offset = day*24; int i = 0; double xDistance = -moonLine.offset; do { xDistance += moonLine.xDistance[i++]; }while (xDistance < offset); xDistance = (int)(xDistance/24); if (day == xDistance) type = (byte)(moonLine.endingHeight[i]-2); return type; } private Image upMoon(int percent) { int i; /* Possible moon Pictures: *Location - Percent Filled * 0-0 - 0% * 0-1 - 10% * 0-2 - 25% * 0-3 - 35% * 0-4 - 50% * 0-5 - 55% * 0-6 - 65% * 0-7 - 70% * 0-8 - 80% * 0-9 - 90% * 0-10 -100% */ if (percent < 50) // between 0 & 50 { if (percent < 25)// between 0 & 25 { if (percent < 10) //between 0 & 10 i = (percent < 10-percent)?0:1; else //between 10 & 25 i = (percent-10 < 25-percent)?1:2; } else //between 25 & 50 { if (percent < 35) //between 25 & 35 i = (percent-25 < 35-percent)?2:3; else //between 35 & 50 i = (percent-35 < 50-percent)?3:4; } } else // between 50 & 100 { if (percent < 80) // between 50 & 80 { if (percent < 65) // between 50 & 65 { if (percent < 55) //between 50 & 55 i = (percent - 50 < 55 - percent)?4:5; else //between 55 & 65 i = (percent - 55 < 65 - percent)?5:6; } else //between 65 & 80 { if (percent < 70) // between 65 & 70 i = (percent - 65 < 70 - percent)?6:7; else //between 70 & 80 i = (percent - 65 < 70 - percent)?7:8; } } else //between 80 & 100 { if (percent < 90) // between 80 & 90 i = (percent - 80 < 90 - percent)?8:9; else //between 90 & 100 i = (percent - 90 < 100 - percent)?9:10; } } return moonImages[UP][i]; } private Image downMoon(int percent) { /* Possible moon Pictures: *Location - Percent Filled * 1-0 - 0% * 1-1 - 5% down * 1-2 - 15% down * 1-3 - 30% down * 1-4 - 40% down * 1-5 - 45% down * 1-6 - 50% down * 1-7 - 55% down * 1-8 - 65% down * 1-9 - 70% down * 1-10 - 80% down * 1-11 - 90% down * 1-12 -100% */ int i; if (percent < 50) //between 0 & 50 { if (percent < 30) //between 0 & 30 { if (percent < 15) { if (percent < 5) //between 0 & 5 i = (percent < 5-percent)?0:1; else //between 5 & 15 i = (percent-5 < 15-percent)?1:2; } else //between 15 & 30 i = (percent-15 < 30-percent)?2:3; } else //between 30 & 50 { if (percent < 40) //between 30 & 40 i = (percent-30 < 40-percent)?3:4; else if (percent < 45) //between 40 & 45 i = (percent-40 < 45-percent)?4:5; else //between 45 & 50 i = (percent-45 < 50-percent)?5:6; } } else // between 50 & 100 { if (percent < 70) // between 50 & 70 { if (percent < 55) // between 50 & 55 i = (percent-50 < 55-percent)?6:7; else if (percent < 65) //between 55 & 65 i = (percent-55 < 65-percent)?7:8; else //between 65 & 70 i = (percent-65 < 70-percent)?8:9; } else // between 70 & 100 { if (percent < 80) //between 70 & 80 i = (percent-70 < 80-percent)?9:10; else if (percent < 90)//between 80 & 90 i = (percent-80 < 90-percent)?10:11; else //between 90 & 100 i = (percent-90 < 100-percent)?11:12; } } return moonImages[DOWN][i]; } private class MoonStructure { private byte[] startingHeight; private byte[] endingHeight; private float[] xDistance; private float offset = 0; private MoonStructure() { this(0); } private MoonStructure(int size) { xDistance = new float[size]; startingHeight = new byte[size]; endingHeight = new byte[size]; offset = 0; } } }