listcanvas.java

来自「手机在线系统 采用Java 中的J2ME, JSP 跟MySql 运行环」· Java 代码 · 共 241 行

JAVA
241
字号
/*
 * @(#)ListCanvas.java	1.11 01/08/23
 * Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
 * @version 	1.0, 10/05/2004
 * @author 	饶荣庆
 * @author 	余煜辉
 */
/**
 *此common包是连网的共有包,为所有连网提供统一的接口
 */
package com.j2me.common;

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

/*
 *此类是继承Canvas来实现List的功能
 */
public class ListCanvas extends Canvas
{

	/*-----------------------------------------------*/
	/*            字段属性                            */
	/*定义列表菜单标题*/
	private String[] menu;

	/*定义列表图标*/
	private Image[] icon;

	/*获得屏幕宽度*/
	private  int width = this.getWidth();

	/*获得屏幕高度*/
    private int height = this.getHeight();

	/*定义列表菜单的长度,即有多少个菜单*/
	private int length;

	/*定义列表菜单索引*/
	private int index;

	/*定义滚动新闻内容*/
	private String tickerString;

	private Image offscreen_buffer;
	
	

	/*-------------------------------------------------------*/
	/* 构造函数                                               */
	
	/*空构造函数*/
	public ListCanvas()
	{
		super();
		offscreen_buffer = Image.createImage(width, height);
	}

    /*
	 *构造一个低级列表
	 */
	public ListCanvas(String[] menu, Image[] icon)
		throws NullPointerException
	{
		super();
		offscreen_buffer = Image.createImage(width, height);
		if (menu == null)
		{
			throw new NullPointerException("菜单对象不能为空, 图标对象可以为空");
		}
		else if (icon != null)
		{	
			/*创建图标与菜单*/
			this.menu = new String[menu.length];
	     	this.icon = new Image[icon.length];
		    this.menu = menu;
		    this.icon = icon;
			/*长度附值*/
			this.length = menu.length;
			/*菜单的索引从0开始*/
			this.index = 0;
		}
		else
		{
			this.menu = new String[menu.length];
			this.menu = menu;
			/*长度附值*/
			this.length = menu.length;
			/*菜单的索引从0开始*/
			this.index = 0;
		}		
	}
    /*-----------------------------------------------------------------*/


	/*-------------------------------------------------*/
	/*   方法                                       */

	/*获得图形对象*/
	protected Graphics getGraphics()
    {
        return offscreen_buffer.getGraphics();
    }

	/*获得菜单长度*/
	public int getLength()
	{
		return length;
	}

	/*获得索引*/
	public int getSelectIndex()
	{
		return index;
	}

	/*
	 *检测列表有没有图标
	 */
	public boolean checkIcon()
	{
		if (icon == null)
		{
			return true;
		}
		else 
			return false;
	}

	/*
	 *检测列表有没有标题内容
	 */
	public boolean checkMenu()
	{
		if (menu == null)
		{
			return true;
		}
		else
			return false;
	}

	/*
	 *在画布上绘制图标
	 */
	public void drawIcon(Graphics g)
	{	
		/*设置字体颜色*/
		//g.setColor(255);
		if (this.icon != null )
		{	 
			for (int i = 0; i < icon.length; i++ )
			{
				g.drawImage(icon[i], 0, 20 + 12 * i, Graphics.HCENTER | Graphics.VCENTER);
			}
		}
		
	}

	/*
	 *在画布上绘制标题内容
	 */
	public void drawMenu(Graphics g)
	{
		/*设置字体颜色*/
		g.setColor(255);
		if (icon != null)
		{	
			for (int i = 0; i < menu.length; i++ )
			{
				g.drawString(menu[i], 12 + 5, 20 + 12 * i, Graphics.LEFT | Graphics.TOP);
			}
		}
		else
		{
			for (int i = 0; i < menu.length; i++ )
			{
				g.drawString(menu[i], 0, 20 + 12 * i, Graphics.LEFT | Graphics.TOP);
			}
		}
	}

	/*增加菜单与图标*/
	public void append(String menu, Image icon)
	{
		Graphics g = this.getGraphics();
		this.append(g, menu, icon);
	}


	/*增加菜单与图标*/
	public void append(Graphics g, String menu, Image icon)
	{
		if (icon != null)
		{ 
			if (this.menu != null && this.icon != null)
			{
				g.drawImage(icon, 0, 20 + 12 * this.length, Graphics.HCENTER | Graphics.VCENTER);
				g.drawString(menu, 12 + 5, 20 + 12 * this.length, Graphics.LEFT | Graphics.TOP);
				
				/*增加长度*/
				this.length++;
			}		    
		}
		else
		{
			g.drawString(menu, 0 + 5, 20 + 12 * this.length, Graphics.LEFT | Graphics.TOP);
			
			/*增加长度*/
			this.length++;
		}
		//this.repaint();
	}

	/*在进度条上显示主要内容*/
	public void setTickerString(String s)
	{
		tickerString = s;
	}

	/*绘制覆盖文字的画面*/
	public void drawFull(Graphics g)
	{
		//绘制矩形,用于显示进度条
		g.setColor(255, 255, 255);
		g.fillRect(0, 20 + 12 * index, this.width, 12 + 12 * index);
		g.drawString(menu[0], 0 + 5, 20 + 0, Graphics.LEFT | Graphics.TOP);
	}

	public void paint(Graphics g)
	{
		g.setColor(0, 0, 0);	//设置底色为白色
        g.fillRect(0, 0, width, height); //填充整个屏幕
		drawIcon(g);
		drawMenu(g);
		drawFull(g);
	}
		 				
		
}		   

⌨️ 快捷键说明

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