Нарисуйте строку вертикально с прозрачным фоном - PullRequest
0 голосов
/ 20 апреля 2011

Я хотел бы нарисовать строку на экране, повернутую на 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 ]

1 голос
/ 21 апреля 2011
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

0 голосов
/ 21 апреля 2011

Если вы используете Java2D для рисования, вы можете указать java.awt.Graphics2D # setTransform .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...