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

📄 menu.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 simple menu system.
//
// Menus are constructed from two 2D arrays, one with strings to display,
// and the other with instructions to follow when an option is selected.
// A positive number will be passed to the MenuListener, while a negative
// number will cause a jump to another menu.
//
// For example...
//     String[][] optStr =
//     {   { "Option 1","Option 2","Exit" },
//         { "Submenu option 1","Submenu option 2" }
//     };
//     int[][] optInt =
//     {   { -1,-1,0 },
//         { 100,200 }
//     };
//
// In the above the top level menu will show "Option 1" (associated with
// the value -1). "Option 2" (-1 also) and "Exit" (0).  If either option
// zero or one are selected, we check the associated value, which is -1,
// negative --- so we now jump to menu (index) 1.  Alternatively, If 
// option two was selected, associated with zero, we would return zero
// via the listener callback.
// Menu index 1 is a submenu, with values 100 and 200 associated with
// each of its two options.  As both of these are positive, they will
// be passed via the listener, rather than triggering yet another menu.
// *********************************************************************
class Menu extends Canvas implements CommandListener
{	private Display display;				// Screen display
	private String[][] optionStr;			// Option texts
	private int[][] optionInt;				// Option values (returns or jumps)
	private MenuListener listener;			// Menu listener, singleton
	private int currentMenu,currentOption;	// Current menu/option
	private int itemW,itemH,x,y;			// List item size, menu position
	private Displayable previous;			// Previous display component
	private Command okayCommand;			// Select menu
	private Command cancelCommand;			// Cancel menu
		
	// -----------------------------------------------------------------
	// CONSTRUCTORS
	// -----------------------------------------------------------------
	Menu(Display d,MenuListener ml,String[][] opts1,int[][] opts2,int xPos,int yPos)
	{	display=d;  listener=ml;  
		optionStr=opts1;  optionInt=opts2;
	
		previous=d.getCurrent();			// Remember previous menu	
		initMenu(0 , xPos,yPos);			// Init top level menu		
		display.setCurrent(this);			// Set display to us
	
		okayCommand = new Command("Okay",Command.OK,1);
		cancelCommand = new Command("Cancel",Command.CANCEL,1);
		this.addCommand(cancelCommand);  this.addCommand(okayCommand);
		this.setCommandListener(this);
	}
	Menu(Display d,MenuListener ml,String[][] opts1,int[][] opts2)
	{	this(d,ml,opts1,opts2,0,0);
	}
	
	// -----------------------------------------------------------------
	// Initialise a new menu
	// -----------------------------------------------------------------
	private void initMenu(int m , int xPos,int yPos)
	{	currentMenu=m;  currentOption=optionStr[currentMenu].length-1;
		x=xPos;  y=yPos;
		
		String[] sa=optionStr[currentMenu];
		
		// -----Find dimensions to fit menu
		Font f=Font.getDefaultFont();
		itemW=0;  itemH=f.getHeight();
		for(int i=0;i<sa.length;i++)
		{	int a=f.stringWidth(sa[i]);
			itemW = (a>itemW) ? a : itemW ;
		}
		
		// -----Centre if negative
		int scrW=getWidth() , scrH=getHeight();
		int menuW=itemW+4 , menuH=itemH*optionStr[currentMenu].length+4;
		if(x<0)  x=(scrW-menuW)/2;
		if(y<0)  y=(scrH-menuH)/2;
		// -----Normalise
		if(x+menuW > scrW)  x=scrW-menuW;
		if(y+menuH > scrH)  y=scrH-menuH;
		
		repaint();
	}
	
	// -----------------------------------------------------------------
	// Display update
	// -----------------------------------------------------------------
	public void paint(Graphics g)
	{	String[] sa=optionStr[currentMenu];
		
		g.translate(x,y);
		g.setFont(Font.getDefaultFont());
	
		int w=itemW+4 , h=itemH*sa.length+4;

		// -----Background
		g.setColor(0xff,0xff,0xff);  g.fillRect(0,0 , w,h);
		g.setColor(0xcc,0xcc,0xff);  g.drawRect(1,1 , w-2,h-2);
		g.setColor(0x00,0x00,0x00);  g.drawRect(0,0 , w,h);
		g.fillRect(4,h,w,4);  g.fillRect(w,4,4,h);
		// -----Selection
		g.setColor(0xcc,0xcc,0xff);
		g.fillRect(2,currentOption*itemH+2 , itemW+1,itemH+1);		
		// -----Draw strings
		g.setColor(0x00,0x00,0x00);
		for(int i=0;i<sa.length;i++)
			g.drawString(sa[i] , 2,i*itemH+2 , Graphics.TOP|Graphics.LEFT);
	}
	
	// -----------------------------------------------------------------
	// Key press handler
	// -----------------------------------------------------------------
	public void keyPressed(int keyCode)
	{	String[] sa=optionStr[currentMenu];
		switch(getGameAction(keyCode))
		{	case UP : 		currentOption=(currentOption-1+sa.length)%sa.length;  break;
			case DOWN :		currentOption=(currentOption+1)%sa.length;  break;
			case FIRE :		_selection();  return;
		}
		repaint(x,y,itemW+4,itemH*sa.length+4);
	}		
	public void keyRepeated(int keyCode)
	{	switch(getGameAction(keyCode))
		{	case UP : 
			case DOWN : 
				keyPressed(keyCode);
				break;
		}
	}
	
	// -----------------------------------------------------------------
	// Soft key handler
	// -----------------------------------------------------------------
	public void commandAction(Command c,Displayable d)
	{	if(c==cancelCommand)
		{	display.setCurrent(previous);
			listener.menuSelection(0);
		}
		else if(c==okayCommand)
		{	_selection();
		}
	}
	
	private void _selection()
	{	int opt = optionInt[currentMenu][currentOption];
		if(opt<0)
		{	initMenu(Math.abs(opt) , x+itemW,y+itemH*currentOption);
		}
		else  
		{	display.setCurrent(previous);
			listener.menuSelection(opt);
		}
	}		
}

⌨️ 快捷键说明

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