import java.awt.*; public class GraphPanel extends Panel { private boolean debug = false; // debuggin mode private Graph lab_graph; private ControlArrow up, down, left, right; private Image itablo; // double buffering for display private Graphics gtablo; private FontMetrics fm; private Font tablo_font; private Font state_font; private Substance substance; private State current_state; private Rectangle bounds, graph, buttons, tablo, state; private TextField state_field; private boolean loaded; public GraphPanel(Substance _substance) { substance = _substance; lab_graph = new Graph(substance); setLayout(null); loaded = false; tablo_font = new Font("Courier",Font.BOLD, 12); state_font = new Font("TimesRoman", Font.PLAIN, 10); state_field = new TextField(); state_field.setEditable(false); } public void setSubstance(Substance sub) { substance = sub; lab_graph.setSubstance(sub); repaint(); } public void reset() { lab_graph.reset(); repaint(); } public void reveal() { lab_graph.reveal(); repaint(); } public void clear() { lab_graph.clear(); } public void setSaveMode(boolean mode) { lab_graph.setSaveMode(mode); } //----------------------------------------------------------- public boolean mouseDown(Event evt, int x, int y) { if(up.inside(x,y)) lab_graph.moveUp(); else if(down.inside(x,y)) lab_graph.moveDown(); else if(left.inside(x,y)) lab_graph.moveLeft(); else if(right.inside(x,y)) lab_graph.moveRight(); else return super.mouseDown(evt, x, y); repaint(); return true; } public boolean keyDown(Event evt, int key) { repaint(); return super.keyDown(evt, key); } public boolean mouseEnter(Event evt, int x, int y) { if(evt.target == lab_graph) { lab_graph.requestFocus(); } else return super.mouseEnter(evt,x,y); return true; } //------------------------------------------------------------- public void paint(Graphics g) { if(!loaded) { initComponents(); loaded = true; } drawBG(g); drawUpArrow(g); drawDownArrow(g); drawLeftArrow(g); drawRightArrow(g); update(g); } public void update(Graphics g) { drawTablo(g); drawState(g); } //------------------------------------ private void drawBG(Graphics g) { g.setColor(Color.black); g.fill3DRect(bounds.x,bounds.y,bounds.width,bounds.height,true); } private void drawUpArrow(Graphics g) { up.draw(g); } private void drawDownArrow(Graphics g) { down.draw(g); } private void drawLeftArrow(Graphics g) { left.draw(g); } private void drawRightArrow(Graphics g) { right.draw(g); } public void drawTablo(Graphics g) { if(gtablo==null) { itablo = createImage(tablo.width, tablo.height); gtablo = itablo.getGraphics(); } String t = "Temperature: " + lab_graph.getTemperature() + " (C)"; String p = "Pressure : " + lab_graph.getPressure() + " (kbars)"; gtablo.setColor(Color.black); gtablo.fillRect(0,0,tablo.width,tablo.height); gtablo.setFont(tablo_font); int x = 5; int y1 = tablo.height/2-5; int y2 = tablo.height/2+10; gtablo.setColor(Color.green); gtablo.drawString(t, x, y1); gtablo.drawString(p, x, y2); g.drawImage(itablo, tablo.x, tablo.y, this); } public void drawState(Graphics g) { if(current_state == lab_graph.getState()) return; state_field.setText(lab_graph.getState().getName()); current_state = lab_graph.getState(); } //---------------------------------------- private void initComponents() { initBounds(); initGraph(); initButtons(); initTablo(); initState(); } private void initBounds() { bounds = new Rectangle(0, 0, size().width, size().height); } private void initGraph() { graph = new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height-50); add(lab_graph); lab_graph.reshape(graph.x, graph.y, graph.width, graph.height); } private void initButtons() { int offtop = 5; int offbottom = 5; int offleft = 5; int x = bounds.x + offleft; int y = graph.y + graph.height + offtop; int height = bounds.height - graph.height - offbottom - offtop; int width = 4*height; buttons = new Rectangle(x, y, width, height); up = new ControlArrow(ControlArrow.UP, buttons.x, buttons.y, buttons.width/4, buttons.height); down = new ControlArrow(ControlArrow.DOWN, up.x+up.width,buttons.y,buttons.width/4,buttons.height); left = new ControlArrow(ControlArrow.LEFT, down.x+down.width,buttons.y,buttons.width/4,buttons.height); right = new ControlArrow(ControlArrow.RIGHT, left.x+left.width,buttons.y,buttons.width/4,buttons.height); } private void initTablo() { int x = buttons.x + buttons.width + 10; int y = buttons.y; int w = 180; int h = buttons.height; tablo = new Rectangle(x, y, w, h); } private void initState() { int x = tablo.x + tablo.width + 10; int y = tablo.y; int w = (bounds.x+bounds.width) - (tablo.x+tablo.width) - 2*10; int h = tablo.height; state = new Rectangle(x, y, w, h); int off = 5; state_field.reshape(x, y+off, w, h-2*off); add(state_field); } private void dbi(String s) { if(debug) System.out.println(s); } }