/Users/petercappello/NetBeansProjects/56-2014/56-2014-1-Clique-OO/src/clique/Node.java
package clique;

import java.awt.Color;
import java.awt.Graphics;
import static java.lang.Math.*;

/**
 *
 * @author Peter Cappello
 */
public class Node
{

    /**
     *
     */
    public static final int IMAGE_SIZE = 600;

    /**
     *
     */
    public static final int BORDER_SIZE = IMAGE_SIZE / 100; // units: pixels

    /**
     *
     */
    public static final int FIGURE_SIZE = IMAGE_SIZE / 2;
    private static int nNodes;
    private static int CLIQUE_RADIUS;
    private static int NODE_DIAMETER;
           
    /**
     * Initialize class variables
     */
    static void initialize( int nNodes )
    {
        Node.nNodes = nNodes;
        NODE_DIAMETER = IMAGE_SIZE / nNodes;
        Node.CLIQUE_RADIUS = ( IMAGE_SIZE - NODE_DIAMETER ) / 2 - BORDER_SIZE;
    }
    
    private final int nodeN; // node #
    private final double angle; // (radians)
    private final int x;
    private final int y;
    private final float redfraction;
    
    /**
     * Constructs a Node object whose number is nodeN.
     * @param nodeN node number (nonnegative).
     */
    public Node( int nodeN )
    { 
        this.nodeN = nodeN;
        angle = 2.0 * PI * nodeN / nNodes;
        x = FIGURE_SIZE + (int) ( CLIQUE_RADIUS * cos( angle ) ) ;
        y = FIGURE_SIZE - ( (int) ( CLIQUE_RADIUS * sin( angle ) ) );
        redfraction = (float) abs( cos( angle ) );
    }
    
    
    /**
     * Draw this Node onto a Graphics object.
     * @param graphics Graphics object on which to draw
     */
    public void draw( Graphics graphics )
    {
        graphics.setColor( Color.red );
        graphics.fillOval( x - NODE_DIAMETER / 2, y - NODE_DIAMETER / 2, NODE_DIAMETER, NODE_DIAMETER );
        graphics.setColor( Color.black );
        graphics.drawOval( x - NODE_DIAMETER / 2, y - NODE_DIAMETER / 2, NODE_DIAMETER, NODE_DIAMETER );
    }
    
    /**
     * Draw an edge between this Node object & the argument Node object.
     * @param graphics the Graphics object on which the edge is drawn.
     * @param toNode The other endpoint of the edge.
     */
    void drawEdge( Graphics graphics,  Node toNode )
    {
        if ( nodeN > toNode.nodeN )
        {
            toNode.drawEdge( graphics, this );
            return;
        }
        
        // compute & set line color                
        float blufraction = (float) abs( sin( toNode.angle ) );
        graphics.setColor( new Color( redfraction, (float) 0.0, blufraction ) );
        graphics.drawLine( x, y, toNode.x, toNode.y );
    }
    
    /**
     * Draw an edge between this Node object & all higher-numbered nodes.
     * @param graphics the Graphics object on which the edge is drawn.
     * @param toNode The other endpoint of the edge.
     */
    void drawEdges( Graphics graphics,  Node[] nodes )
    {
        for ( int stopNode = nodeN + 1; stopNode < nNodes; stopNode++ )
        {
            drawEdge( graphics, nodes[ stopNode ] );                
        }
    }
}