/*
 * [TestDraw.java]
 *
 * Summary: Demonstrate use of java.swing.JPanel.
 *
 * 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 2010-01-19 initial version
 */
package com.mindprod.example;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

/**
 * Demonstrate use of java.swing.JPanel.
 * <p/>
 * Draw a rainbow triangle to illustrate Kohlberg's six stages of moral development.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2010-10-19 initial version
 * @since 2010-01-19
 */
public final class TestDraw
    {
    /**
     * size in pixels of the image
     */
    private static final int SIZE = 1024;
    /*
Kohlberg's Six Stages of Moral Evolution
6. Universal ethical principles
   (Principled conscience)
5. Social contract orientation
4. Authority and social-order
   maintaining orientation
   (Law and order morality)
3. Interpersonal accord and conformity
   (Social norms)
   (The good boy/good girl attitude)
2. Self-interest orientation
   (What's in it for me?)
1. Obedience and punishment orientation
   (How can I avoid punishment?)
     */
    // ---- PUBLIC METHODS ----

    /**
     * Debugging harness
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
        // fire up JApplet on the Swing Thread
        SwingUtilities.invokeLater( new Runnable()
            {
            /**
             * do all swing work on the swing thread.
             */
            public void run()
                {
                TriangleCanvas canvas = new TriangleCanvas();
                JFrame.setDefaultLookAndFeelDecorated( true );
                final JFrame frame = new JFrame( "Kohlberg's Six Stages of Moral Development" );
                frame.setResizable( false );
                frame.setUndecorated( false );
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                // allow some extra room for the frame title bar.
                frame.setSize( SIZE + 24, SIZE + 64 );
                frame.getContentPane().add( canvas );
                frame.validate();
                frame.setVisible( true );
                } // end run
            } // end Runnable
        );
        } // end main
    }

/**
 * draws rainbow triangle with Kohlberg's Summary.
 */
final class TriangleCanvas extends JPanel
    {
    /**
     * size in pixels of the image
     */
    private static final int SIZE = 1024;

    private static final String[] bandText =
            {
                    "avoiding punishment",
                    "self-interest",
                    "good boy attitude",
                    "law and order morality",
                    "social contract",
                    "principle"
            };

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

    private static final int[] bandBottoms = {
            SIZE,
            SIZE - 150,
            SIZE - 150 * 2,
            SIZE - 150 * 3,
            SIZE - 150 * 4,
            SIZE - 150 * 5,
            0 };

    private static final int[] bandColors = { 0x6a00ce, 0x005cff, 0x00ff80, 0xfff700, 0xff6200, 0xcc0000 };

    /**
     * does drawing
     *
     * @param g where to paint
     */
    public void paintComponent( Graphics g )
        {
        Graphics2D g2d = ( Graphics2D ) g;
        g2d.setBackground( Color.WHITE );
        // this gets background cleared
        super.paintComponent( g );
        // for antialiasing 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 );
        // draw 6 bands of colour
        for ( int band = 0; band < 6; band++ )
            {
            g2d.setColor( new Color( bandColors[ band ] ) );
            g2d.fillRect( 0,
                    bandBottoms[ band + 1 ],
                    SIZE
                    /* width */,
                    bandBottoms[ band ] - bandBottoms[ band + 1 ]
                    /* height */ );
            }
        g2d.setColor( Color.WHITE );
        /* crop off top left triangle, specify triangle points counterclockwise */
        g2d.fillPolygon( new int[] { 0, 0, SIZE / 2, }, new int[] { 0, SIZE, 0 }, 3 );
        /* crop off top right triangle, specify triangle points counterclockwise */
        g2d.fillPolygon( new int[] { SIZE, SIZE, SIZE / 2, }, new int[] { 0, SIZE, 0 }, 3 );
        g2d.setColor( Color.BLACK );
        /** add top left border to triangle.*/
        g2d.drawLine( SIZE / 2, 0, 0, SIZE );
        /** add top right border to triangle.*/
        g2d.drawLine( SIZE / 2, 0, SIZE, SIZE );
        g2d.setColor( Color.BLACK );
        g.setFont( font );
        final FontMetrics fm = g.getFontMetrics( font );
        // draw text for each band
        for ( int band = 0; band < 6; band++ )
            {
            String wording = bandText[ band ];
            final int xadj = fm.stringWidth( wording ) / 2;
            // across the top
            // x,y is bottom left corner of text
            g.drawString( wording, SIZE / 2 - xadj, bandBottoms[ band ] - 50 );
            }
        }
    }