Java Draw Vertical String drawVerticalString(Graphics g, String text, Font font, int x, int y)

Here you can find the source of drawVerticalString(Graphics g, String text, Font font, int x, int y)

Description

Draws a string vertically, turned 90 degrees counter-clockwise.

License

Apache License

Parameter

Parameter Description
g the graphics context on which to draw
text the string to draw
font the font to use
x the <i>x </i> coordinate where you want to place the baseline of the text.
y the <i>y </i> coordinate where you want to place the first letter of the text.

Declaration

public static void drawVerticalString(Graphics g, String text, Font font, int x, int y) 

Method Source Code


//package com.java2s;
/*//w w  w  .ja  v a2 s. co m
 * The baseCode project
 * 
 * Copyright (c) 2006 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

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.AffineTransform;

public class Main {
    /**
     * Draws a string vertically, turned 90 degrees counter-clockwise. Read carefully what the <i>x </i> and <i>y </i>
     * coordinates means; chances are that if you draw to (x,y) = (0,0), you won't see anything.
     * 
     * @param g the graphics context on which to draw
     * @param text the string to draw
     * @param font the font to use
     * @param x the <i>x </i> coordinate where you want to place the baseline of the text.
     * @param y the <i>y </i> coordinate where you want to place the first letter of the text.
     */
    public static void drawVerticalString(Graphics g, String text, Font font, int x, int y) {
        Graphics2D g2 = (Graphics2D) g;
        AffineTransform fontAT = new AffineTransform();
        // fontAT.shear(0.2, 0.0); // slant text backwards
        fontAT.setToRotation(Math.PI * 3.0f / 2.0f); // counter-clockwise 90
        // degrees
        FontRenderContext frc = g2.getFontRenderContext();
        Font theDerivedFont = font.deriveFont(fontAT);
        TextLayout tstring = new TextLayout(text, theDerivedFont, frc);
        tstring.draw(g2, x, y);

    }
}