
import java.awt.*;
import java.awt.image.*;

public class FuelGauge extends Canvas
{
private Image offImage;       // buffer for double buffering
private Graphics offGraphics; // buffer for double buffering
public Ship spaceship;
public int width;
public int height;
public int height_2;
public int height_1;
public double fuel;
public double delta;
public double safe;
    
public FuelGauge(Ship spaceship_, Space canvas)
    {
	spaceship = spaceship_;
	width = canvas.width;
	height = 8;
	height_2 = height-2;
	height_1 = height-1;
	setSize(width, height);
	fuel = width;
	delta = width/100;
	safe = width/10;
    }

public void reset()
    {
	fuel = width;
    }
    
public void decrement()
    {
	fuel-=delta;
	if(fuel < 0)
	    fuel = 0;
    }
    
public void update(Graphics g)
    {
	paint(g);
    }
    
public void paint(Graphics g)
    {
	if((offGraphics == null))
	    {
		offImage = createImage(width, height);
		offGraphics = offImage.getGraphics();
	    }
	Color color;
	if(fuel > safe)
	    color = Color.blue;
	else
	    color = Color.red;
	offGraphics.setColor(Color.black);
	offGraphics.fillRect(0, 0, width, height_2);
	offGraphics.setColor(color);
	offGraphics.fillRect(0, 0, (int)fuel, height_2);
	if(fuel > safe)
	    {
		offGraphics.setColor(Color.red);
		offGraphics.fillRect(0, 0, (int)safe, height_2);
	    }
	offGraphics.setColor(Color.red);
	offGraphics.drawLine(0, height_1, (int)safe, height_1);
	offGraphics.setColor(Color.blue);
	offGraphics.drawLine((int)safe, height_1, width, height_1);
	g.drawImage(offImage, 0, 0, this);
    }
    
public void dump()
    {
	System.out.println((int)(spaceship.Velocity.magnitude()*10));
    }
}

