📄 imagealbum.java
字号:
/*
* Filename: ImageAlbum.java
* - contains definition of the Main Class: ImageAlbum. It serves as the main object with static variables storing
* information about number of images in Album. It has methods for image display control - move forward/backward
* within Album and start slide show. The CommandListener receives command that contains in images-showing Canvas.
* The constructor read images/descriptions from phone storage and prepare them in memory for use when the MIDlet is
* launched.
*
*
* Application Name: Image Album Demo
* Version: 2.0
* Release Date: 27th September, 2002
* Author: Edmund Wong, Application Engineer, Metrowerks Hong Kong
*
* Image Album is a J2ME MIDlet demo program for viewing PNG format images on handsets, user can view
* images by either changing it one by one manually or in the form of slide show. The images can be
* packed in the J2ME JAR package before installing on phone or download from web server over-the-air (OTA).
* Users can also add their own descriptions to images.
*
* Please see ReadMe.txt for instructions on how to use Image Album Demo.
*
* (c) Copyright. 2002. Metrowerks Corp. ALL RIGHTS RESERVED.
* This code is provided "AS IS" without warranty of any kind and subject to Metrowerks Sample Application
* End User License Agreement. NO EXTERNAL DISTRIBUTION OR COMMERCIALIZATION OF THE SOFTWARE IS PERMITTED
* EXCEPT BY WRITTEN AGREEMENT OF METROWERKS."
*
*/
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
//import javax.microedition.rms.*;
/**
* The Main Class - ImageAlbum
*/
public class ImageAlbum extends MIDlet implements CommandListener{
/**
* Define the static variable for storing the no. of image that the Album contains
* initialize to 0 first, will be stored with the actual no. after reading the PNG files from jar file and
* reading image data from RecrodStore
*/
public static int imageNumberInJAR = 0; // number of images in JAR package
public static int imageNumberInRecordStore = 0; // number of images in RecordStore
public static int totalImageNumber = 0; // total number of images in Album (i.e. # of images in JAR package + # of images in RMS)
/**
* Define a counter for counting image
*/
public static int imageShowCounter;
/**
* Define command for displayable
*/
// for image Canvas used in View Album function
Command nextImageCommand = new Command("Next",Command.SCREEN,0);
Command previousImageCommand = new Command("Previous Image",Command.SCREEN,1);
Command deleteImageCommand = new Command("Delete Image",Command.SCREEN,2);
Command editDescriptionCommand = new Command("Edit description",Command.SCREEN,1);
// for Edit Description Form
Command saveEditedDescriptionCommand = new Command("Save Description",Command.SCREEN,0);
Command backToViewAlbum = new Command("Back",Command.BACK,1);
// for image Canvas used in both View Album and Slide Show functions
Command pauseCommand = new Command("Pause",Command.STOP,0);
Command continueSlideShowCommand = new Command("Continue Slide Show",Command.SCREEN,0);
// static mainMenuCommand for various Displayables
public static Command mainMenuCommand = new Command("Main menu",Command.SCREEN,4);
// Text Field for edit description Form to edit description
TextField editDescriptionTextField;
byte [] byteArray;
/**
* Create Alert components for alerting user of reaching start/end of Album
*/
public Alert startOfAlbumAlert = new Alert("Oops!","Reach start of Album!",null,null);
public Alert endOfAlbumAlert = new Alert("Oops!","Reach end of Album!",null,null);
public Alert endSlideShowAlert = new Alert("Oops!","Reach end of Album.\nEnd of Slide Show!!",null,null);
/**
* Create an image and description vector, initial size set to 10, capacity increment is set to 3
*/
Vector imageVector = new Vector (10,3);
Vector descriptionsVector = new Vector (10,3);
/**
* Create a RMSOperations object for manipulating Record Store
*/
RMSOperations rmsOperations = new RMSOperations();
/**
* Create a Photo Canvas for View Album function
*/
public PhotoCanvas viewAlbumCanvas = new PhotoCanvas(null, " ", this);
/**
* Create a Photo Canvas for Slide Show function
*/
public PhotoCanvas slideShowCanvas = new PhotoCanvas(null, " ", this);
/**
* Define a variable for storing the time interval between slides (in Slide Show)
*/
int timerTime = 0;
/**
* Create a Timer for slide show
*/
Timer t = new Timer();
/**
* Create a TimerTask for slide show
*/
MyTimerTask slideTask;
/**
* Create a boolean variable for showing whether it is in a Slide Showing state
*/
public static boolean slideShowing;
/**
* Reference to the Display
*/
public static Display myDisplay;
/**
* Create form to display Help
*/
//public HelpDisplayForm helpDisplayForm = new HelpDisplayForm(this);
/**
* Create form to Input Options
*/
public OptionsInputForm optionsInputForm = new OptionsInputForm(this);
/**
* Create form to Input Options
*/
public DownloadFromURLForm downloadFromURLForm = new DownloadFromURLForm(this);
/**
* Create selectList as MainMenu to Input Options
*/
public MainMenu selectList;
/**
* Constructor of ImageAlbum
*/
public ImageAlbum() {
myDisplay = Display.getDisplay(this);
/*
try{
RecordStore.deleteRecordStore("ImageDB");
RecordStore.deleteRecordStore("DescriptionsDB");
}
catch (Exception e){
System.out.println("Exception: " + e.toString());
}*/
/**
* Set display time for Alert message
*/
startOfAlbumAlert.setTimeout(2000);
endOfAlbumAlert.setTimeout(2000);
endSlideShowAlert.setTimeout(2000);
/**
* Create Image from PNG files in JAR package
*/
int i=0;
while (i>=0) {
try {
imageVector.addElement(Image.createImage("/"+Integer.toString(i+1)+".png"));
} catch (Exception e) {
// when no more images can be loaded, set the current i to imageNumberInJAR, which is the current no. of images
// in JAR package
imageNumberInJAR = i;
// set i to -2, so that it will be a negative no. during testing i's value and quit the while loop.
i = -2;
}
i++;
}
/**
* Create Image from bytes array in ImageDB
*/
byte [] byteArray = null;
// open the RecordStore "ImageDB"
rmsOperations.openRecordStore("ImageDB", true);
// get number of records in Image Record Store
imageNumberInRecordStore = rmsOperations.getNumberOfRecords();
// total no. of images = no. of images in JAR package + no. of images in RecordStore
totalImageNumber = imageNumberInJAR + imageNumberInRecordStore;
System.out.println("total Image number:" + totalImageNumber+" no. of Image in JAR:"+ imageNumberInJAR);
if (imageNumberInRecordStore > 0) {
for (i = 1; i <= imageNumberInRecordStore; i++){
//byteArray = rmsOperations.getRecord(rmsOperations.getPreviousRecordID()); // for SEMC phone
byteArray = rmsOperations.getRecord(rmsOperations.getNextRecordID()); // for Motorola phone
imageVector.addElement(Image.createImage(byteArray, 0, byteArray.length));
System.out.println("current Image # in vector " + imageVector.size());
}
}
// close RecordStore "ImageDB"
rmsOperations.closeRecordStore();
byteArray = null;
/**
* Create and store default descriptions for the image resided in the Jar package during the first run after installation
*/
// open the RecordStore "DescriptionsDB"
rmsOperations.openRecordStore("DescriptionsDB", true);
// create descriptions for images in JAR for first run after installation, and storing into descriptions vector
if (rmsOperations.getNumberOfRecords() == 0) {
System.out.println("first run");
for (i=1; i <= imageNumberInJAR; i++){
// convert string into byte data
try{
byteArray = StringByteOperations.stringToByteConvertion("JAR Package - "+ i +".png");
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
// write the byte array of the string into RecordStore "DescriptionsDB"
System.out.println("index:" + i + " Add Des DB recID:" + rmsOperations.addRecord(byteArray));
// write default description string into descriptionsVector making it ready for display use
descriptionsVector.addElement(new String("JAR Package - "+ i +".png"));
}
}
else { // not first run after installation, read all image descriptions from RecordStore "DescriptionsDB"
// read all image descriptions from RecordStore "DescriptionsDB" and store into descriptionsVector
for (i = 1; i <= totalImageNumber; i++) {
//byteArray = rmsOperations.getRecord(rmsOperations.getPreviousRecordID()); // for SEMC phone
byteArray = rmsOperations.getRecord(rmsOperations.getNextRecordID()); // for Motorola phone
try {
descriptionsVector.addElement(StringByteOperations.byteToStringConvertion(byteArray));
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
}
// close RecordStore DescriptionsDB
rmsOperations.closeRecordStore();
}
/**
* Add command for the Canvas used in viewing Album manually (viewAlbumCanvas)
*/
viewAlbumCanvas.addCommand(nextImageCommand);
viewAlbumCanvas.addCommand(previousImageCommand);
viewAlbumCanvas.addCommand(mainMenuCommand);
viewAlbumCanvas.addCommand(editDescriptionCommand);
viewAlbumCanvas.addCommand(deleteImageCommand);
viewAlbumCanvas.setCommandListener(this);
/**
* Add command for the Canvas used in viewing Album in Slide Show Mode (slideShowCanvas)
*/
slideShowCanvas.addCommand(nextImageCommand);
slideShowCanvas.addCommand(previousImageCommand);
slideShowCanvas.addCommand(pauseCommand);
slideShowCanvas.addCommand(continueSlideShowCommand);
slideShowCanvas.addCommand(mainMenuCommand);
slideShowCanvas.setCommandListener(this);
}
/**
* Starts the MIDlet
*/
protected void startApp() throws MIDletStateChangeException {
selectList = new MainMenu(this);
myDisplay.setCurrent(selectList);
}
/**
* Stop the MIDlet
*/
protected void pauseApp() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -