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

📄 textlengthcalculatorimpl.java

📁 It is all about project scheduling. GanttProject is a tool for creating a project schedule by means
💻 JAVA
字号:
/* * Created on 26.12.2004 */package net.sourceforge.ganttproject.util;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.font.FontRenderContext;import java.awt.font.TextLayout;import java.awt.geom.Rectangle2D;/** * @author bard */public class TextLengthCalculatorImpl implements TextLengthCalculator {    private Graphics2D myGraphics;    private State myState;    public static int getTextLength(Graphics g, String text) {        if(text.length() == 0)            return 0;        Graphics2D g2 = (Graphics2D) g;        FontRenderContext frc = g2.getFontRenderContext();        Font font = g.getFont();        TextLayout layout = new TextLayout(text, font, frc);        Rectangle2D bounds = layout.getBounds();        return (int) bounds.getWidth() + 1;    }    public TextLengthCalculatorImpl(Graphics g) {        if (g != null) {            setGraphics(g);        }    }    public void setGraphics(Graphics g) {        Graphics2D g2 = (Graphics2D) g;        myGraphics = g2;        myState = null;    }    public int getTextLength(String text) {        return getTextLength(myGraphics, text);    }    public Object getState() {        if (myState == null) {            myState = new State(myGraphics.getFontRenderContext(), myGraphics                    .getFont());        }        return myState;    }    static class State {        Object context;        Object font;        State(Object context, Object font) {            this.context = context;            this.font = font;            if (context == null) {                throw new NullPointerException();            }            if (font == null) {                throw new NullPointerException();            }        }        public boolean equals(Object o) {            State rvalue = (State) o;            if (rvalue == null) {                return false;            }            return rvalue.context.equals(this.context)                    && rvalue.font.equals(this.font);        }        public int hashCode() {            return font.hashCode();        }    }}

⌨️ 快捷键说明

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