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坐标.
Graphics是很重要的类在J2ME里,它有9个静态属性:
BASELINE
、
BOTTOM
、
HCENTER
、
LEFT
、
RIGHT
、
SOLID
、
DOTTED
、
TOP
、
VCENTER
,其中三个BASELINE、DTTED是不能用来修饰文本的,他的绘制方法的参数记得当时学的时候疑惑了半天,我觉得我们在看它的参数的时候先想要是自己去设计这个方法自己会怎么样,再写个代码把各种放置参数都测试一下,就一幕了然了.下面是我的理解吧.
1,弄清出坐标:在MIDP中的坐标的y方向与我们平时的相反,它的y轴的方向是向下的.
2,弄清那些静态属性是不可以组合的,如:RIGHT和LEFT显然不能组合,这些组合我们不要想得太复杂,常识判断就够了
3,弄清各个静态属性的使用范围,那些可以修饰图片,那些可以修饰文本等等
4,要把绘制的对象想像成一个矩形,下面是我提供的入门级别的例子.
package datuu.srk.demo.canvas;
<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.Image;
import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
<o:p> </o:p>
public class ImageGraphicsDemo extends MIDlet {
private static ImageGraphicsDemo instance;
private static ImageGraphicsDemoCanvas displayImageGraphicsDemo;
public ImageGraphicsDemo() {
instance = this;
displayImageGraphicsDemo = new ImageGraphicsDemoCanvas();
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(instance).setCurrent(displayImageGraphicsDemo);
}
<o:p> </o:p>
protected void pauseApp() {
}
<o:p> </o:p>
protected void destroyApp(boolean _boolean) throws
MIDletStateChangeException {
}
public static void quitApp() {
try {
instance.destroyApp(false);
} catch(MIDletStateChangeException e) {
e.printStackTrace();
}
instance.notifyDestroyed();
}
}
class ImageGraphicsDemoCanvas extends Canvas implements CommandListener {
private Image image;
private static final int ADJUST =Graphics.HCENTER |Graphics.TOP ; //测试时候要改变的值
public ImageGraphicsDemoCanvas() {
init();
}
protected void paint(Graphics g) {
g.setColor(0,255,0);
int x = image.getWidth();
int y = image.getHeight();
System.out.println(“现在的图片坐标是:(“+x+”,”+y+”)”);
g.drawImage(image,x,y,ADJUST);
}
public void init() {
try {
image = Image.createImage(“/dmusic.png”);
} catch(IOException e) {
System.out.println(“打开图片出现异常”);
e.printStackTrace();
}
this.addCommand(new Command(“退出”,Command.EXIT ,1));
this.setCommandListener(this);
}
<o:p> </o:p>
public void commandAction(Command command, Displayable displayable) {
if(command.getCommandType() == Command.EXIT ) {
ImageGraphicsDemo.quitApp();
}
}
}
<o:p> </o:p>
下面是绘画线条的例子,注意要先设置所话的属性,再化
<o:p> </o:p>
package datuu.srk.demo.canvas;
<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.Display;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
<o:p> </o:p>
public class LineGraphicsDemo extends MIDlet {
private static LineGraphicsDemo instance;
private LineGraphicsDemoCanvas displayLineGraphicsDemoCanvas;
public LineGraphicsDemo() {
instance = this;
displayLineGraphicsDemoCanvas = new LineGraphicsDemoCanvas();
}
<o:p> </o:p>
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(instance).setCurrent(displayLineGraphicsDemoCanvas);
}
<o:p> </o:p>
protected void pauseApp() {
}
<o:p> </o:p>
protected void destroyApp(boolean _boolean) throws
MIDletStateChangeException {
}
<o:p> </o:p>
public static void quitApp() {
try {
instance.destroyApp(false);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
instance.notifyDestroyed();
}
}
<o:p> </o:p>
<o:p> </o:p>
class LineGraphicsDemoCanvas extends Canvas implements CommandListener {
private static final int ADJUST = Graphics.DOTTED | Graphics.HCENTER;
public LineGraphicsDemoCanvas() {
this.addCommand(new Command(“退出”, Command.EXIT, 1));
this.setCommandListener(this);
}
<o:p> </o:p>
public void paint(Graphics g) {
<o:p> </o:p>
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
<o:p> </o:p>
g.setColor(255, 0, 0);
g.setStrokeStyle(Graphics.DOTTED); //虚线
g.drawLine(10, 10, 100, 10);
<o:p> </o:p>
g.setColor(0, 255, 0);
g.setStrokeStyle(Graphics.SOLID); //实线
g.drawLine(10, 100, 100, 100);
<o:p> </o:p>
}
<o:p> </o:p>
public void commandAction(Command command, Displayable displayable) {
if (command.getCommandType() == Command.EXIT) {
LineGraphicsDemo.quitApp();
}
}
}