用透明背景垂直绘制字符串

时间:2011-04-20 19:37:58

标签: java-me

我想在屏幕上绘制一个字符串,在透明背景上旋转90度:

public static void drawStringWithTransformROT90(String text, int x, int y, int color, Graphics g) {
    // create a mutable image with white background color
    Image im = Image.createImage(g.getFont().stringWidth(text), g.getFont().getHeight());
    Graphics imGraphics = im.getGraphics();
    // set text color to black
    imGraphics.setColor(0x00000000);
    imGraphics.drawString(text, 0, 0, Graphics.TOP|Graphics.LEFT);
    int[] rgbData = new int[im.getWidth() * im.getHeight()];
    im.getRGB(rgbData, 0, im.getWidth(), 0, 0, im.getWidth(), im.getHeight());
    for (int i = 0; i < rgbData.length; i++) {
        // if it is the background color (white), set it to transparent
        if (rgbData[i] == 0xffffffff) {
            rgbData[i] = 0x00000000;
        } else {
            // otherwise (black), change the text color
            rgbData[i] = color;
        }
    }
    Image imageWithAlpha = Image.createRGBImage(rgbData, im.getWidth(), im.getHeight(), true);
    Sprite s = new Sprite(imageWithAlpha);
    // rotate the text
    s.setTransform(Sprite.TRANS_ROT90);
    s.setPosition(x, y);
    s.paint(g);
}

有没有更好的方法呢?我应该创建旋转字母的透明图像,并使用Sprite对象绘制它吗?

2 个答案:

答案 0 :(得分:1)

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;

public class MyCanvas extends Canvas {
    public void paint(Graphics g) {
        //The text that will be displayed
        String s="java";
        //Create the blank image, specifying its size
        Image img=Image.createImage(50,50);
        //Create an instance of the image's Graphics class and draw the string to it
        Graphics gr=img.getGraphics();
        gr.drawString(s, 0, 0, Graphics.TOP|Graphics.LEFT);
        //Display the image, specifying the rotation value. For example, 90 degrees
        g.drawRegion(img, 0, 0, 50, 50, Sprite.TRANS_ROT90, 0, 0, Graphics.TOP|Graphics.LEFT);
    }
}

见于http://wiki.forum.nokia.com/index.php/How_to_display_rotated_text_in_Java_ME

答案 1 :(得分:0)

如果您使用Java2D进行绘图,则可以指定java.awt.Graphics2D#setTransform

相关问题