/*
 * [TestCanvas.java]
 *
 * Summary: Demonstrate use of java.awt.Canvas. Draw a pixel ruler.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-01-01 initial version
 */
package com.mindprod.example;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Demonstrate use of java.awt.Canvas. Draw a pixel ruler.
 * <p/>
 * Draw a pixel ruler with grid lines every green=5, red=10, blue=100 pixels
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestCanvas
    {
    // ---- PUBLIC METHODS ----

    /**
     * Debugging harness for a Frame
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
        final Frame frame = new Frame();
        TriangleCanvas canvas = new TriangleCanvas();
        frame.add( canvas );
        frame.setSize( 1000, 1000 );
        frame.addWindowListener( new WindowAdapter()
            {
            /**
             * Handle request to shutdown.
             * @param e event giving details of closing.
             */
            public void windowClosing( WindowEvent e )
                {
                System.exit( 0 );
                }
            } );
        frame.validate();
        frame.setVisible( true );
        } // end main
    }

final class GridCanvas extends Canvas
    {
    /**
     * size of square image we generate.
     */
    private static final int size = 1000;

    /**
     * Background colour of the images.
     */
    private static final Color background = Color.WHITE;

    /**
     * colour of minor lines, pale green
     */
    private static final Color five = new Color( 128, 250, 128 );

    /**
     * colour of very major lines, dark blue.
     */
    private static final Color hundred = new Color( 0, 0, 40 );

    /**
     * outline of whole grid
     */
    private static final Color outline = Color.BLACK;

    /**
     * colour of major lines, pale red
     */
    private static final Color ten = new Color( 250, 128, 128 );

    /**
     * font for labelling
     */
    private static final Font font = new Font( "Tiresias PCFont Z", Font.PLAIN, 18 );

    /**
     * does drawing
     *
     * @param g where to paint
     */
    public void paint( Graphics g )
        {
        Graphics2D g2d = ( Graphics2D ) g;
        // for antialising geometric shapes
        g2d.addRenderingHints( new RenderingHints( RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON ) );
        // for antialiasing text
        g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
        g.setColor( background );
        g.fillRect( 0, 0, size, size );
        // draw horizontal lines
        g.setColor( five );
        for ( int i = 0; i <= size; i += 5 )
            {
            g.drawLine( i, 0, i, size );
            }
        // draw vertical lines
        for ( int i = 0; i <= size; i += 5 )
            {
            g.drawLine( 0, i, size, i );
            }
        g.setColor( ten );
        for ( int i = 0; i <= size; i += 10 )
            {
            g.drawLine( i, 0, i, size );
            }
        for ( int i = 0; i <= size; i += 10 )
            {
            g.drawLine( 0, i, size, i );
            }
        g.setColor( hundred );
        for ( int i = 0; i <= size; i += 100 )
            {
            g.drawLine( i, 0, i, size );
            }
        for ( int i = 0; i <= size; i += 100 )
            {
            g.drawLine( 0, i, size, i );
            }
        g.setColor( outline );
        for ( int i = 0; i <= size; i += 100 )
            {
            g.drawLine( i, 0, i, size );
            }
        for ( int i = 0; i <= size; i += 1000 )
            {
            g.drawLine( 0, i, size, i );
            }
        g.setFont( font );
        final FontMetrics fm = g.getFontMetrics( font );
        // label  horizontally
        for ( int i = 200; i <= size - 200; i += 100 )
            {
            final String wording = Integer.toString( i );
            // calc adjustment to centre
            final int xadj = fm.stringWidth( wording ) / 2;
            // across the top
            // x,y is bottom left corner of text
            g.drawString( wording, i - xadj, 100 );
            // across the bottom
            g.drawString( wording, i - xadj, 900 );
            }
        // vertically
        for ( int i = 200; i <= size - 200; i += 100 )
            {
            final String wording = Integer.toString( i );
            // calc adjustment to centre
            final int xadj = fm.stringWidth( wording ) / 2;
            // down the left
            g.drawString( wording, 100 - xadj, i );
            // down the right
            g.drawString( wording, 900 - xadj, i );
            }
        }
    }