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

📄 texttitle.java

📁 大家打开看看啊, 很有用的东西
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (font == null) {
            throw new IllegalArgumentException("TextTitle.setPaint(...): "
                                               + "null paint not permitted.");
        }

        // make the change...
        if (!this.paint.equals(paint)) {
            this.paint = paint;
            notifyListeners(new TitleChangeEvent(this));
        }

    }

    /**
     * Returns the background paint.
     *
     * @return the paint.
     */
    public Paint getBackgroundPaint() {
        return this.backgroundPaint;
    }

    /**
     * Sets the background paint.  Registered listeners are notified that
     * the title has been modified.
     *
     * @param paint  the new background paint.
     */
    public void setBackgroundPaint(Paint paint) {

        // make the change...
        this.backgroundPaint = paint;
        notifyListeners(new TitleChangeEvent(this));

    }


    /**
     * Returns true for the positions that are valid for TextTitle (TOP and
     * BOTTOM for now) and false for all other positions.
     *
     * @param position  the position.
     *
     * @return <code>true</code> if position is <code>TOP</code> or <code>BOTTOM</code>.
     */
    public boolean isValidPosition(int position) {

        return ((position == AbstractTitle.TOP) || (position == AbstractTitle.BOTTOM));

    }

    /**
     * Returns the preferred width of the title.
     *
     * @param g2  the graphics device.
     *
     * @return the preferred width of the title.
     */
    public double getPreferredWidth(Graphics2D g2) {

        // get the title width...
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        Rectangle2D titleBounds = font.getStringBounds(text, frc);
        double result = titleBounds.getWidth();

        // add extra space...
        Spacer spacer = getSpacer();
        if (spacer != null) {
            result = spacer.getAdjustedWidth(result);
        }

        return result;

    }

    /**
     * Returns the preferred height of the title.
     *
     * @param g2  the graphics device.
     *
     * @return the preferred height of the title.
     */
    public double getPreferredHeight(Graphics2D g2) {

        double result = 0.0;
        
        if (this.text != null) {
            // get the title height...
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();
            LineMetrics lineMetrics = font.getLineMetrics(text, frc);
            result = lineMetrics.getHeight();

            // add extra space...
            Spacer spacer = getSpacer();
            if (spacer != null) {
                result = spacer.getAdjustedHeight(result);
            }
        }
        return result;

    }

    /**
     * Draws the title on a Java 2D graphics device (such as the screen or a printer).
     *
     * @param g2  the graphics device.
     * @param area  the area within which the title (and plot) should be drawn.
     */
    public void draw(Graphics2D g2, Rectangle2D area) {

        // if the text is null, just return
        if (this.text == null) {
            return;
        }
        
        int position = getPosition();
        if (position == TOP || position == BOTTOM) {
            drawHorizontal(g2, area);
        }
        else {
            throw new RuntimeException("TextTitle.draw(...) - invalid title position.");
        }

    }

    /**
     * Draws the title on a Java 2D graphics device (such as the screen or a printer).
     *
     * @param g2  the graphics device.
     * @param area  the area within which the title should be drawn.
     */
    protected void drawHorizontal(Graphics2D g2, Rectangle2D area) {

        FontRenderContext frc = g2.getFontRenderContext();
        Rectangle2D titleBounds = font.getStringBounds(text, frc);
        LineMetrics lineMetrics = font.getLineMetrics(text, frc);

        double titleWidth = titleBounds.getWidth();
        double leftSpace = 0.0;
        double rightSpace = 0.0;
        double titleHeight = lineMetrics.getHeight();
        double topSpace = 0.0;
        double bottomSpace = 0.0;

        Spacer spacer = getSpacer();
        if (spacer != null) {
            leftSpace = spacer.getLeftSpace(area.getWidth());
            rightSpace = spacer.getRightSpace(area.getWidth());
            topSpace = spacer.getTopSpace(titleHeight);
            bottomSpace = spacer.getBottomSpace(titleHeight);
        }

        double titleY = area.getY() + topSpace;

        // work out the vertical alignment...
        int verticalAlignment = getVerticalAlignment();
        if (verticalAlignment == TOP) {
            titleY = titleY + titleHeight - lineMetrics.getLeading() - lineMetrics.getDescent();
        }
        else  {
            if (verticalAlignment == MIDDLE) {
                double space = (area.getHeight() - topSpace - bottomSpace - titleHeight);
                titleY = titleY + (space / 2) + titleHeight
                                - lineMetrics.getLeading() - lineMetrics.getDescent();
            }
            else {
                if (verticalAlignment == BOTTOM) {
                    titleY = area.getMaxY() - bottomSpace
                                            - lineMetrics.getLeading()
                                            - lineMetrics.getDescent();
                }
            }
        }

        // work out the horizontal alignment...
        int horizontalAlignment = getHorizontalAlignment();
        double titleX = area.getX() + leftSpace;
        if (horizontalAlignment == CENTER) {
            titleX = titleX + ((area.getWidth() - leftSpace - rightSpace) / 2) - (titleWidth / 2);
        }
        else if (horizontalAlignment == LEFT) {
            titleX = area.getX() + leftSpace;
        }
        else if (horizontalAlignment == RIGHT) {
            titleX = area.getMaxX() - rightSpace - titleWidth;
        }

        if (this.backgroundPaint != null) {
            g2.setPaint(this.backgroundPaint);
            g2.fill(area);
        }
        g2.setFont(this.font);
        g2.setPaint(this.paint);
        g2.drawString(text, (float) (titleX), (float) (titleY));

    }

    /**
     * Tests this title for equality with another object.
     *
     * @param obj  the object.
     *
     * @return <code>true</code> or <code>false</code>.
     */
    public boolean equals(Object obj) {

        if (obj == null) {
            return false;
        }

        if (obj == this) {
            return true;
        }

        if (obj instanceof TextTitle) {

            TextTitle t = (TextTitle) obj;
            if (super.equals(obj)) {
                boolean b0 = ObjectUtils.equalOrBothNull(this.text, t.text);
                boolean b1 = ObjectUtils.equalOrBothNull(this.font, t.font);
                boolean b2 = ObjectUtils.equalOrBothNull(this.paint, t.paint);

                return b0 && b1 && b2;
            }
        }

        return false;

    }

    /**
     * Provides serialization support.
     *
     * @param stream  the output stream.
     *
     * @throws IOException  if there is an I/O error.
     */
    private void writeObject(ObjectOutputStream stream) throws IOException {
        stream.defaultWriteObject();
        SerialUtilities.writePaint(this.paint, stream);
    }

    /**
     * Provides serialization support.
     *
     * @param stream  the input stream.
     *
     * @throws IOException  if there is an I/O error.
     * @throws ClassNotFoundException  if there is a classpath problem.
     */
    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        this.paint = SerialUtilities.readPaint(stream);
    }

}

⌨️ 快捷键说明

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