browsephoto.java

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

JAVA
197
字号
 /**
  * @(#)BrowsePoto.java	
  * Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
  * @version 	1.0, 10/05/2004
  * @author 	饶荣庆
  * @author 	
  */
package com.j2me.myphoto;

import java.util.*;
import com.j2me.language.*;
import javax.microedition.lcdui.*;

/**
 *此类是用来顺序浏览相片的类
 */
public class BrowsePhoto extends Canvas implements CommandListener
{
	private Display display = null;
	private PhotoBookMenu photoBookMenu = null;
	private Command backCommand = null;			//定义离开软键
	private Command infoCommand = null;
	private Command browseNextCommand = null;
	private Command browseBackCommand = null;
	private PhotoBook pb = null;
	private DetailInfoForm detailInfoForm = null;
	private Vector vectorPhotos = null;	  //定义获得全部相片的对象
	private Enumeration enumerationVector;	 //列举所有相片
	private Photo photo;
	private int sortBy;		  //指定排序方式获得对象
	private int [] photoID;		//获得id
	private String [] photoName = null ;
	private String [] photoPath = null;	 //获得相片详细信息
	private boolean isNext = true;
	private int next = 0;	 //定义顺序浏览的张数;
	private int num;	  //定义获得相片总数
	private int i = 1;  //定义第几张


	public BrowsePhoto(int _sortBy)	   //根据指定的排序方式,获得Friend对象 ,0为名,1为id
	{
		this.sortBy = _sortBy;  //以指定方式来列表。0为相片名,1为id
		this.infoCommand = new Command("详细信息", Command.ITEM, 1);	
		this.backCommand = new Command("返回", Command.OK, 2);
		this.browseNextCommand = new Command("下一张", Command.BACK, 1);
		this.addCommand(backCommand);
		this.addCommand(browseNextCommand);
		this.addCommand(infoCommand);
		this.setPhotoInfo();
		this.setCommandListener(this);	 
	}

	public void showForm(Display display, PhotoBookMenu photoBookMenu)	//用来显示界面
	{
		this.display = display;
	   	this.photoBookMenu = photoBookMenu;
		this.display.setCurrent(this);
	}

	public void setPhotoInfo()
	{
		int i = 0;  //用于记录几笔信息
		this.pb = new PhotoBook();
		this.vectorPhotos = pb.getAllPhotos (sortBy);  //根据指定的排序方式,获得Photo对象
		if (vectorPhotos.isEmpty())
		{
			this.removeCommand(infoCommand);
		}
		this.num = vectorPhotos.size();
		this.photoID = new int[num];
		this.photoName = new String[num];
		this.photoPath = new String[num];
		this.enumerationVector = vectorPhotos.elements();
		this.pb.closeRS();
		while (enumerationVector.hasMoreElements())	    //把存储的对象一个个读出来并保存在数组中
		{
			this.photo = (Photo)enumerationVector.nextElement();
			this.photoID[i] = photo.getID();		  //读取相册信息
			this.photoName[i] = photo.getName();
			this.photoPath[i] = photo.getPath();
			i++;
		}
		//System.out.println(photoPath[next] );
	} 

	public void paint(Graphics g)		  //绘制图象
	{
		Image image = null;
		g.setColor(255, 255, 255);					   //设置底色为白色
		g.fillRect(0, 0, this.getWidth(), getHeight());				 //填充为白色
		try
		{
			image = Image.createImage("/myphoto/" + photoPath[next] + ".png");
		}
		catch(Exception e)		 //为什么抓不住这个异常呢?
		{
			g.setColor(255, 0, 0);
			g.drawString(e.toString(), 0, this.getHeight()/2,  g.TOP|g.LEFT);
		}
		g.setColor(255, 0, 0);
		g.drawString("第" + i + "张",  0,  0, g.TOP|g.LEFT);
		try
		{
			g.drawImage(image, this.getWidth()/2,  this.getHeight()/2, g.VCENTER|g.HCENTER);  
		}
		catch(NullPointerException e)
		{
			if (vectorPhotos.isEmpty())
			{
				this.removeCommand(infoCommand);
			}
			g.setColor(255, 0, 0);
			g.drawString(e.toString(), 0, this.getHeight()/2 - 20,  g.TOP|g.LEFT);
		}
	}

	public void commandAction(Command c,Displayable s)
	{
		if(c == backCommand)
		{
			this.display.setCurrent(photoBookMenu);
		}

		if (c == infoCommand)
		{
			if (Language.isEnglish == true)  //修改语言为英文
			{				
				this.detailInfoForm = new DetailInfoForm("Detail Info");		//new一个详细信息窗口
				this.detailInfoForm.idItem.setLabel("ID:");
				this.detailInfoForm.nameItem.setLabel("Name:");
				this.detailInfoForm.pathItem.setLabel("Path:");
				this.detailInfoForm.removeCommand(this.detailInfoForm.openCommand);	   //删除软键打开相片
			}
			else										
			{
				this.detailInfoForm = new DetailInfoForm("详细信息");
				this.detailInfoForm.idItem.setLabel("ID:");
				this.detailInfoForm.nameItem.setLabel("图片名:");
				this.detailInfoForm.pathItem.setLabel("路径:");
				this.detailInfoForm.removeCommand(this.detailInfoForm.openCommand);
			}		
			
			if (photoID[next] <10)
				{
					this.detailInfoForm.showForm(display, null, photoBookMenu, this, "00" + photoID[next]);
				}
				else if (photoID[next] <100)
				{
					this.detailInfoForm.showForm(display, null, photoBookMenu, this, "0" + photoID[next]);
				}
				else
				{
					this.detailInfoForm.showForm(display, null, photoBookMenu, this, photoID[next] + "");
				}
		}

		if (c == browseNextCommand)
		{
		    if (next >= 0 && next < num && isNext == true)    //判断是否是下一张
			{
				this.next++;
				this.i++;
				this.repaint();
				if (this.next == num-1)	  //超出相片总量时,重新看
				{
					isNext = false;
					browseBackCommand =  new Command("上一张", Command.BACK, 1);
				    this.addCommand(browseBackCommand);
				    this.removeCommand(browseNextCommand);
				    this.setCommandListener(this);
				}
			}

		}

		if (c == browseBackCommand)
		{
			if (next >= num-1 || isNext == false)  //判断时候是上一张
			{	
				this.next--;
				this.i--;
			    this.repaint();
			}
							
			if (next == 0)	   //next=0时表示相册已经是第一张了,所以重新浏览
			{
				this.browseNextCommand = new Command("下一张", Command.BACK, 1);
				this.addCommand(browseNextCommand);
				this.removeCommand(browseBackCommand);
				this.setCommandListener(this);
				this.isNext = true;
			}				
		}
	}
}

⌨️ 快捷键说明

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