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

📄 zfont.java

📁 用Java写的报表.功能如下: 0.内建网络打印,网络预览功能! 1.文件操作。包括url 指定的文件。 2.全功能打印支持。包括打印预览。 3.Undo 和 redo。 4.合并单元格。 5.Cel
💻 JAVA
字号:
/*
 * Copyright 2002 EZCell , Inc. All rights reserved.
 * Version  1.0.
 * Author   W.John
 */
package ezcell;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.Vector;


/**
 * DOCUMENT ME!
 *
 * @version 1.00
 * @author W.John
 */
public class ZFont implements Serializable, Cloneable {
    String name = "Arial";
    int style = 0;
    int size = 12;
    boolean outline;
    Color outlineColor;

    /**
     * put your documentation comment here
     */
    public ZFont() {
        this("Arial", 0, 12);
    }

    public ZFont(String name, int style, int size) {
        this.name = name;
        this.style = style;
        this.size = size;
    }

    /**
     * put your documentation comment here
     * @param bold
     */
    public void setBold(boolean bold) {
        if (bold) {
            style |= Font.BOLD;
        } else {
            style &= ~Font.BOLD;
        }
    }

    /**
     *
     * @return
     */
    public boolean isBold() {
        return (style & Font.BOLD) != 0;
    }

    /**
     * put your documentation comment here
     * @param italic
     */
    public void setItalic(boolean italic) {
        if (italic) {
            style |= Font.ITALIC;
        } else {
            style &= ~Font.ITALIC;
        }
    }

    /**
     *
     * @return
     */
    public boolean isItalic() {
        return (style & Font.ITALIC) != 0;
    }

    /**
     * put your documentation comment here
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     *
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @param outline
     */
    public void setOutline(boolean outline) {
        this.outline = outline;
    }

    /**
     *
     * @return
     */
    public boolean isOutline() {
        return outline;
    }

    /**
     *
     * @param outlineColor
     */
    public void setOutlineColor(Color outlineColor) {
        this.outlineColor = outlineColor;
    }

    /**
     *
     * @return
     */
    public Color getOutlineColor() {
        return outlineColor;
    }

    /**
     * put your documentation comment here
     * @param size
     */
    public void setSize(int size) {
        this.size = size;
    }

    /**
     *
     * @return
     */
    public int getSize() {
        return size;
    }

    /**
     * put your documentation comment here
     * @param style
     */
    public void setStyle(int style) {
        this.style = style;
    }

    /**
     *
     * @return
     */
    public Font getSwingFont() {
        return new Font(name, style, size);
    }

    /**
     * put your documentation comment here
     * @param underline
     */
    public void setUnderline(boolean underline) {
        //        this.underline = underline;
    }

    /**
     * put your documentation comment here
     * @return
     */
    public boolean isUnderline() {
        return false; //underline;
    }

