J2ME字體專題
<o:p> </o:p>
1,Font類的特點:
①沒有構造函數
②是final類型的類
③沒有color屬性,只有三性:.
2,特點決定用法:不能被繼承,不能被實例化.
<o:p> </o:p>
3,關于字體的知識:字體的形狀-----face,字體的風格----Style,字體的大小---size
4,J2ME提供的字體:
形狀: FACE_MONOSPACE---等寬字體
FACE_PROPORTIONAL----均衡字體
FACE_SYSTEM----系統字體
風格: STYLE_BOLD-----粗體
STYLE_ITALIC---斜體
STYLE_PLAIN----普通
STYLE_UNDERLINED----下畫線
注意:風格可以組合.如: STYLE_BOLD| STYLE_ITALIC 兩兩組合,或者三者組合STYLE_UNDERLINED |STYLE_BOLD| STYLE_ITALIC
大小: SIZE_LARGE--- 16
SIZE_MEDIUM----0
SIZE_SMALL-----8
<o:p> </o:p>
5,color:由于J2ME技術比較簡單,所以沒有實現專門的顏色類,而只是使用RGB的概念來代表顏色。這里簡單介紹一下RGB的概念,顏色是由紅(Red)、綠(Green)、藍(Blue)三原色組成的,所以可以使用這三個顏色的組合來代表一種具體的顏色,其中R、G、B的每個數值都位于0-255之間。在表達顏色的時候,即可以使用三個數字來表達,也可以使用一個格式如0X00RRGGBB這樣格式的十六進制來表達,下面是常見顏色的表達形式:<o:p></o:p>
紅色:(255,0,0)或0x00FF0000
綠色:(0,255,0)或0x0000FF00
藍色:(255,255,255)或0x00FFFFFF<o:p></o:p>
可以使用setColor(int red,int green,int blue)或者setColor(int RGB)來設置字體的顏色,用getColor()來獲取字體的顏色
<o:p> </o:p>
6.用法:
①在Graphics類中的運用:
package datuu.srk.demo.font;
<o:p> </o:p>
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;
<o:p> </o:p>
public class GraphicsTest extends MIDlet {
private GraphicsTestCanvas showCanvas;
public GraphicsTest() {
showCanvas = new GraphicsTestCanvas();
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(showCanvas);
}
<o:p> </o:p>
protected void pauseApp() {
}
<o:p> </o:p>
protected void destroyApp(boolean _boolean) throws
MIDletStateChangeException {
}
<o:p> </o:p>
<o:p> </o:p>
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);
}
}
}
<o:p> </o:p>
②繪制會動的字體:
package datuu.srk.demo.font;
<o:p> </o:p>
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
<o:p> </o:p>
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);
<o:p> </o:p>
}
<o:p> </o:p>
protected void startApp() {
}
<o:p> </o:p>
protected void pauseApp() {
}
<o:p> </o:p>
protected void destroyApp(boolean unconditional) {
}
<o:p> </o:p>
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
<o:p> </o:p>
<o:p> </o:p>
class HCanvas extends Canvas implements Runnable {
<o:p> </o:p>
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();
<o:p> </o:p>
public HCanvas() {
new Thread(this).start();
}
<o:p> </o:p>
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是字體寬度
}
}
<o:p> </o:p>
public void run() {
while (bStart) {
try {
repaint();
Thread.sleep(70);
k++;
if (k > (adjustHight.length - 1)) {
k = str.length();
}
} catch (InterruptedException e) {}
}
}
<o:p> </o:p>
}
<o:p> </o:p>
③List中的運用很簡單,有List.setFont(int index,Font font)方法,可以把預設定好的字體.
<o:p> </o:p>
小結一下:字體在J2ME中是很重要的一部分,因為我們做出來的軟件美觀也是很重的義部分,字體有很多種,要設置跟更美觀的字體可以使用德國開源包polish,j使用也很簡單,就像css樣式列表一樣使用,通過它可以設置跟網頁一樣美觀的字體,當我們要繪制動態字體的時候,其實就是坐標的變換,左右滾動變換x坐標,上下滾動,變換y坐標.
<o:p> </o:p>
<o:p> </o:p>