import java.awt.*; public class Graph extends Canvas { private boolean debug = false; // debugging mode private Substance substance; // current substance private int id; // id of the substance private Rectangle bounds; // defines the whole graph panel private Rectangle gpanel; // defines ONLY the graph area private boolean loaded; private boolean mode; //true for save mode and false otherwise private Font title_font; //font for graph title private Font TPfont; // font for axis labels private Font H1font; // font for heading private Font scale_font; // font for scale private FontMetrics fm; private int between = 2; // distance between two current rectangles private Point origin; // point where two axis intersect private int offtop = 35; private int offbottom = 40; private int offleft = 50; private int offright = 20; private Increment current; // current state of path on the graph private State current_state; private Path[] path; // path of increments private Image image; private Graphics offg; public Graph(Substance _substance) { substance = _substance; id = substance.getID(); current_state = substance.getState(substance.getXRange().x,substance.getYRange().x); loaded = false; path = new Path[3]; mode = false; // default not to save path title_font = new Font("TimesRoman", Font.PLAIN, 18); TPfont = new Font("TimesRoman", Font.ITALIC+Font.BOLD, 14); scale_font = new Font("TimesRoman", Font.PLAIN, 12); } public void setSubstance(Substance _substance) { substance = _substance; id = substance.getID(); if(!mode) { resetEverything(); } else { current = path[id].last(); setState(); resetScreen(); } } private void resetEverything() { resetCurrent(); setState(); current.setColor(current_state.getColor()); path[id] = new Path(current); resetScreen(); } private void setState() { State new_state = substance.getState(calculateTP().x, calculateTP().y); if(current_state != new_state) { current_state = new_state; } } public State getState() { return current_state; } public int getTemperature() { return calculateTP().x; } public int getPressure() { return calculateTP().y; } public void reset() { resetEverything(); } public void clear() { path[id].clear(); resetScreen(); } public void reveal() { int width = gpanel.width/(current.width+between); int height = gpanel.height/(current.height+between); boolean right = true; resetEverything(); for(int i=0; i gpanel.y + current.height + between) { eraseCurrent(); int x = current.x; int y = current.y - current.height - between; int side = current.width; current = new Increment(Color.black, x, y, side, side); move(); } } public void moveDown() { if(current.y+current.height < gpanel.y + gpanel.height) { eraseCurrent(); int x = current.x; int y = current.y + current.height + between;; int side = current.width; current = new Increment(Color.black, x, y, side, side); move(); } } public void moveLeft() { if(current.x > gpanel.x) { eraseCurrent(); int x = current.x - current.width - between;; int y = current.y; int side = current.width; current = new Increment(Color.black, x, y, side, side); move(); } } public void moveRight() { if(current.x + current.width + between < gpanel.x + gpanel.width) { eraseCurrent(); int x = current.x + current.width + between;; int y = current.y; int side = current.width; current = new Increment(Color.black, x, y, side, side); move(); } } //---------------------------------------------------------------------- private void drawCurrent(Graphics g) { g.setColor(Color.white); // draw current using white color g.fillRect(current.x,current.y,current.width,current.height); } private void eraseCurrent() { Graphics g = getGraphics(); current.draw(g); // draw current using its own color g.dispose(); } private void resetCurrent() { current = new Increment(current_state.getColor(),origin.x,origin.y-4,4,4); } //-------------------------------------------------------- private Point calculateTP() { if(current==null) { return new Point(substance.getXRange().x,substance.getYRange().x); } int x = current.x - gpanel.x; int y = gpanel.height - (current.y - gpanel.y); int tlength = substance.getXRange().y - substance.getXRange().x; int plength = substance.getYRange().y - substance.getYRange().x; int temperature = x*tlength/gpanel.width + substance.getXRange().x; int pressure = y*plength/gpanel.height + substance.getYRange().x; return new Point(temperature, pressure); } //------------------------------------------------------- public void paint(Graphics g) { if(!loaded) { setLayout(); loaded = true; } drawGraphBG(g); drawSubstanceName(g); drawAxis(g); drawScale(g); drawPressureLabel(g); drawTemperatureLabel(g); path[id].draw(g); g.setColor(Color.gray); g.drawLine(bounds.x, bounds.y+bounds.height-1, bounds.x+bounds.width,bounds.y+bounds.height-1); update(g); } public void update(Graphics g) { drawCurrent(g); } //------------------- Paint background --------------------- private void drawGraphBG(Graphics g) { g.setColor(Color.black); g.fill3DRect(bounds.x,bounds.y,bounds.width,bounds.height, true); } private void drawAxis(Graphics g) { //Engraved.draw3DLine(g, origin.x-2, origin.y, gpanel.x-2, gpanel.y, true); //Engraved.draw3DLine(g, origin.x, origin.y+1, gpanel.x+gpanel.width, // gpanel.y+gpanel.height+1, false); g.setColor(Color.green); g.drawLine(origin.x-2, origin.y, gpanel.x-2, gpanel.y); g.drawLine(origin.x, origin.y+1, gpanel.x+gpanel.width, gpanel.y+gpanel.height+1); } private void drawScale(Graphics g) { int x, y; g.setColor(Color.white); g.setFont(scale_font); fm = g.getFontMetrics(scale_font); int[] xpoints = substance.getXPoints(); int[] ypoints = substance.getYPoints(); int width = substance.getXRange().y-substance.getXRange().x; int height = substance.getYRange().y-substance.getYRange().x; int off = 4; // horizontal y = origin.y+2; for(int i=0; i