Package org.geoforge.awt.java2d

Source Code of org.geoforge.awt.java2d.MySurfaceText

/*
*
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*   - Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*
*   - 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.
*
*   - Neither the name of Oracle 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 org.geoforge.awt.java2d;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import org.geoforge.java.awt.color.GfrColor;

/**
*
* modified by bantchao
*/
public class MySurfaceText extends MySurfaceAbs
{
   static private TexturePaint _TEXTURE_PAINT_;
   static private Image _IMG_;
  
   static {
        BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
        Graphics2D big = bi.createGraphics();
        big.setBackground(GfrColor.YELLOW);
        big.clearRect(0, 0, 5, 5);
        big.setColor(GfrColor.RED);
        big.fillRect(0, 0, 3, 3);
        _TEXTURE_PAINT_ = new TexturePaint(bi, new Rectangle(0, 0, 5, 5));
    }
  
    transient private boolean doClip = true;
    transient private String clipType = "GP"; // Lines";
   
   
   
   
    transient private String _strText_ = null;
    transient private int _intFontStyle_ = Font.BOLD;
   
    public MySurfaceText(String strText, int intFontStyle)
    {
        this(strText);
    }
   
     public MySurfaceText(String strText)
    {
        super();
       
        this._strText_ = strText;
       
        setBackground(Color.WHITE);
    }
   
    public MySurfaceText(String strText, String strWallPaper)
    {
        super(strWallPaper);
       
        this._strText_ = strText;
       
        setBackground(Color.WHITE);
    }

    @Override
    public void render(int w, int h, Graphics2D g2)
    {
        if (this._strText_ == null)
            return;
       
       
        FontRenderContext frc = g2.getFontRenderContext();
        Font fnt = new Font("sansserif", this._intFontStyle_, 32);
        String str = this._strText_;
        TextLayout tl = new TextLayout(str, fnt, frc);
        double sw = tl.getBounds().getWidth();
        double sh = tl.getBounds().getHeight(); sh *=2;
        double sx = (w - 40) / sw;
        double sy = (h - 40) / sh;
        AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);
        Shape shape = tl.getOutline(Tx);
        sw = shape.getBounds().getWidth();
        sh = shape.getBounds().getHeight();
        Tx =
                AffineTransform.getTranslateInstance(w / 2 - sw / 2, h / 2 + sh
                / 2);
        shape = Tx.createTransformedShape(shape);
        Rectangle r = shape.getBounds();

        if (doClip) {
            g2.clip(shape);
        }

        if (clipType.equals("Lines")) {
            g2.setColor(Color.BLACK);
            g2.fill(r);
            g2.setColor(GfrColor.YELLOW);
            g2.setStroke(new BasicStroke(1.5f));
            for (int j = r.y; j < r.y + r.height; j = j + 3) {
                Line2D line = new Line2D.Float(r.x, j,
                        (r.x + r.width), j);
                g2.draw(line);
            }
        } else if (clipType.equals("Image")) {
            g2.drawImage(_IMG_, r.x, r.y, r.width, r.height, null);
        } else if (clipType.equals("TP")) {
            g2.setPaint(_TEXTURE_PAINT_);
            g2.fill(r);
        } else if (clipType.equals("GP")) {
            g2.setPaint(new GradientPaint(0, 0, GfrColor.GREEN, w, h, GfrColor.BLUE));
            g2.fill(r);
        } else if (clipType.equals("Text")) {
            g2.setColor(Color.BLACK);
            g2.fill(shape.getBounds());
            g2.setColor(GfrColor.CYAN);
            fnt = new Font("serif", this._intFontStyle_, 10);
            tl = new TextLayout("java", fnt, frc);
            sw = tl.getBounds().getWidth();

            int x = r.x;
            int y = (int) (r.y + tl.getAscent());
            sh = r.y + r.height;
            while (y < sh) {
                tl.draw(g2, x, y);
                if ((x += (int) sw) > (r.x + r.width)) {
                    x = r.x;
                    y += (int) tl.getAscent();
                }
            }
        }
        g2.setClip(new Rectangle(0, 0, w, h));

        g2.setColor(Color.GRAY);
        g2.draw(shape);
    }
   
}
TOP

Related Classes of org.geoforge.awt.java2d.MySurfaceText

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.