⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selector.java

📁 sudoku j2me手机游戏主要有游戏主类和闪屏类菜单类和模型类等
💻 JAVA
字号:
/*
 *  Copyright, 2005, S.E.Morris, C.Speirs
 *
 *  This file is part of SudokuME.
 *
 *  SudokuME is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  SudokuME is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with SudokuME; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package sudoku;

import javax.microedition.lcdui.*;

// *********************************************************************
// A list display/selection class, with support for loading.
//
// A model is used to fetch data from a given source (ala Swing models)
// and a listener may be used to listen for action events (FIRE, GAME_A,
// GAME_B, GAME_C, GAME_D).
//
// The model contains a method called 'loaded'.  If this returns greater
// than zero, it is assumed the underlying data is still loading.  A
// thread is created to periodically refresh the display, and instead of
// a list, a countdown (based upon the value returned by 'loaded') is
// displayed.  This continues until 'loaded' reaches zero.  To use this
// facility, create a model for your data which returns a countdown (via
// 'loaded') to when the data is available.  Add the model to your
// choosen Selector, which will automatically kick off the refresh
// display.  Then, in a separate thread, load your data and ammend your
// countdown (your return from 'loaded') accordingly.
// *********************************************************************
class Selector
{	private SelectorListener listener;
	SelectorModel model;
	private Canvas ui;
	private int xPos,yPos,width,height;
	int index,offset;
	private int fh,lines;

	private Thread refreshThread;
	boolean quitFlag;

	Selector(SelectorListener l,Canvas c,int x,int y,int w,int h)
	{	listener=l;  ui=c;
		xPos=x;  yPos=y;  width=w;  height=h;
		fh=Font.getDefaultFont().getHeight();
		index=0;  offset=0;  lines=height/fh;
	}

	void reset()
	{	index=0;  offset=0;
	}

	// Sets a new model.  If the data begin this model is unloaded, creates
	// a thread to repeatedly repaint the display until complete.
	void setModel(SelectorModel m)
	{	model=m;  index=0;  offset=0;
		if(model==null)  return;
		if(model.loaded()>0)
		{	quitFlag=false;
			refreshThread = new Thread()
			{	public void run()
				{	while(model.loaded()>0 && !quitFlag)
					{	try { Thread.sleep(100); } catch(InterruptedException e) {}
						ui.repaint();
					}
				}
			};
			refreshThread.start();
		}
		ui.repaint();
	}

	public void paint(Graphics g)
	{	if(model==null)  return;

		int topLeft = Graphics.TOP|Graphics.LEFT;
		int centered = Graphics.TOP|Graphics.HCENTER;

		g.setClip(xPos,yPos,width,height);
		g.translate(xPos,yPos);

		int toGo = model.loaded();
		String err = model.loadingErrorMessage();
		if(toGo>0)	// Still loading?
		{	// -----Loading display
			g.setColor(0x00,0x00,0x00);
			g.drawString("Loading...",width/2,10,centered);
			g.drawString
			(	(toGo==Integer.MAX_VALUE) ? "Connecting" : "Countdown: "+toGo
				,width/2,30,centered
			);
		}
		else if(err!=null)	// Error when loading?
		{	// -----Display error message
			g.setColor(0x00,0x00,0x00);
			g.drawString("Error...",width/2,10,centered);
			int i=0,fh=Font.getDefaultFont().getHeight();
			while(err.length()>0)
			{	int max=Math.min(20,err.length());
				g.drawString(err.substring(0,max),0,30+i*fh,topLeft);
				err=err.substring(max);
				i++;
			}
		}
		else
		{	// -----List
			if(model.length()<=0)  return;
			int l = Math.min(lines,model.length());
			g.setColor(0xcc,0xcc,0xff);  g.fillRect(0,(index-offset)*fh,width,fh);
			g.setColor(0x00,0x00,0x00);
			for(int i=0;i<l;i++)
				g.drawString(model.itemAt(i+offset),0,i*fh,topLeft);
		}
	}

	public void keyPressed(int keyCode)
	{	if(model==null)  return;

		int l=model.length();
		if(l<=0)  return;

		switch(ui.getGameAction(keyCode))
		{	// -----Process up/down
			case Canvas.UP :  index=(index-1+l)%l;  break;
			case Canvas.DOWN :  index=(index+1)%l;  break;
			// -----'Action' buttons targeted towards listener
			default :
				listener.itemSelected(index,keyCode);  break;
		}
		if(index>=offset+lines)  offset=index-lines+1;
		else if(index<offset)  offset=index;
		//System.out.println("<"+index+" "+offset+" "+lines+" "+model.length());
		ui.repaint();
	}
}

⌨️ 快捷键说明

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