/* @(#)IntersectTest.java   15 February 2006 */

/* Useful imports */

import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.net.URL;
import java.util.regex.*;
import java.text.ParseException;


public class IntersectTest
    extends DisplayPanel
{
    private BufferedPanel window = new BufferedPanel(400, 400);
    
    private PaintableSequence sequence = window.getPaintableSequence();
    
    private Paintable[] paintables = null;
    
    private TablePanel signalLabel =
        new TablePanel(16, 1, 10, 10, WEST);
    
    private TablePanel signalPanel = null;
    
    private ShapePaintable[][] signals = null;
    
    private Object[]   mainStuff = null;
    
    private TablePanel mainPanel = null;
    
    private Color onSignal  = Colors.red;
    
    private Color offSignal = Colors.blue;
    
    private MouseAction setSignalsAction =
        new MouseAction() {
            public void mouseActionPerformed(MouseEvent evt) {
                setSignals();
            }
        };
    
    private void makePaintables() {
        double radius;
        double x;
        double y;
        double rotate;
        
        radius = 40;
        x = 100;
        y = 100;
        rotate = 24;
        
        Shape shape0 = Shapes.regularPolygon(5, radius, x, y);
        
        Paintable paintable0 =
            new ShapePaintable
                (shape0, PaintMode.FILL_DRAW, Colors.red, Colors.black);
        
        setCenter(paintable0, x, y);
        setRotate(paintable0, rotate);
        
        signalLabel.addObject("0 = Large Polygon", 0, 0);
        
        radius = 40;
        x = 200;
        y = 100;
        rotate = 48;
        
        Shape shape1 = Shapes.regularWavygon(5, radius, x, y);
        
        Paintable paintable1 =
            new ShapePaintable
                (shape1, PaintMode.DRAW, null, Colors.blue);
        
        setCenter(paintable1, x, y);
        setRotate(paintable1, rotate);
        
        signalLabel.addObject("1 = Large Wavygon", 1, 0);
       
        
        radius = 40;
        x = 300;
        y = 100;
        rotate = 36;
        
        Shape shape2 = Shapes.regularStar(7, 3, radius, x, y);
        
        Paintable paintable2 =
            new ShapePaintable
                (shape2, PaintMode.FILL, Colors.green);
        
        setCenter(paintable2, x, y);
        setRotate(paintable2, rotate);
        
        signalLabel.addObject("2 = Large Star", 2, 0);
       
        
        radius = 3;
        x = 100;
        y = 200;
        
        Shape shape3 = new XCircle(x, y, radius);
        
        Paintable paintable3 =
            new ShapePaintable
                (shape3, PaintMode.FILL, Colors.orange);
        
        setCenter(paintable3, x, y);
        
        signalLabel.addObject("3 = Small Dot", 3, 0);
        
        
        radius = 6;
        x = 200;
        y = 200;
        rotate = 13;
        
        Shape shape4 = Shapes.regularWavygon(7, radius, x, y);
        
        Paintable paintable4 =
            new ShapePaintable
                (shape4, PaintMode.FILL, Colors.magenta);
        
        setCenter(paintable4, x, y);
        setRotate(paintable4, rotate);
        
        signalLabel.addObject("4 = Small Wavygon", 4, 0);
        
        
        radius = 8;
        x = 300;
        y = 200;
        rotate = 12;
        
        Shape shape5 = Shapes.regularStar(5, 2, radius, x, y);
        
        Paintable paintable5 =
            new ShapePaintable
                (shape5, PaintMode.DRAW, null, Colors.orangered);
        
        setCenter(paintable5, x, y);
        setRotate(paintable5, rotate);
        
        signalLabel.addObject("5 = Small Star", 5, 0);
        
        
        x = 200;
        y = 300;
        rotate = 10;
        
        float xf = (float) x;
        float yf = (float) y;
        
        Font font = new Font("Serif", Font.PLAIN, 60);
        
        String text = "O my goodness!";
        
        Paintable paintable6 =
            new TextPaintable
                (text, font, Colors.blueviolet, TextAnchor.CENTER_BASELINE, xf, yf);
        
        setCenter(paintable6, x, y);
        setRotate(paintable6, rotate);
        
        signalLabel.addObject("6 = Large Text", 6, 0);
        
        
        paintables = new Paintable[] {
            paintable0,
            paintable1,
            paintable2,
            paintable3,
            paintable4,
            paintable5,
            paintable6,
       };
    }
    
    
    private static void setCenter(Paintable paintable, double x, double y) {
        if (paintable == null)
            return;
        
        Point2D P = new Point2D.Double(x, y);
        
        paintable.setDefaultOriginalCenter(P);
    }
    
    
    private static void setRotate(Paintable paintable, double rotate) {
        if (paintable == null)
            return;
        
        paintable.mutate(Mutator.rotate(rotate));
    }
    
    
    private void makeSignalPanel() {
        int N = paintables.length;
        
        // make signals
        signals = new ShapePaintable[N][N];
        
        Shape square = new XSquare(10);
        
        for (int row = 1; row < N; row++)
            for (int col = 0; col < row; col++)
                signals[row][col] =
                    new ShapePaintable(square, PaintMode.FILL, offSignal);
        
        // make signal panel
        signalPanel = new TablePanel(N+1, N+1, 10, 10, CENTER);
        
        // add row labels
        for (int row = 0; row < N; row++)
            signalPanel.addObject(row + "", row, 0);
        
        // add col labels
        for (int col = 0; col < N; col++)
            signalPanel.addObject(col + "", N, col+1);
        
        // add signals
        for (int row = 1; row < N; row++)
            for (int col = 0; col < row; col++)
                signalPanel.addObject(signals[row][col], row, col+1);
    }
    
    
    private void setSignal(boolean set, int row, int col) {
        signals[row][col].setFillPaint(set ? onSignal : offSignal);
    }
    
    
    private void setSignals() {
        int N = paintables.length;
        
        Shape[] mutated = new Shape[N];
        
        boolean set;
        
        for (int i = 0; i < N; i++)
            mutated[i] = ShapeTools.getMutatedOutline(paintables[i]);
        
        for (int i = 1; i < N; i++)
            for (int j = 0; j < i; j++) {
                set = ShapeTools.intersects(mutated[i], mutated[j]);
                setSignal(set, i, j);
            }
     }
    
    
    private void initialize() {
        makePaintables();
        sequence.addSequence(paintables);
        
        makeSignalPanel();
        
        window.drawGrid(50);
        
        window.installSimpleMouseActions(true);
        
        MouseActionAdapter adapter = window.getMouseActionAdapter();
        
        // testing is too slow to perform while dragging
        // adapter.addMouseDraggedAction (setSignalsAction);
        
        adapter.addMouseReleasedAction(setSignalsAction);
        
        Object[] pairStuff = { signalLabel, signalPanel };
        
        TablePanel pairPanel =
            new TablePanel(pairStuff, HORIZONTAL, 30, 10, CENTER);
        
        mainStuff =
            new Object[] { window, pairPanel };
        
        mainPanel =
            new TablePanel(mainStuff, VERTICAL, 5, 5, CENTER);
        
        add(mainPanel);
    }
    
    
    public IntersectTest() {
        initialize();
        frame("Intersect Test");
    }
    
    
    public static void main(String[] args) {
        new IntersectTest();
    }
    
}

