// Copyright Andy Deck 2002, GNU Public License 2
// With the notable provision that any commercial use
// must be approved by the copyright holder.
import java.awt.*;
import java.awt.*;
import java.util.StringTokenizer;

public class scribble extends Module
{

    public scribble()
    {
        lineWid = 30;
        count = 7;
        off = new double[2];
    }

    public void init(lexicon lexicon1, String s)
    {
        super.lex = lexicon1;
        try
        {
            StringTokenizer stringtokenizer = new StringTokenizer(s);
            switch(stringtokenizer.countTokens())
            {
            case 1: // '\001'
                lineWid = Integer.parseInt(stringtokenizer.nextToken());
                break;

            case 2: // '\002'
                lineWid = Integer.parseInt(stringtokenizer.nextToken());
                count = Integer.parseInt(stringtokenizer.nextToken());
                break;
            }
            if(count < 1 || count > 20)
                count = 7;
            if(lineWid < 1 || lineWid > 255)
                lineWid = 30;
        }
        catch(Exception exception)
        {
            count = 7;
            lineWid = 30;
        }
        off[0] = 0.0D;
        off[1] = 0.0D;
        thickness = 1.0D;
    }

    public void finish(int i, int j, int k, int l, int i1, Color color, Color color1)
    {
        Polygon polygon = new Polygon();
        polygon.addPoint((int)Math.round((double)(float)l - off[0]), (int)Math.round((double)(float)i1 - off[1]));
        polygon.addPoint(j, k);
        polygon.addPoint((int)Math.round((double)(float)l + off[0]), (int)Math.round((double)(float)i1 + off[1]));
        Graphics g = super.lex.img.getGraphics();
        if(g != null)
        {
            setRenderMode(g);
            g.setColor(color);
            g.fillPolygon(polygon);
            g.setColor(color1);
            g.drawPolygon(polygon);
            g.dispose();
        }
    }

    public void drawSegment(int i, int j, int k, int l, int i1, Color color, Color color1)
    {
        double d = j - l;
        double d1 = k - i1;
        double d2 = Math.sqrt(d * d + d1 * d1);
        if(d2 == 0.0D)
            return;
        double d3 = thickness;
        double d4 = (d2 / 4D) % 2D;
        if((double)Math.round((d2 + d3) / 2D) < d3)
            d3 -= d4;
        else
            d3 += d4;
        d3 = Math.min(d3, i);
        double d5 = Math.min(Math.round((d3 + thickness) / 2D), (double)i / 2D);
        double d6 = d2 != 0.0D ? (d5 * d1) / d2 : off[0];
        double d7 = d2 != 0.0D ? (-d5 * d) / d2 : off[1];
        Polygon polygon = new Polygon();
        polygon.addPoint((int)Math.round((double)l - off[0]), (int)Math.round((double)i1 - off[1]));
        polygon.addPoint((int)Math.round((double)j - d6), (int)Math.round((double)k - d7));
        polygon.addPoint((int)Math.round((double)j + d6), (int)Math.round((double)k + d7));
        polygon.addPoint((int)Math.round((double)l + off[0]), (int)Math.round((double)i1 + off[1]));
        Graphics g = super.lex.img.getGraphics();
        if(g != null)
        {
            setRenderMode(g);
            g.setColor(color);
            g.fillPolygon(polygon);
            g.drawLine(j, k, l, i1);
            g.setColor(color1);
            g.drawPolygon(polygon);
            g.dispose();
        }
        Rectangle rectangle = new Rectangle((int)((double)j - d5), (int)((double)k - d5), (int)d3, (int)d3);
        Rectangle rectangle1 = rectangle.union(polygon.getBoundingBox());
        rectangle1.grow(1, 1);
        l = j;
        i1 = k;
        off[0] = d6;
        off[1] = d7;
        thickness = d3;
        super.repaint(rectangle1.x - 1, rectangle1.y - 1, rectangle1.width + 2, rectangle1.height + 2);
    }

    public boolean handleEvent(Event event)
    {
        if(event.id == 501)
        {
            ox = event.x;
            oy = event.y;
            return true;
        }
        if(event.id == 506)
        {
            int i = event.x;
            int j = event.y;
            if(ox == 0 && oy == 0)
            {
                ox = i;
                oy = j;
            }
            drawSegment(lineWid, event.x, event.y, ox, oy, count % 2 != 0 ? Color.black : Color.white, count % 2 != 0 ? Color.white : Color.black);
            ox = i;
            oy = j;
            return true;
        }
        if(event.id == 502)
        {
            count--;
            ox = event.x;
            oy = event.y;
            if(count == 0)
                super.lex.showNext();
            finish(lineWid, event.x, event.y, ox, oy, count % 2 != 0 ? Color.black : Color.white, count % 2 != 0 ? Color.white : Color.black);
            return true;
        } else
        {
            return false;
        }
    }

    int lineWid;
    int ox;
    int oy;
    int count;
    double thickness;
    double off[];
    public static final int CURSOR = 8;
}
