www.pudn.com > gun.rar > ScrollCanvas.java


 
 
import javax.microedition.lcdui.*; 
 
/** 
 * 

Title: ScrollCanvas

* *

Description: * ScrollCanvas是一个滚动屏幕的实现,实现了电影中的滚动屏幕效果,可以通过函数设置背景,文字的滚动速度,以及暂停滚动,继续滚动等功能。ScrollCanvas是基于J2ME中的Canvas设计,更多的功能正在加入之中。如有问题或者是疑问,请发信到batfree@yeah.net联系。欢迎更多的人使用,改进并提出意见。 *
使用说明:
* ScrollCanvas扩展了Canvas,可以通过ScollCanvas(),ScrollCanvas(String * s),ScrollCanvas(String[] s) * 三种不同的方式进行构造函数。采用无参数构造函数创建之后可以通过setString(String * s)来设定要显示的字符串。
* ScrollCanvas(String s) * 构造时即赋于要显示的字符串。也可以调用setString(String * s)来修改要显示的字符串。
* ScrollCanvas(String[] * s)传入字符串数组,显示时将按顺序显示在屏幕中央。不可以再调节显示内容。
* 使用说明:
代码示例:
ScrollCanvas * sc=new ScrollCanvas(s);
Thread t=new * Thread(sc); // 建立新的线程 ,
t.start() * //开始运行 ;
* 如果想要暂停,我在设计时默认为按下任意键暂停,再次按下将继续执行,也可以在程序中调用pause()函数来暂停执行,调用goOn()来继续刚才暂停的线程。调用destroy()将会停止执行。 * 调用setFont(int face,int style,int * size)来设置显示字体。调用setBackground(int r,int g * ,int b)来设置显示时背景。调用setBackground(Image * img)来设置显示时背景画面。setSpeed(int * i)来设置滚动速度。

* *

Copyright: Copyright (c) 2004 * http://batfree.yeah.net *
版权说明:本程序为开放源码的,采用LGPL许可证。主要为学习J2ME的用途,程序及文档中如果提到其他公司产品,其相应的版权归其公司所有。本程序无任何担保。如有不尽事宜,请写信到batfree@yeah.net询问。

* * *

Company: http://batfree.yeah.net

* * @author Batfree * @version 0.1 */ public class ScrollCanvas extends Canvas implements Runnable { private Image backgroundIMG; private String string = "TestOfScrollCanvas0.1\n Copyright(c)Batfree@yeah.net\n 这是我写的一个用于J2ME的一个手机控件,基于Canvas,具有滚动效果,类似于电视节目演员表的式样,可以自定义背景图像,滚动速度,显示字体,字体的样式以及开始停止等功能,其他功能正在加入中。本软件可以免费使用以及分发,版权归batfree@yeah.net所有,可以在LGPL许可证下分发使用。\n 如果你有什么建议或者是需要加入什么功能,请到给我发email或者是到batfree.blogbus.com给我留言,更新的版本或者是相关内容也可以在http://batfree.blogbus.com查看。 \n谢谢使用!!!"; private int[] backColor = { 0, 0, 0}; //背景颜色数组,分别为Red,Green,Blue private int x, y; //////////字符的起始显示地址 private int flag = 0; //标志位,如果为0,则为滚动,1为暂停,2为停止 private int speed = 1; //图象的滚动速度,数字越大,滚动越快 private char[] content = null; ///显示内容的字符串数组。 private String[] strArr = null; public ScrollCanvas() { y = this.getHeight(); content = string.toCharArray(); } /** * ScrollCanvas * 构造函数,以String数组为参数,显示时将会显示在屏幕中间,按顺序分行显示。数组中的每个String占一行。 * * @param stringArray String[] s * 要显示的字符串数组。 */ public ScrollCanvas(String[] stringArray) { y = this.getHeight(); strArr = stringArray; } /** * ScrollCanvas * 带参数的构造函数,s为想要显示的字符串。 * * @param s String * 想要显示的滚动文字,通过\n实现换行。 */ public ScrollCanvas(String s) { y = this.getHeight(); content = s.toCharArray(); } /** * setString 设置要显示的字符串。 * * @param s String s 要显示的字符串 */ public void setString(String s) { this.content = s.toCharArray(); } public void paint(Graphics g) { int ay = 0; //y相对位置 if (backgroundIMG != null) { g.drawImage(backgroundIMG, 0, 0, g.TOP | g.LEFT); } else { g.setColor(backColor[0], backColor[1], backColor[2]); //将屏幕清成黑色 g.fillRect(0, 0, this.getWidth(), this.getHeight()); } g.setColor(forColor[0], forColor[1], forColor[2]); int i = 0; int strbegain = 0; x = 0; if (this.font != null) { g.setFont(font); } int fontHeight = font.getHeight(); if (strArr != null) { for (int line = 0; line < strArr.length; line++) { g.drawString(strArr[line], (this.getWidth() - font.stringWidth(strArr[line])) / 2, y + ay, g.TOP | g.LEFT); ay += font.getHeight() + 3; } } else { while (i < content.length) { while ( (x + font.charWidth(content[i])) < this.getWidth()) { x += font.charWidth(content[i]); if (i >= content.length - 1 || content[i] == '\n') { //到达最后一行或者是遇到\n换行符 i++; break; } i++; } x = 0; g.drawChars(content, strbegain, i - 1 - strbegain, 0, y + ay, g.TOP | g.LEFT); strbegain = i - 1; ay += fontHeight + 3; } } } public void run() { while (true) { if (flag == 2) { break; } synchronized (this) { if (flag == 1) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } long start = System.currentTimeMillis(); y = y - speed; this.repaint(); this.serviceRepaints(); long finish = System.currentTimeMillis(); if ( (finish - start) < 50) { try { Thread.sleep(50 - (finish - start)); } catch (Exception e) {} } System.out.println("" + (System.currentTimeMillis() - start)); } System.out.println("Finished the run"); } /////////////////设置背景颜色///////////////////////////// /** * setBackground * 设置背景颜色,如果不设置,默认为黑,即r=0,g=0,b=0 * * @param r int 红色,数值为0-255 * @param g int 绿色 数值为0-255 * @param b int 蓝色,数值为0-255 */ public void setBackground(int r, int g, int b) { backColor[0] = r; backColor[1] = g; backColor[2] = b; } private int[] forColor = { 255, 255, 255}; public void setForColor(int r, int g, int b) { forColor[0] = r; forColor[1] = g; forColor[2] = b; } ///////////////////////////////设置背景图象/////////////////////////// public void setBackground(Image image) { backgroundIMG = image; } ////////////////暂停 /** * pause 暂停 */ public void pause() { flag = 1; synchronized (this) { this.notify(); } } public void goOn() { flag = 0; System.out.println("goon The news"); synchronized (this) { this.notify(); } } //////停止 /** *destroy 调用之后将会停止, */ public void destroy() { flag = 2; synchronized (this) { this.notify(); } } /** * setSpeed 设置滚动速度,一般为大于零 * 的整数,数值越大,滚动速度越快。 * * @param speed int */ public void setSpeed(int speed) { this.speed = speed; } private Font font = Font.getDefaultFont(); //取得系统的字体 /** * setFont 设置显示的字体 * * @param face int 字体中的Face的参数 * @param style int style参数 * @param size int 字体的大小 */ public void setFont(int face, int style, int size) { //设置字体 this.font = Font.getFont(face, style, size); } private boolean keyboolean = false; //设置为按下一次按钮将暂停,再按下一次,将继续滚动。 public void keyPressed(int keycode) { keyboolean = !keyboolean; if (keyboolean) { this.pause(); } else { this.goOn(); } } }