/*
 * Copyright (c) 2001-2005 Frederic Banino. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  o Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  o Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  o Neither the name of Frederic Banino nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.frban.graphics.hatch;

import java.io.Serializable;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;

/**
 * un modèle simplifié de hachures avec des traits pleins, d'épaisseur,
 * d'espacement et d'orientation variables, avec une couleur de rendu
 * unique.
 * @version 1.0
 * @author FB
 * @see HatchedPaintContext
 */
public class HatchedPaint implements Paint, Serializable {
    /** numero de série fixe */
    private static final long serialVersionUID = 650742910953652500L;

    /** couleur des hachures */
    private Color color;
    /** espacement en x des hachures */
    private float space;
    /** angle de hachure */
    private float angle;
    /**épaisseur de trait */
    private float lineWidth;

    /** le paint context associé */
    private transient HatchedPaintContext context;

    /** construit un modèle simple de hachures */
    public HatchedPaint(Color c) {
            this(c,10,0,1.5f);
    }

    /** construit un modèle simple de hachures (angle en radians) */
    public HatchedPaint(Color c, float space, float angle) {
        this(c,space,angle,1.5f);
    }

    /** construit un modèle simple de hachures (angle en radians) */
    public HatchedPaint(Color c, float space, float angle, float lineWidth) {
        this.color=c;
        this.space=space;
        this.angle=angle;
        this.lineWidth = lineWidth;
        initContext();
    }

    /** init du contexte : j'ai besoin de cette fonction parce que le
     * contexte est transient, et que je n'ai donc pas la garantie
     * qu'il soit défini
     */
    private void initContext() {
        context = new HatchedPaintContext(color,space,angle,lineWidth);
    }

    /** le contexte est transparent */
    public int getTransparency() { return Transparency.TRANSLUCENT; }

    /** renvoie un HatchedPaintContext */
    public PaintContext createContext(ColorModel cm,
                Rectangle deviceBounds,
                Rectangle2D userBounds,
                AffineTransform xform,
                RenderingHints hints) {
        if (context==null) initContext();
        return context;
    }

    /** affichage formalisé du contexte */
    public String toString() {
        return "H" + Integer.toHexString(color.getRGB()).substring(2)+"-"+space+":"+Math.round(Math.toDegrees(angle));
    }

    /** renvoie la couleur de hachure */
    public Color getColor() {
        return color;
    }
    /** change la couleur de hachure */
    public void setColor(Color color) {
        this.color = color;
        initContext();
    }
    /** renvoie l'angle des traits */
    public float getAngle() {
        return angle;
    }
    /** fixe l'angle des traits */
    public void setAngle(float angle) {
        this.angle = angle;
        initContext();
    }
    /** renvoie l'espacement des traits */
    public float getSpace() {
        return space;
    }
    /** fixe l'espacement des traits */
    public void setSpace(float space) {
        this.space = space;
        initContext();
    }
    /** renvoie l'épaisseur des traits */
    public float getLineWidth() {
        return lineWidth;
    }
    /** fixe l'épaisseur des traits */
    public void setLineWidth(float lineWidth) {
        this.lineWidth = lineWidth;
        initContext();
    }
}