import com.cult3d.*;
import com.cult3d.world.*;

import java.util.Date;
import java.awt.Graphics;
import java.awt.Color;

public class Clock implements Cult3DScript, Runnable
{
	// Name of the texture to which the time will be written
	private static final String TEXTURE_NAME = "friday";
	
	// A variable to tell the Java code when the load thread has ended
	private volatile boolean loaded = false;
	
	private Texture texture;
	private TextureImage image;
	private Graphics graphics;
	
	
	public Clock()
	{
		new Thread(this).start();
	}
	
	// Load the texture in a seperate thread to avoid possible freeze.
	public void run()
	{
		// Get the texture representation
		texture = new Texture(TEXTURE_NAME);

		// Get an image where to apply the drawn date on
		image = (TextureImage)Cult.createImage(texture.getWidth(), texture.getHeight());

		// Get an offscreen graphics object to write on
		graphics = image.getGraphics();

		// Tell the Java code that everything is loaded
		loaded = true;
	}
	
	// Method called from the Designer
	public void changeTime(String s)
	{
		// Check to see if the thread has completed it's loading
		if (!loaded)
		{
			System.out.println("All textures are not loaded yet");
			return;
		}

		// Deprecated, but works, Remember that you should only use Java 1.1 stuff, if you want a broad public to be able to view it
		Date date = new Date();
		
		// Set the white as the background
		graphics.setColor(new Color(255, 255, 255));
		graphics.fillRect(0, 0, texture.getWidth(), texture.getHeight());
		
		// Draw the date, In this case we draw for example Mon March 18 12:00
		graphics.setColor(new Color(0, 0, 0));
		graphics.drawString(date.toString(), 10, 10);

		// Set the drawn picture to the texture and update it
		texture.setTexture(image);
	}
		
	// Method called when Cult3D viewer is about to die.
	public void cult3dDestroy()
	{
	}
}

