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

public class Speedometer 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 int safe;
public int speed;
public int scale;
    
public Speedometer(Ship spaceship_, Space canvas)
    {
	spaceship = spaceship_;
	width = canvas.width;
	height = 8;
	height_2 = height-2;
	height_1 = height-1;
	setSize(width, height);
	scale = 20;
	safe = 3*scale;
    }
    
public void update(Graphics g)
    {
	paint(g);
    }
    
public void paint(Graphics g)
    {
	if((offGraphics == null))
	    {
		offImage = createImage(width, height);
		offGraphics = offImage.getGraphics();
	    }
	speed = (int)(spaceship.Velocity.magnitude()*scale);
	Color color;
	if(spaceship.Velocity.Y <= 0)
	    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, speed, height_2);
	offGraphics.setColor(Color.blue);
	offGraphics.drawLine(0, height_1, safe, height_1);
	offGraphics.setColor(Color.red);
	offGraphics.drawLine(safe, height_1, width, height_1);
	g.drawImage(offImage, 0, 0, this);
    }
    
public void dump()
    {
	System.out.println((int)(spaceship.Velocity.magnitude()*10));
    }
}

