import java.awt.*; public final class MgSiO3 implements Substance { private int id; private final String name = "MgSiO3"; private final String formula = "MgSiO3"; private State[] state; private Color[] color; private final Point xrange = new Point(500, 2600); // temperature range private final Point yrange = new Point(0, 260); // pressure range private int[] xpoints = { 500, 1000, 1500, 2000, 2500 }; // points that appear on the graph private int[] ypoints = { 0, 50, 100, 150, 200, 250 }; public MgSiO3() { Polygon temp; color = new Color[15]; int r, g, b; for(int i=0; i<15; i++) { r = (int)(Math.random()*250); g = (int)(Math.random()*250); b = (int)(Math.random()*250); color[i] = new Color(r, g, b); } state = new State[15]; int state1XPoints[] = { 500, 500, 700, 750, 550, 500 }; int state1YPoints[] = { 0, 65, 70, 50, 0, 0 }; temp = new Polygon(state1XPoints, state1YPoints, state1XPoints.length); state[1] = new State("Low CEn", color[1], temp); int state2XPoints[] = { 550, 750, 700, 2200, 1700, 1600, 1500, 1000, 550 }; int state2YPoints[] = { 0, 50, 70, 115, 20, 10, 15, 0, 0 }; temp = new Polygon(state2XPoints, state2YPoints, state2XPoints.length); state[2] = new State("Orthoenstatite", color[2], temp); int state3XPoints[] = { 1000, 1500, 1600, 1550, 1000 }; int state3YPoints[] = { 0, 15, 10, 0, 0 }; temp = new Polygon(state3XPoints, state3YPoints, state3XPoints.length); state[3] = new State("Protoenstatite", color[3], temp); int state4XPoints[] = { 1550, 1600, 1700, 1650, 1550 }; int state4YPoints[] = { 0, 10, 20, 0, 0 }; temp = new Polygon(state4XPoints, state4YPoints, state4XPoints.length); state[4] = new State("High Clinoenstatite", color[4], temp); int state5XPoints[] = { 1650, 1700, 2200, 2350, 2600, 2600, 1650 }; int state5YPoints[] = { 0, 20, 115, 160, 220, 0, 0 }; temp = new Polygon(state5XPoints, state5YPoints, state5XPoints.length); state[5] = new State("Liquid", color[5], temp); int state6XPoints[] = { 500, 500, 600, 1600, 2350, 2200, 700, 500 }; int state6YPoints[] = { 65, 140, 145, 170, 160, 115, 70, 65 }; temp = new Polygon(state6XPoints, state6YPoints, state6XPoints.length); state[6] = new State("High-P Clinoenstatite", color[6], temp); int state7XPoints[] = { 600, 1100, 1600, 1600, 600 }; int state7YPoints[] = { 145, 180, 175, 170, 145 }; temp = new Polygon(state7XPoints, state7YPoints, state7XPoints.length); state[7] = new State("Bt & St", color[7], temp); int state8XPoints[] = { 1600, 1600, 2050, 2600, 2350, 1600 }; int state8YPoints[] = { 170, 175, 215, 220, 160, 170 }; temp = new Polygon(state8XPoints, state8YPoints, state8XPoints.length); state[8] = new State("Majorite", color[8], temp); int state9XPoints[] = { 500, 500, 1100, 600, 500 }; int state9YPoints[] = { 140, 255, 180, 145, 140 }; temp = new Polygon(state9XPoints, state9YPoints, state9XPoints.length); state[9] = new State("Spinel & Stishovite", color[9], temp); int state10XPoints[] = { 1100, 500, 2050, 1600, 1100 }; int state10YPoints[] = { 180, 255, 215, 175, 180 }; temp = new Polygon(state10XPoints, state10YPoints, state10XPoints.length); state[10] = new State("Ilmenite", color[10], temp); int state11XPoints[] = { 500, 500, 2600, 2600, 2050, 500 }; int state11YPoints[] = { 255, 260, 260, 220, 215, 255 }; temp = new Polygon(state11XPoints, state11YPoints, state11XPoints.length); state[11] = new State("Perovskite", color[11], temp); } public int getID() { return id; } public void setID(int _id) { id = _id; } public String getName() { return name; } public String getFormula() { return formula; } public Point getXRange() { return xrange; } public Point getYRange() { return yrange; } public int[] getXPoints() { return xpoints; } public int[] getYPoints() { return ypoints; } public State getState(int temperature, int pressure) { State state = findState(temperature, pressure); if(state == null) state = approximate(temperature, pressure); return state; } public String getDescription() { String s = "not implemented yet"; return s; } /* checks if given temp and pres is inside any state if not, it returns null */ private State findState(int temperature, int pressure) { for(int i=1; i<=11; i++) { if(state[i].inside(temperature, pressure)) return state[i]; } return null; } /* if given temperature and pressure are not inside any state, which could happen on intersection of states, it approximates the state by looking in different directions. xoff and yoff are the increments by which it looks, and MAXCOUNT is the number of times it increments temp and pres to look in one direction */ private State approximate(int temperature, int pressure) { System.out.println("Approximating . . ."); int temp = temperature; int pres = pressure; // reseted each time different side is looked into int MAXCOUNT = 10; // max number of times to look in one side int xoff, yoff; int times = 0; // how many times while loop executed while(true) { // keeps looking until state is found times++; // increment number of times while loop executed xoff = times; // begins at 1; yoff = times; // keeps incrementing every time according to 'times' temp = temperature; pres = pressure; for(int i=0; i