⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transformedtextlabel.java

📁 一个很好的java2d小程序的源码
💻 JAVA
字号:
package wt;// Source File Name:   TransformedTextLabel.javaimport java.awt.*;import java.awt.font.*;import java.awt.geom.*;import java.util.Vector;import javax.swing.JComponent;public abstract class TransformedTextLabel extends JComponent{    private class NotSubClassedError extends Error    {        public NotSubClassedError()        {        }        public NotSubClassedError(String s)        {            super(s);        }    }    public TransformedTextLabel()    {        this("");    }    public TransformedTextLabel(String text)    {        mDebugOutlines = false;        super.setFont(new Font("SansSerif", 1, 24));        setText(text);    }    protected int length()    {        return mText.length();    }    protected void computeTextMetrics()    {        int n = length();        mCharWidths = new double[n];        mCharDists = new double[n];        mTextDist = 0.0D;                Font font = getFont();        FontRenderContext frc = new FontRenderContext(((Graphics2D)getGraphics()).getTransform(), true, true);                System.out.println(mText);        GlyphVector gv = font.createGlyphVector(frc, mText);        for(int i = 0; i < n; i++)        {            Rectangle2D letterRect = gv.getGlyphOutline(i).getBounds2D();            double w = letterRect.getWidth();            double a = gv.getGlyphMetrics(i).getAdvance();            if(w <= 0.0D)                w = a;            mCharWidths[i] = w;            mCharDists[i] = mTextDist + w / 2D;            mTextDist += i != n - 1 ? a : w;                        System.out.println("mTextDist="+mTextDist);           // System.out.println("mCharDists[i]="+mCharDists[i]);           // System.out.println("mCharWidth="+mCharWidths[i]);        }    }    protected void invalidateTextMetrics()    {        mCharWidths = null;        mCharDists = null;        mTextDist = 0.0D;        invalidateTransforms();    }    protected void validateTextMetrics()    {        if(mCharWidths == null)            computeTextMetrics();    }    protected double getCharWidth(int i)    {        validateTextMetrics();        return mCharWidths[i];    }    protected double getCharDist(int i)    {        validateTextMetrics();        return mCharDists[i];    }    protected double getTextDist()    {        validateTextMetrics();        return mTextDist;    }    protected void invalidateLetterBounds()    {        mLetterBounds = null;        repaint();    }    protected Rectangle2D getLetterBounds()    {        if(mLetterBounds == null)            mLetterBounds = computeLetterBounds();        return mLetterBounds;    }    protected Rectangle2D computeLetterBounds()    {        validateTextMetrics();        Rectangle2D bounds = null;        int n = length();        if(mDebugOutlines)            mOutlines = new Vector();        GlyphVector gv = getFont().createGlyphVector(new FontRenderContext(((Graphics2D)getGraphics()).getTransform(), true, true), mText);        AffineTransform warpAT = new AffineTransform();        for(int i = 0; i < n; i++)        {            warpAT.setToIdentity();            warpAT.concatenate(getTransform(i));            warpAT.translate(-mCharWidths[i] / 2D, 0.0D);            Shape letterRect = warpAT.createTransformedShape(gv.getGlyphOutline(i).getBounds2D());            Rectangle2D dispRect = letterRect.getBounds2D();            if(mDebugOutlines)                mOutlines.add(letterRect);            if(bounds == null)                bounds = dispRect;            else                Rectangle2D.union(bounds, dispRect, bounds);        }        if(bounds == null)            bounds = new java.awt.geom.Rectangle2D.Double();        if(mDebugOutlines)            mOutlines.add(bounds);        return bounds;    }    protected void invalidateTransforms()    {        mTransforms = null;        invalidateLetterBounds();    }    protected void setTransform(int i, AffineTransform at)    {        if(mTransforms == null)            mTransforms = new AffineTransform[length()];        mTransforms[i] = at;        invalidateLetterBounds();    }    protected AffineTransform getTransform(int i)    {        if(mTransforms == null || mTransforms[i] == null)            setTransform(i, computeTransform(i));        return mTransforms[i];    }    protected AffineTransform computeTransform(int i)    {        throw new NotSubClassedError("either subclass TransformedTextLabel or call setTransform for all characters");    }    public void setText(String text)    {        mText = text;        invalidateTextMetrics();    }    public String getText()    {        return mText;    }    public void setDebugOutlines(boolean draw)    {        if(mDebugOutlines == draw)            return;        mDebugOutlines = draw;        if(mDebugOutlines)            invalidateLetterBounds();        repaint();    }    public boolean getDebugOutlines()    {        return mDebugOutlines;    }    public void setFont(Font font)    {        super.setFont(font);        invalidateTextMetrics();    }    public Dimension getPreferredSize()    {        Rectangle2D bounds = getLetterBounds();        return new Dimension((int)bounds.getWidth() + 4, (int)bounds.getHeight() + 4);    }    public void paintComponent(Graphics gIn)    {    	System.out.println("width:"+getWidth());    	System.out.println("height:"+getHeight());        if(isOpaque())        {            gIn.setColor(getBackground());            gIn.fillRect(0, 0, getWidth(), getHeight());            gIn.setColor(getForeground());        }        Rectangle2D bounds = getLetterBounds();        Graphics2D g = (Graphics2D)gIn.create();        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);        g.translate((double)getWidth() / 2D - bounds.getCenterX(), (double)getHeight() / 2D - bounds.getCenterY());        AffineTransform baseTransform = g.getTransform();        int n = length();        for(int i = 0; i < n; i++)        {            g.setTransform(baseTransform);            g.transform(getTransform(i));            g.translate(-mCharWidths[i] / 2D, 0.0D);            g.drawString(mText.substring(i, i + 1), 0, 0);        }        if(mDebugOutlines)        {            g.setTransform(baseTransform);            g.setColor(Color.red);            for(int i = 0; i < mOutlines.size(); i++)                g.draw((Shape)mOutlines.get(i));        }        g.dispose();    }    private String mText;    private double mCharWidths[];    private double mCharDists[];    private double mTextDist;    private Rectangle2D mLetterBounds;    private AffineTransform mTransforms[];    private boolean mDebugOutlines;    private Vector mOutlines;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -