Java Graphics Draw String drawScaleLabel(Graphics g, String label, int x, int y, boolean yAxisP)

Here you can find the source of drawScaleLabel(Graphics g, String label, int x, int y, boolean yAxisP)

Description

Draws the scale label in the String label using the graphics in the Graphics object g.

License

Open Source License

Parameter

Parameter Description
g <code>java.awt.Graphics</code> object to the graphics.
label <code>String</code> label of the scale label.
x int x-coordinate of the scale label.
y int y-coordinate of the scale label.
yAxisP boolean flag indicating whether the scale label is for the y-axis.

Declaration

private static void drawScaleLabel(Graphics g, String label, int x, int y, boolean yAxisP) 

Method Source Code

//package com.java2s;
/**//from w w  w  .j a  v a 2s .c o m
* PlotUtilities.java
*
*
* Cytobank (TM) is server and client software for web-based management, analysis,
* and sharing of flow cytometry data.
*
* Copyright (C) 2009 Cytobank, Inc. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Cytobank, Inc.
* 659 Oak Grove Avenue #205
* Menlo Park, CA 94025
*
* http://www.cytobank.org
*/

import java.awt.*;

public class Main {
    /**
    * <p>
    * Draws the scale label in the <code>String</code> label using the graphics
    * in the <code>Graphics</code> object g.
    * </p>
    *
    * <p>
    * The scale label is drawn using the current font in the graphics in the
    * <code>Graphics</code> object g.
    * </p>
    *
    * @param g
    * <code>java.awt.Graphics</code> object to the graphics.
    * @param label
    * <code>String</code> label of the scale label.
    * @param x
    * int x-coordinate of the scale label.
    * @param y
    * int y-coordinate of the scale label.
    * @param yAxisP
    * boolean flag indicating whether the scale label is for the
    * y-axis.
    */
    private static void drawScaleLabel(Graphics g, String label, int x, int y, boolean yAxisP) {
        if ((g == null) || (label == null) || (label.length() <= 0)) {
            // If the graphics is null or the label is null or empty, then quit.
            return;
        }

        // Get the font metrics of the current font
        FontMetrics metrics = g.getFontMetrics();

        // Get the height of the current font
        int fontHeight = metrics.getHeight();

        // Get the width of the label in the current font
        int fontWidth = metrics.stringWidth(label);

        if (yAxisP) {
            // If the scale label is for the y-axis, then draw the label
            // centered around the y-coordinate with the right at the
            // x-coordinate.
            g.drawString(label, x - fontWidth, y + (fontHeight / 2));
        } else {
            // Otherwise, the scale label is for the x-axis, so draw the label
            // centered around the x-coordinate with the top at the
            // y-coordinate.
            g.drawString(label, x - (fontWidth / 2), y + fontHeight);
        }
    }
}

Related

  1. drawLineDrawChar(Graphics g, int x, int y, int bi, char c, int charWidth, int charHeight)
  2. drawMessage(Graphics2D g, String message)
  3. drawRightJustifiedText(String text, int right, int y, Graphics g)
  4. drawRightText(Graphics g, String str, int x, int y, int width, int height)
  5. drawRotatedShape(final Shape shape, final Graphics2D g2, final float x, final float y, final double angle)
  6. drawSystemNameLabel(Graphics2D g, String sysName, Color color, double safetyOffset, boolean isLocationKnownUpToDate)
  7. drawText(Graphics graphics, int x, int y, String text)
  8. drawText(Graphics2D graphics, Font font, Dimension2D dimension, String text)
  9. drawTextCenter(Graphics2D g2, String str, int x, int y)