    /**
     *
     * @return
     */
    public Object clone() {
        Object font = null;

        try {
            font = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        return font;
    }

    /**
     *
     * @param g2
     * @param cellLoc
     * @param str
     * @param alignX
     * @param alignY
     */
    public void paint(Graphics2D g2, ZRect cellLoc, String str, int alignX, int alignY, boolean wrap) {
        if ((str == null) || str.equals("")) {
            return;
        }

        boolean singleLineMode = true;

        g2.setFont(getSwingFont());

        int strWidth;
        int strHeight;

        strWidth = strHeight = 0;

        // compute dimension of text image
        AttributedString attrStr;
        Vector tls = null;

        if (wrap || isOutline()) {
            attrStr = new AttributedString(str);
            attrStr.addAttribute(TextAttribute.FONT, g2.getFont());

            int w = cellLoc.getWidth();

            if (!wrap) {
                w = Integer.MAX_VALUE;
            }

            tls = this.getTextLayouts(g2, attrStr, w);

            if (isOutline() || (tls.size() > 1)) {
                singleLineMode = false;

                for (int i = 0; i < tls.size(); i++) {
                    TextLayout tl = (TextLayout) tls.get(i);
                    strHeight += (tl.getDescent() + tl.getLeading() + tl.getAscent());
                    strWidth = Math.max(strWidth, (int) tl.getBounds().getWidth());
                }
            }
        }

        if (singleLineMode) {
            // get the str demension
            FontMetrics fm = g2.getFontMetrics(g2.getFont());
            strWidth = fm.stringWidth(str);
            strHeight = fm.getHeight() - fm.getLeading() - fm.getDescent();
        }

        // where the str from x
        int strX;

        // from y
        int strY;

        if (singleLineMode) {
            if (alignX == ZBase.LEFT) {
                strX = cellLoc.left;
            } else if (alignX == ZBase.XCENTER) {
                strX = (cellLoc.left + (cellLoc.getWidth() / 2)) - (strWidth / 2);
            } else {
                strX = (cellLoc.left + cellLoc.getWidth()) - strWidth;
            }

            if (alignY == ZBase.BOTTOM) {
                strY = cellLoc.top + cellLoc.getHeight();
            } else if (alignY == ZBase.TOP) {
                strY = cellLoc.top + strHeight;
            } else {
                strY = cellLoc.top + (cellLoc.getHeight() / 2) + (strHeight / 2);
            }

            g2.drawString(str, strX, strY);
        } else {
            if (alignY == ZBase.BOTTOM) {
                strY = cellLoc.bottom - strHeight;
            } else if (alignY == ZBase.TOP) {
                strY = cellLoc.top;
            } else {
                strY = cellLoc.top + ((cellLoc.getHeight() - strHeight) / 2);
            }

            AffineTransform afSave = g2.getTransform(); //save initial

            // Prepares outline color and fill brush of the painted text.
            Color strokeColor = null;
            Paint savePaint = null;
            Shape saveClip = null;

            if (isOutline()) {
                saveClip = g2.getClip();
                savePaint = g2.getPaint();
                strokeColor = (outlineColor == null) ? Color.red : outlineColor;
                g2.setStroke(new BasicStroke(1.5f));
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);


            }

            for (int i = 0; i < tls.size(); i++) {
                TextLayout tl = (TextLayout) tls.get(i);

                if (alignX == ZBase.LEFT) {
                    strX = cellLoc.left;
                } else if (alignX == ZBase.XCENTER) {
                    strX = (cellLoc.left + (cellLoc.getWidth() / 2)) - ((int) tl.getBounds().getWidth() / 2);
                } else {
                    strX = (cellLoc.left + cellLoc.getWidth()) - (int) tl.getBounds().getWidth();
                }


                // Move down to baseline:
                strY += tl.getAscent();

                if (isOutline()) {
                                 g2.setStroke(new BasicStroke(2.5f));
                    Shape outline = tl.getOutline(null);
                    g2.setColor(strokeColor);
                    g2.translate(strX, strY);
                    g2.draw(outline);
                    g2.setPaint(savePaint);
                    g2.clip(outline);
                    g2.fill(outline);
                    g2.setTransform(afSave);
                    g2.setClip(saveClip);
                } else {
                    // Draw line:
                    tl.draw(g2, strX, strY);
                }


                // Move down to top of next line:
                strY += (tl.getDescent() + tl.getLeading());
            }

            g2.setTransform(afSave);
        }
    }

    /**
     * Returns all TextLayouts after split of the specified string
     * @param g2     Graphics object .
     * @param attrStr  An AttributedString contains string need to be separated.
     * @param width   Maximium width of line width.
     *
     * @return A collection of TextLayout  .
     */
    private Vector getTextLayouts(Graphics2D g2, AttributedString attrStr, int width) {
        // Get iterator for string:
        AttributedCharacterIterator characterIterator = attrStr.getIterator();

        // Get font context from graphics:
        FontRenderContext fontRenderContext = g2.getFontRenderContext();

        // Create measurer:
        LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, fontRenderContext);

        Vector tls = new Vector();

        while (measurer.getPosition() < characterIterator.getEndIndex()) {
            // Get line:
            TextLayout textLayout = measurer.nextLayout(width);
            tls.add(textLayout);
        }

        return tls;
    }

    /**
     * put your documentation comment here
     * @param ois
     * @exception ClassNotFoundException, IOException
     */
    private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
        ois.defaultReadObject();
    }

    /**
     * put your documentation comment here
     * @param oos
     * @exception IOException
     */
    private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.defaultWriteObject();
    }
}

⌨️ 快捷键说明

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