scrolltextmidlet.java.svn-base

来自「example2 众多JAVA实例源码...学习java基础的好帮手」· SVN-BASE 代码 · 共 155 行

SVN-BASE
155
字号
package opusmicro.demos.scroll2;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class ScrollTextMIDlet extends MIDlet {
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
	protected void pauseApp() {}
	protected void startApp() throws MIDletStateChangeException {
		ScrollTextCanvas stc = new ScrollTextCanvas();
		stc.arrangeText("this is my first message this is my first message this is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first messagethis is my first message");
		Display.getDisplay(this).setCurrent(stc);
	}
}

class ScrollTextCanvas extends Canvas {
	Font font;
	private static Graphics m_gfx;
	public static int m_menuState = 2;
	private int lineHeight, lineWidth, maxLines;
	private Vector strText = new Vector();
	public static int topLine = 0;
	public boolean topLinePresent = false;
	int kk;
	private int canWidth;
	private int canHeight;

	public ScrollTextCanvas(){
		canWidth = getWidth();
		canHeight = getHeight();
		font = Font.getDefaultFont();
	}
	public void paint(Graphics g) {
		String shortText;
		m_gfx = g;

//		font = g.getFont();
//		canWidth = getWidth();
//		canHeight = getHeight();

		switch ( m_menuState) {
			case 0:
				drawMenu();
				break;
			case 1:
				drawSubMenu();
				break;
			case 2:
				addText();
				break;

		}
	} // paint

	private void drawSubMenu() {
		
	}

	private void drawMenu() {
		
	}

	public void addText() {
		if ( topLinePresent) {
			m_gfx.setColor(0, 0, 0);
			m_gfx.fillRect(0, 0, canWidth, canHeight);
			m_gfx.setColor(255, 255, 255);
			for ( int k = 0 ; k < maxLines && k < topLine ; k++) {
				m_gfx.drawString((String) strText.elementAt(kk + k), canWidth / 2, (lineHeight * k + 15),
						(Graphics.TOP | Graphics.HCENTER));
				topLine--;
			}
		}
		else {
			m_gfx.setColor(0, 0, 0);
			m_gfx.fillRect(0, 0, canWidth, canHeight);
			m_gfx.setColor(255, 255, 255);

			for ( kk = 0 ; kk < maxLines && kk < strText.size() ; kk++) {
				m_gfx.drawString((String) strText.elementAt(kk), canWidth / 2, (lineHeight * kk + 15),
						(Graphics.TOP | Graphics.HCENTER));
				if ( topLine != 0) topLine--;
			}
		}
		strText.removeAllElements();
	}

	public void arrangeText(String text) {
//System.out.println("1...");
		lineWidth = canWidth - 35;
		lineHeight = font.getHeight() + 1;
//System.out.println("2...");
		maxLines = (canHeight / lineHeight) - 2;
//System.out.println("3...");
		if ( font.stringWidth(text) > lineWidth) {
			// seperate text in to multiple lines and
			int offset = 0;
			int len = 1;
			do {
				// calculate the max lenth of substring fitting one screen line

				while ( ((offset + len) <= text.length()) && (font.substringWidth(text, offset, len) <= lineWidth)) {
					len++;
				}
				strText.addElement(text.substring(offset, offset + len - 1));
				offset += len - 1;
				len = 1;
			}
			while ( offset + len <= text.length());
		}// if
		else {
			strText.addElement(text);
		}
		if ( strText.size() > maxLines) {
			topLine = strText.size() - maxLines;
		}
	}

	/*******************************************************************************************************************
	 * keyPressed()
	 ******************************************************************************************************************/
	public void keyPressed(int key){
		int action = getGameAction(key);
		switch ( action) {
			case DOWN:
				if ( m_menuState == 2) {
					if ( topLine != 0) {
						topLinePresent = true;
					}
					else {
						topLinePresent = false;
					}
					repaint();
					break;
				}
			case UP:
				if ( m_menuState == 2) {
					topLinePresent = false;
				}
				repaint();
				break;
			default:
				break;

		}// switch
	}// keyPressed
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?