📄 maincanvas.java
字号:
package demo;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
public class MainCanvas extends GameCanvas implements Runnable{
private boolean m_bRunning; //控制项目运行
public int m_nPosY; //控制文字位置的纵坐标
public Font m_aFont[]; //Font类的数组,存储多种字体
public MainCanvas(){
super(true);
//变量初始画
m_nPosY = 0;
m_aFont = new Font[5]; //分配数组大小
//存储各种字体,如果创建新字体失败,则存储系统默认字体
try{
m_aFont[0] = Font.getFont(Font.FACE_SYSTEM,
Font.STYLE_BOLD, Font.SIZE_MEDIUM);
}catch(Exception e){
m_aFont[0] = Font.getDefaultFont();
}
try{
m_aFont[1] = Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_UNDERLINED|Font.STYLE_BOLD,
Font.SIZE_SMALL);
}catch(Exception e){
m_aFont[1] = Font.getDefaultFont();
}
try{
m_aFont[2] = Font.getFont(Font.FACE_SYSTEM,
Font.STYLE_ITALIC, Font.SIZE_MEDIUM);
}catch(Exception e){
m_aFont[2] = Font.getDefaultFont();
}
try{
m_aFont[3] = Font.getFont(Font.FACE_SYSTEM,
Font.STYLE_UNDERLINED, Font.SIZE_SMALL);
}catch(Exception e){
m_aFont[3] = Font.getDefaultFont();
}
try{
m_aFont[4] = Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_PLAIN, Font.SIZE_LARGE);
}catch(Exception e){
m_aFont[4] = Font.getDefaultFont();
}
Start(); //启动线程
}
public void Start(){
m_bRunning = true;
Thread thread = new Thread(this); //分配新线程
thread.start(); //线程启动
}
public void run() { //新线程自动调用此方法
//得到系统当前时间,并将时间换算成毫秒
long T1 = System.currentTimeMillis();
long T2 = T1;
while(m_bRunning){
T2 = System.currentTimeMillis();
if( T2 - T1 > 100 ){ //间隔100毫秒
T1 = T2;
Input();
Logic();
Paint();
}
}
}
public void Stop(){ //终止游戏
m_bRunning = false;
}
public void Input(){
}
public void Logic(){
//不断更改m_nPosY的值,实现文字滚屏
m_nPosY ++;
//如果文字已滚至屏幕最下方,则重新设置文字的纵坐标
if( m_nPosY > getHeight() )
m_nPosY = 0;
}
public void Paint(){
Graphics g = getGraphics();
//用黑色清屏
g.setColor(0x00000000);
g.fillRect( 0, 0, getWidth(), getHeight() );
g.setColor(0x00ffff00);
//设置当前字体,并以不同字体输出“2008年奥运会”几个字
g.setFont(m_aFont[0]);
g.drawString( "2008年奥运会", 20, m_nPosY - 40, 0 );
g.setFont(m_aFont[1]);
g.drawString( "2008年奥运会", 50, m_nPosY - 25, 0 );
g.setFont(m_aFont[2]);
g.drawString( "2008年奥运会", 80, m_nPosY, 0 );
g.setFont(m_aFont[3]);
g.drawString( "2008年奥运会", 90, m_nPosY + 26, 0 );
g.setFont(m_aFont[4]);
g.drawString( "2008年奥运会", 40, m_nPosY + 42, 0 );
flushGraphics();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -