//package com.neilmoomey.Neatline;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.io.*;
import java.util.*;
import com.neilmoomey.util.Type;

/** Neatline.java
 * @author Neil Moomey, www.neilmoomey.com
 * @version 0.1
 **/

public class Neatline extends JFrame {

	int width=400;
	int height=9509;

	public Neatline() {
		super ("Neatline");
		System.out.println("Constructor");
        setSize(width, height);
	}

	public void paint(Graphics g) {

		double startDeg, endDeg, startMin, endMin, x,y, delta, lineWidth, range, min;
		startMin = 12;
		endMin = 20;
		min = startMin;
		range = endMin - startMin; 


		//setBackground(Color.white);
		// Create a BufferedImage to draw in
		BufferedImage bi = new BufferedImage(width , height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2D = bi.createGraphics();
		//Graphics2D g2D = (Graphics2D)g;  Use this for displaying only, not for saving image.
		g2D.setPaint(Color.white);
		g2D.fillRect(0,0,width,height);
		g2D.setColor(Color.black);


		//****** draw 1/10 minute lines ******
		x = 100;
		lineWidth=10;
		Font font = new Font("Arial", Font.BOLD, 60);
		g2D.setFont(font);

		// Draw vertical lines
		g2D.drawLine(Type.toInt(x-lineWidth),0, Type.toInt(x-lineWidth), Type.toInt(height));
		g2D.drawLine(Type.toInt(x),0, Type.toInt(x), Type.toInt(height));
		g2D.drawLine(Type.toInt(x+lineWidth),0, Type.toInt(x+lineWidth), Type.toInt(height));

		delta = height/((endMin - startMin)*2);
		
				// Draw minute vertical lines
				g2D.drawLine(Type.toInt(x-1+lineWidth/2),Type.toInt(height), Type.toInt(x-1+lineWidth/2), Type.toInt(height-(delta*2)));
				g2D.drawLine(Type.toInt(x+lineWidth/2),Type.toInt(height), Type.toInt(x+lineWidth/2), Type.toInt(height-(delta*2)));
				g2D.drawLine(Type.toInt(x+1+lineWidth/2),Type.toInt(height), Type.toInt(x+1+lineWidth/2), Type.toInt(height-(delta*2)));
				
				
		for (y=height; y>0; y-=delta) {
			// Draw half minute marks
			g2D.drawLine(Type.toInt(x),Type.toInt(height-(y)), Type.toInt(x+lineWidth), Type.toInt(height-(y)));
			if((min/1)==new Double(min/1).intValue()) {
				// Draw minute numbers every 5 minutes
				g2D.drawString(Type.toString(Type.toInt(min)), Type.toInt(x+lineWidth*2), Type.toInt(y));
			}
			if((min/2)==new Double(min/2).intValue()) {
				// Draw minute vertical lines
				g2D.drawLine(Type.toInt(x-1+lineWidth/2),Type.toInt(height-(y)), Type.toInt(x-1+lineWidth/2), Type.toInt(height-(y+delta*2)));
				g2D.drawLine(Type.toInt(x+lineWidth/2),Type.toInt(height-(y)), Type.toInt(x+lineWidth/2), Type.toInt(height-(y+delta*2)));
				g2D.drawLine(Type.toInt(x+1+lineWidth/2),Type.toInt(height-(y)), Type.toInt(x+1+lineWidth/2), Type.toInt(height-(y+delta*2)));


			}

			min+=0.5;
		}

		delta = height/((endMin - startMin)*10);
		for (y=height; y>0; y-=delta) {
			// Draw 1/10 minute marks
			g2D.drawLine(Type.toInt(x),Type.toInt(height-(y)), Type.toInt(x-lineWidth), Type.toInt(height-(y)));
		}


		//****** Display graph ******
		Graphics2D g_display = (Graphics2D)g;
		g_display.setPaint(new TexturePaint(bi, new Rectangle(width, height)));
		g_display.fill(new Rectangle2D.Double(0,0,width,height));

		//****** Save image as png file ******
		ImageUtil iu= new ImageUtil();
		File file1 = new File("Neatline.png");
		iu.savePNG(bi,file1);
	}

	public static void main(String[] args) {
        JFrame f = new Neatline();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

