Java Draw String drawStringVertical(Graphics graphics, String string, int x, int y)

Here you can find the source of drawStringVertical(Graphics graphics, String string, int x, int y)

Description

Draw a String vertical.

License

Open Source License

Parameter

Parameter Description
graphics graphics to paint with
string string to to draw
x upper left x
y upper left y

Declaration

public static void drawStringVertical(Graphics graphics, String string, int x, int y) 

Method Source Code


//package com.java2s;
/*/*from   w  ww. j  a  v  a2s  . c om*/
 *  File: GraphicsHelper.java 
 *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
 *  A commercial license is available, see http://www.jaret.de.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

public class Main {
    /**
     * Draw a String vertical. This method might be quite costly since it uses an image to buffer.
     * 
     * @param graphics graphics to paint with
     * @param string string to to draw
     * @param x upper left x
     * @param y upper left y
     */
    public static void drawStringVertical(Graphics graphics, String string, int x, int y) {
        Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
        BufferedImage img = new BufferedImage((int) rect.getWidth(), (int) rect.getHeight(),
                BufferedImage.TYPE_3BYTE_BGR);

        Graphics imageGraphics = img.getGraphics();
        imageGraphics.setColor(Color.WHITE);
        imageGraphics.fillRect(0, 0, img.getWidth(), img.getHeight());
        imageGraphics.setColor(Color.BLACK);

        imageGraphics.drawString(string, 0, (int) rect.getHeight());

        BufferedImage vertImg = new BufferedImage((int) rect.getHeight(), (int) rect.getWidth(),
                BufferedImage.TYPE_3BYTE_BGR);

        for (int xx = 0; xx < img.getWidth(); xx++) {
            for (int yy = 0; yy < img.getHeight(); yy++) {
                vertImg.setRGB(yy, img.getWidth() - xx - 1, img.getRGB(xx, yy));
            }
        }

        graphics.drawImage(vertImg, x, y, new ImageObserver() {

            public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                return false;
            }

        });

    }
}

Related

  1. drawStringPair(Graphics2D g, String str1, String str2, int left, int right, int y, int size, Color color, boolean bold)
  2. drawStringRight(final Graphics g, final FontMetrics m, final String str, final int x, final int y)
  3. drawStringRightAlignedVTop(Graphics graphics, String string, int x, int yTop)
  4. drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)
  5. drawStringVCentered(Graphics graphics, String string, int x, int upperY, int lowerY)
  6. drawStringWithHighlighting(Graphics g, String s, int x, int y, Color foreground, Color highlighting)