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

📄 griditem.java

📁 PDA餐饮管理系统,在掌上电脑实现的,可以开台,点菜等多功能模块
💻 JAVA
字号:
package item;

import javax.microedition.lcdui.*;
import java.util.*;

/**
 * 显示成表格形式的抽象CustomItem
 * @author hong
 * 
 */
abstract class GridItem extends CustomItem {
	protected int dx = 80;
	protected int dy = 20;
	protected int bottomHeight=20;
	protected int rows;
	protected int cols;

	private final static int UPPER = 0;
	private final static int IN = 1;
	private final static int LOWER = 2;
	private int location = UPPER;
	protected int curX = 0;
	protected int curY = 0;
	protected int curPage=0;
	protected int contentNum;
    protected int pages;

    protected Object cur_content[][];
    protected Vector all_content=new Vector();

	private boolean horz;
	private boolean vert;
	
	//绘制每一格内容
	abstract protected void drawContent(Graphics g,int x,int y);
	//绘制底部说明
	abstract void drawBottom(Graphics g);
	
	/**
	 * 
	 * @param title 说明
	 * @param itemWidth 可用的宽度
	 * @param itemHeight 可用的高度
	 * @param dx 每个项目的宽度
	 * @param dy 每个项目的高度
	 * @param bottomHeight 底部说明内容的高度
	 */
	public GridItem(String title,int itemWidth,int itemHeight,
			int dx,int dy,int bottomHeight) {
		super(title);
		this.setLayout(Item.LAYOUT_NEWLINE_BEFORE);

		int interactionMode = this.getInteractionModes();
		horz = ((interactionMode & CustomItem.TRAVERSE_HORIZONTAL) != 0);
		vert = ((interactionMode & CustomItem.TRAVERSE_VERTICAL) != 0);
	
		this.dx=dx;
		this.dy=dy;
		this.bottomHeight=bottomHeight;
		
		cols=itemWidth / dx;
		rows=(itemHeight-bottomHeight) / dy;
		
		cur_content=new Object[cols][rows];
	}
	
    //初始化当前显示内容
	private void initShowMenu() {
		int m = rows * cols * curPage;
		
		for (int j = 0; j < rows; j++)
			for (int i = 0; i < cols; i++) {
				if (m < contentNum) 
					cur_content[i][j] = all_content.elementAt(m++);
				else
					cur_content[i][j] = null;
			}
	}
	
	protected int getMinContentHeight() {
		return dy+bottomHeight;
	}

	protected int getMinContentWidth() {
		return 160;
	}

	protected int getPrefContentHeight(int width) {
		return (rows * dy) + 1+bottomHeight;
	}

	protected int getPrefContentWidth(int height) {
		return (cols * dx) + 1;
	}

	protected void paint(Graphics g, int w, int h) {
		for (int i = 0; i <= rows; i++) {
			g.drawLine(0, i * dy, cols * dx, i * dy);
		}

		for (int i = 0; i <= cols; i++) {
			g.drawLine(i * dx, 0, i * dx, rows * dy);
		}

		int oldColor = g.getColor();
		g.setColor(0x00D0D0D0);
		g.fillRect(curX * dx + 1, curY * dy + 1, dx - 1, dy - 1);
		g.setColor(oldColor);

		for (int j = 0; j < rows; j++)
			for (int i = 0; i < cols; i++) {
				int oldclipX = g.getClipX();
				int oldClipY = g.getClipY();
				int oldClipWidth = g.getClipWidth();
				int oldClipHeight = g.getClipHeight();
				g.setClip((i * dx) + 1, j * dy, dx - 1, dy );
				drawContent(g, i, j);
				g.setClip(oldclipX, oldClipY, oldClipWidth, oldClipHeight);
			}
		
		drawBottom(g);
	}
	
	protected void keyPressed(int keycode) {
		switch (keycode){
		case Canvas.KEY_NUM1://前翻页
			if (curPage>0){
				curPage--;
				initShowMenu();
				repaint();
				notifyStateChanged();
			}
			break;
		case Canvas.KEY_NUM3://后翻页
			if (curPage<pages-1){
				curPage++;
				initShowMenu();
				repaint();
				notifyStateChanged();
			}
			break;
		}
	}
	
	protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
			int[] visRect_inout) {
		boolean result=true;
		if (horz && vert) {
			switch (dir) {
			case Canvas.DOWN:
				if (location == UPPER) {
					location = IN;
				} else {
					if (curY < (rows - 1)) {
						curY++;
						repaint(curX * dx, (curY - 1) * dy, dx, dy);
						repaint(curX * dx, curY * dy, dx, dy);
						repaintBottom();
						notifyStateChanged();
					} else {
						location = LOWER;
						return false;
					}
				}
				break;
			case Canvas.UP:
				if (location == LOWER) {
					location = IN;
				} else {
					if (curY > 0) {
						curY--;
						repaint(curX * dx, (curY + 1) * dy, dx, dy);
						repaint(curX * dx, curY * dy, dx, dy);
						repaintBottom();
						notifyStateChanged();
					} else {
						location = UPPER;
						return false;
					}
				}
				break;
			case Canvas.LEFT:
				if (curX > 0) {
					curX--;
					repaint((curX + 1) * dx, curY * dy, dx, dy);
					repaint(curX * dx, curY * dy, dx, dy);
					repaintBottom();
					notifyStateChanged();
				}
				break;
			case Canvas.RIGHT:
				if (curX < (cols - 1)) {
					curX++;
					repaint((curX - 1) * dx, curY * dy, dx, dy);
					repaint(curX * dx, curY * dy, dx, dy);
					repaintBottom();
					notifyStateChanged();
				}
			}
		} else if (horz || vert) {
			switch (dir) {
			case Canvas.UP:
			case Canvas.LEFT:
				if (location == LOWER) {
					location = IN;
				} else {
					if (curX > 0) {
						curX--;
						repaint((curX + 1) * dx, curY * dy, dx, dy);
						repaint(curX * dx, curY * dy, dx, dy);
						repaintBottom();
						notifyStateChanged();
					} else if (curY > 0) {
						curY--;
						repaint(curX * dx, (curY + 1) * dy, dx, dy);
						curX = cols - 1;
						repaint(curX * dx, curY * dy, dx, dy);
						repaintBottom();
						notifyStateChanged();
					} else {
						location = UPPER;
						return false;
					}
				}
				break;
			case Canvas.DOWN:
			case Canvas.RIGHT:
				if (location == UPPER) {
					location = IN;
				} else {
					if (curX < (cols - 1)) {
						curX++;
						repaint((curX - 1) * dx, curY * dy, dx, dy);
						repaint(curX * dx, curY * dy, dx, dy);
						repaintBottom();
						notifyStateChanged();
					} else if (curY < (rows - 1)) {
						curY++;
						repaint(curX * dx, (curY - 1) * dy, dx, dy);
						curX = 0;
						repaint(curX * dx, curY * dy, dx, dy);
						repaintBottom();
						notifyStateChanged();
					} else {
						location = LOWER;
						return false;
					}
				}
			}
		} else {

		}

		visRect_inout[0] = curX;
		visRect_inout[1] = curY;
		visRect_inout[2] = dx;
		visRect_inout[3] = dy;
		return result;
	}

	/**
	 * 初始化内容
	 * @param content 菜品数组
	 */
	public void setContent(Vector content) {
		all_content.removeAllElements();
		if (content == null) {
			contentNum = 0;
			pages = 0;

		} else {
			for (int i = 0; i < content.size(); i++)
				all_content.addElement(content.elementAt(i));

			contentNum = content.size();
			pages = contentNum / (rows * cols);
			if (contentNum % (rows * cols) != 0)
				pages++;
		}

		curPage = 0;
		curX = 0;
		curY = 0;

		initShowMenu();
		repaint();
	}

	private void repaintBottom(){
		repaint(0,rows*dy,cols*dx,bottomHeight);
	}
	
	/**
	 * 获取所有包含的内容
	 * @return 所有包含的内容
	 */
	public Vector getContent(){
		return all_content;
	}
	
	/**
	 * 增加一条内容
	 * @param aObject
	 */
	public void addContent(Object aObject){
		if (aObject==null)
			return;
		
		if (contentNum%(rows*cols)==0)
			pages++;
		all_content.addElement(aObject);
		contentNum++;
		curPage=pages-1;
		initShowMenu();
		repaint();
	}
	
	/**
	 * 删除当前选择的内容
	 *
	 */
	public void delContent() {
		if (cur_content[curX][curY] != null) {
			if (contentNum == 1) {
				pages = 1;
			} else if (contentNum % (rows * cols) == 1) {
				pages--;
				if (curPage > pages - 1)
					curPage = pages - 1;
			}

			all_content.removeElementAt(rows * cols * curPage + curY * cols
					+ curX);
			contentNum--;
			initShowMenu();
			repaint();
		}
	}
	
	/**
	 * 获取选择的内容
	 * @return 选择的内容
	 */
	public Object getSelectContent(){
		return cur_content[curX][curY];
	}
	
	public int getContentNum() {
		return contentNum;
	}
}

⌨️ 快捷键说明

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