2011-04-20 184 views
0

垂直繪製字符串我想在屏幕上畫一個線,旋轉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對象繪製它?

回答

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發現

,如果你創建一個`Image`用`Image.createImage(INT,INT)這個類是不是Java API Microedition的
+0

部分 – user 2011-04-21 19:02:01

+1

@JánosHarsányi對不起,我錯過了您的請求透明度,希望這應該能夠解決它使用像素和drawRGB()通過諾基亞http://fivedots.coe.psu.ac.th/Software .coe/J2ME /諾基亞%20Articles/Working_witrh_pixels_and_drawRGB_v1_0.pdf – 2011-04-21 21:40:39