/*
 * TextElementComponent.java  $Revision: 1.4 $  $Date: 2007/03/11 07:10:43 $
 *
 * Copyright (c) Meldcraft, Inc. 2006
 */
package com.meldcraft.mvcexample;

import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.util.HashMap;

import javax.swing.*;

/**
 * Copied from MVCExample http://java.sun.com/developer/technicalArticles/javase/mvc/
 * and slightly modified
 * 
 * @version $Revision: 1.4 $ $Date: 2007/03/11 07:10:43 $
 * @author Rob Richards
 */
public class TextElementComponent extends JComponent {

    private int xPosition = 20;
    private int yPosition = 20;
    private Font font;
    private String text;
    private int rotation;
    private int opacity = 100;

    public void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D)g;
        g.setColor(Color.white);
        g.fillRect(0,0, getWidth(),getHeight());

        //  If the current string in the model is empty, then we can return
        //  without painting anything.
        if ((text == null) || text.equals(""))
            return;

        //  Set up our rendering hints

        HashMap renderingHints = new HashMap();
        renderingHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        renderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        renderingHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        renderingHints.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        g2.addRenderingHints(renderingHints);

        //  Set up an attributes HashMap to represent the current font

        HashMap attributes = new HashMap();
        attributes.put(TextAttribute.FONT, font);

        //  Create an AlphaComposite using the current opacity value

        AlphaComposite composite = AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, (float)(opacity / 100.0));
        g2.setComposite(composite);

        //  Create an AffineTransform to handle the X and Y position, as
        //  well as the rotation.

        AffineTransform affineTransform = new AffineTransform();
        affineTransform.translate(xPosition, yPosition);
        affineTransform.rotate(rotation*(Math.PI / 180.0));
        g2.transform(affineTransform);

        //  Obtain the textShape using the proper attributes, then draw it and fill it

        TextLayout textLayout = new TextLayout(
                text, attributes, g2.getFontRenderContext());
        Shape textShape = textLayout.getOutline(null);

        g2.setPaint(Color.black);
        g2.fill(textShape);

        g2.setPaint(Color.black);
        g2.draw(textShape);
    }

    public void setFont(Font font)
    {
        this.font = font;
        repaint();
    }

    public void setOpacity(int opacity)
    {
        this.opacity = opacity;
        repaint();
    }

    public void setRotation(int rotation)
    {
        this.rotation = rotation;
        repaint();
    }

    public void setText(String text)
    {
        this.text = text;
        repaint();
    }

    public void setXPosition(int position)
    {
        xPosition = position;
        repaint();
    }

    public void setYPosition(int position)
    {
        yPosition = position;
        repaint();
    }
}




