javaMe开发的相册

来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 15:49 点击:
 1.实体类 Photo类   
  代码
  public class Photo {
  //图片名称
  private String name;
  //原始图片
  private Image pic;
  //缩放以后的图片
  private Image resizePic;
  public Image getResizePic() {
  return resizePic;
  }
  public void setResizePic(Image resizePic) {
  this.resizePic = resizePic;
  }
  public Photo(String name, Image pic) {
  this.name = name;
  this.pic = pic;
  }
  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  public Image getPic() {
  return pic;
  }
  public void setPic(Image pic) {
  this.pic = pic;
  }
  }
  2.PhotoListForm类,显示图片列表的页面   
  代码
  public class PhotoListForm extends BaseForm {
  Photo[] photos;
  Hashtable photosHash = new Hashtable();
  public static int selectedIndex = -1;
  public PhotoListForm() {
  this.setScrollable(true);
  this.setLayout(new BorderLayout());
  //构造Photo对象,为了模拟,在实际中看你从哪个地方提取照片
  String[] picNames = new String[10];
  for (int i = 0; i < picNames.length; i++) {
  picNames[i] = "图片" + i;
  }
  Image[] pics = new Image[10];
  for (int i = 0; i < pics.length; i++) {
  try {
  pics[i] = Image.createImage("/pic/pic" + (i + 1) + ".jpg");
  } catch (IOException ex) {
  ex.printStackTrace();
  }
  }
  photos = new Photo[10];
  for (int i = 0; i < photos.length; i++) {
  photos[i] = new Photo(picNames[i], pics[i]);
  }
  //每行显示4列,计算行数
  int column = 4;
  int row = (pics.length % column == 0) ? (photos.length / column) : (photos.length / column + 1);
  //图片容器以GridLayout布局
  Container photoContainer = new Container(new GridLayout(row, column));
  for (int i = 0; i < 10; i++) {
  Button b = new Button();
  //Button的LeftMargin=2,RightMargin=2
  int margin = 4;
  //生成缩略图
  photos[i].setResizePic(resizeImage(photos[i].getPi c(), getDestW(margin), getDestW(margin)));
  b.setIcon(photos[i].getResizePic());
  b.setText(photos[i].getName());
  b.setAlignment(Label.CENTER);
  b.setTextPosition(Label.BOTTOM);
  b.setUIID("PhotoButton");
  photoContainer.addComponent(b);
  //int类型无法放入Hashtable,先转为Integer类型
  Integer index = new Integer(i);
  photosHash.put(b, index);
  b.addActionListener(new ButtonActionListener());
  //识别选中的图片,返回时,选中的图片仍然是这张被点击的图片
  if (selectedIndex == i) {
  this.setFocused(b);
  }
  }
  this.addComponent(BorderLayout.CENTER, photoContainer);
  }
  private class ButtonActionListener implements ActionListener {
  public void actionPerformed(ActionEvent evt) {
  int value = Integer.parseInt(photosHash.get((Button) evt.getSource()).toString());
  //标识选中的图片,返回时,选中的图片仍然是这张被点击的图片
  selectedIndex = value;
  new PhotoDetailForm(photos, value);
  }
  }
  //根据屏幕宽度,计算图片应该缩放的宽度
  public int getDestW(int margin) {
  return (Display.getInstance().getDisplayWidth() - 40) / 4;
  }
  //缩放图片
  public Image resizeImage(Image src, int destW, int destH) {
  return src.scaledSmallerRatio(destW, destH);
  }
  }
  3.PhotoDetailForm类,显示大图的页面
  
  代码
  import com.sun.lwuit.Button;
  import com.sun.lwuit.Command;
  import com.sun.lwuit.Component;
  import com.sun.lwuit.Container;
  imp

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Java编程语言的八大优点
      • JVM对象生命周期详细介绍
      • Java平台上的CRM系统
      • Java 算数测试小程序
      • Command(命令模式)
      • Java 一个简单的画图程序
      • Java环境 使用Resin在NT环境下配置JSP环境
      • Java 日历的小程序
      • Java 统计代码的小工具
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1