J2ME字体的设置
|
|
Author: Wupei | Date: 2009-03-29 |
View: 2633 |
J2ME开发 - 新手上路 | Digg:
1
|
|
1,Font类的特点: import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Display; public class GraphicsTest extends MIDlet { private GraphicsTestCanvas showCanvas; public GraphicsTest() { showCanvas = new GraphicsTestCanvas(); } protected void startApp() throws MIDletStateChangeException { Display.getDisplay(this).setCurrent(showCanvas); } protected void pauseApp() { } protected void destroyApp(boolean _boolean) throws MIDletStateChangeException { } class GraphicsTestCanvas extends Canvas { private Font myFont; public GraphicsTestCanvas() { //设置字体 myFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED | Font.STYLE_BOLD | Font.STYLE_ITALIC, Font.SIZE_LARGE); } private final String showMessage = "kuikui,你好!"; protected void paint(Graphics g) { g.setFont(myFont); g.drawString(showMessage, this.getWidth() / 2, this.getHeight() / 2, Graphics.TOP | Graphics.LEFT); } } } ②绘制会动的字体: import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class DrawCanvas extends MIDlet implements CommandListener { private Command exitCommand; private HCanvas sg; public DrawCanvas() { exitCommand = new Command("Exit", Command.EXIT, 1); sg = new HCanvas(); sg.addCommand(exitCommand); sg.setCommandListener(this); Display.getDisplay(this).setCurrent(sg); } protected void startApp() { } protected void pauseApp() { } protected void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } } class HCanvas extends Canvas implements Runnable { private String str = new String("Hello,LinuxFans!"); private int[] adjustHight = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 4, 3, 2, 1, 1, 2, 3, 3, 4, 4, 3, 2, 1, }; boolean bStart = true; private int k = str.length(); public HCanvas() { new Thread(this).start(); } protected void paint(Graphics g) { g.setColor(0x00ffffff); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0x00000000); for (int i = 0; i < str.length(); i++) { g.drawString(str.substring(i, i + 1), 20 + i * 7, 10 - adjustHight[k - i], 0); g.drawString(str.substring(i, i + 1), 21 + i * 7, 11 - adjustHight[k - i], 0); //加重字体7是字体宽度 } } public void run() { while (bStart) { try { repaint(); Thread.sleep(70); k++; if (k > (adjustHight.length - 1)) { k = str.length(); } } catch (InterruptedException e) {} } } }③List中的运用很简单,有List.setFont(int index,Font font)方法,可以把预设定好的字体. 小结一下:字体在J2ME中是很重要的一部分,因为我们做出来的软件美观也是很重的义部分,字体有很多种,要设置跟更美观的字体可以使用德国开源包polish,j使用也很简单,就像css样式列表一样使用,通过它可以设置跟网页一样美观的字体,当我们要绘制动态字体的时候,其实就是坐标的变换,左右滚动变换x坐标,上下滚动,变换y坐标. 更多阅读: |