// file: Tank.java // author: David Fleming // created 7/1/96 import java.awt.*; /* This class is a data abstraction representing a tank holding fluid. It is capable of adding and removing liquid and can be initialized to be of a certain capacity. It also returns messages stating when the tank is either empty or full. */ public class Tank extends Canvas { private float temp; private static final int tankHeight = 100; private static final int tankWidth = 225; private static final float emptyTank = 0.0f; private static final float fullTank = 120.0f; private static final Color borderColor = Color.black; private static final Color fluidColor = Color.green; public Tank( float initial ) { temp = initial; resize( tankWidth+5, tankHeight+5 ); } public float howFull() { return temp; } public float tankCapacity() { float capacity = fullTank; return capacity; } public void addFluid( float amount ) { temp = temp + amount; Graphics g = this.getGraphics(); paint(g); } public void removeFluid( float amount ) { temp = temp - amount; Graphics g = this.getGraphics(); paint(g); } public void paint( Graphics g ) { g.setColor( borderColor ); g.drawRect( 0, 0, tankWidth, tankHeight ); g.setColor( fluidColor ); int fluidHeight=(int)(((temp - emptyTank)/(fullTank-emptyTank))*tankHeight); if ( fluidHeight > tankHeight ) { fluidHeight = tankHeight; } else { g.fillRect( 0, tankHeight-fluidHeight, tankWidth, fluidHeight ); }; } }