using System; using System.Drawing; using System.Windows.Forms; public class WaterTank : Panel { private WaterLevelSensor _highLevelSensor; private WaterLevelSensor _lowLevelSensor; private Pen _whitePen; private Pen _bluePen; private int _penWidth; private int _currentWaterLevel; private int _lastWaterLevel; private int _tankCapacity; private Point _bottomLeft; private Point _bottomRight; private Pump _itsPump; private Graphics _graphics; public int WaterLevel { get {return _currentWaterLevel; } } public int FillRate { get {return _itsPump.PumpingCapacity; } } public int HighSetPoint { get { return _highLevelSensor.SetPoint; } set { _highLevelSensor.SetPoint = value; } } public int LowSetPoint { get { return _lowLevelSensor.SetPoint; } set { _lowLevelSensor.SetPoint = value; } } public WaterTank(int x, int y, int width, int height, int tankCapacity, int pumpCapacity){ this.InitializeComponents(x, y, width, height, tankCapacity, pumpCapacity); } public WaterTank():this(100, 100, 100, 500, 10000, 1000){ } private void InitializeComponents(int x, int y, int width, int height, int tankCapacity, int pumpCapacity){ this.Bounds = new Rectangle(x, y, width, height); this.BackColor = Color.White; this.BorderStyle = BorderStyle.Fixed3D; _graphics = this.CreateGraphics(); _bottomLeft = new Point(0, height); _bottomRight = new Point(width, height); _tankCapacity = tankCapacity; _currentWaterLevel = 0; _itsPump = new Pump(this, pumpCapacity); _penWidth = this.Height/(_tankCapacity/_itsPump.PumpingCapacity); if(_penWidth < 1) _penWidth = 1; _whitePen = new Pen(Color.White, _penWidth); _bluePen = new Pen(Color.Blue, _penWidth); _highLevelSensor = new WaterLevelSensor(tankCapacity - pumpCapacity, 0); _highLevelSensor.SensorMode = WaterLevelSensor.Mode.HighLevelIndicator; _highLevelSensor.Fill += new WaterLevelEventHandler(_itsPump.FillTankEventHandler); _highLevelSensor.Full += new WaterLevelEventHandler(_itsPump.FullTankEventHandler); _lowLevelSensor = new WaterLevelSensor(pumpCapacity, 0); _lowLevelSensor.SensorMode = WaterLevelSensor.Mode.LowLevelIndicator; _lowLevelSensor.Drain += new WaterLevelEventHandler(_itsPump.DrainTankEventHandler); _lowLevelSensor.Empty += new WaterLevelEventHandler(_itsPump.EmptyTankEventHandler); } public void ChangeWaterLevel(int amount){ _lowLevelSensor.WaterLevelChange(amount); _highLevelSensor.WaterLevelChange(amount); _currentWaterLevel += amount; _lastWaterLevel = _currentWaterLevel; this.ChangeVisualLevel(amount); } private void ChangeVisualLevel(int amount){ if(amount > 0){ _graphics.DrawLine(_bluePen, _bottomLeft, _bottomRight); _bottomLeft.Y -= _penWidth; _bottomRight.Y -= _penWidth; }else{ _graphics.DrawLine(_whitePen, _bottomLeft, _bottomRight); _bottomLeft.Y += _penWidth; _bottomRight.Y += _penWidth; Delay(30000000); } } // end ChangeVisualLevel method private void Delay(long ticks){ for(long i = 0; i