j 2 me縦スクロールメニューの基本

1794 ワード


public class AppCanvas extends Canvas {

	int screen_width;
	int screen_height;
	String[] select;
	public final static int KEY_UP = -1;
	public final static int KEY_DOWN = -2;

	public AppCanvas() {
		this.setFullScreenMode(true);
		screen_width = this.getWidth();
		screen_height = this.getHeight();
		select = new String[] { "      ", "      ", " ", " ", " ",
				" ", " ", " ", " " };
	}

	private int itemsOnSceen = 5;// , 
	private int index = 0;//select 
	private int delta = 0;//index itemsOnSceen 

	protected void paint(Graphics g) {
		// TODO Auto-generated method stub

		g.setColor(0xffffff);
		g.fillRect(0, 0, screen_width, screen_height);
		
		for (int i = 0; i < ((itemsOnSceen>select.length)?select.length:itemsOnSceen); i++) {
			if (i == index - delta) {
				g.setColor(0xff0000);
			} else {
				g.setColor(0);
			}
			g.drawString(select[i + delta], screen_width >> 1, 40 + 20 * i,
					Graphics.BASELINE | Graphics.HCENTER);
		}
		
	}

	protected void keyPressed(int keyCode) {
		// TODO Auto-generated method stub
		switch (keyCode) {

		case KEY_UP:
			if (index > 0) {
				index--;
			}
			if (index - delta < 0) { // 
				delta--;
			}
			break;
		case KEY_DOWN:
			if (index < select.length - 1) {
				index++;
			}
			if (index - delta > itemsOnSceen - 1) { // 
				delta++;
			}
			break;
		}
		repaint();

	}
}