?? photoalbum.java
字號:
} /** * Check the attributes in the descriptor that identify * images and titles and initialize the lists of imageNames * and imageList. * <P> * The attributes are named "PhotoTitle-n" and "PhotoImage-n". * The value "n" must start at "1" and increment by 1. */ private void setupImageList() { imageNames = new Vector(); imageList = new List("Images", List.IMPLICIT); imageList.addCommand(exitCommand); imageList.addCommand(aboutCommand); imageList.setCommandListener(this); for (int n = 1; n < 100; n++) { String nthImage = "PhotoImage-"+ n; String image = getAppProperty(nthImage); if (image == null || image.length() == 0) break; String nthTitle = "PhotoTitle-" + n; String title = getAppProperty(nthTitle); if (title == null || title.length() == 0) title = image; imageNames.addElement(image); imageList.append(title, null); } imageNames.addElement("testchart:"); imageList.append("Test Chart", null); } /** * The Run method is used to load the images. * A form is used to report the progress of loading images * and when the loading is complete they are displayed. * Any errors that occur are reported using an Alert. * Images previously loaded into the PhotoFrame are discarded * before loading. * <P> * Load images from resource files using <code>Image.createImage</code>. * Images may be in resource files or accessed using http: * The first image is loaded to determine whether it is a * single image or a sequence of images and to make sure it exists. * If the name given is the complete name of the image then * it is a singleton. * Otherwise it is assumed to be a sequence of images * with the name as a prefix. Sequence numbers (n) are * 0, 1, 2, 3, .... The full resource name is the concatenation * of name + n + ".png". * <p> * If an OutOfMemoryError occurs the sequence of images is truncated * and an alert is used to inform the user. The images loaded are * displayed. * @see createImage */ public void run() { Thread mythread = Thread.currentThread(); Vector images = new Vector(5); /* Free images and resources used by current frame. */ frame.reset(); try { // Catch OutOfMemory Errors try { if (imageName.startsWith("testchart:")) { TestChart t = new TestChart(frame.getWidth(), frame.getHeight()); images = t.generateImages(); } else { // Try the name supplied for the single image case. images.addElement(createImage(imageName)); } } catch (IOException ex) { try { int namelen = imageName.length(); StringBuffer buf = new StringBuffer(namelen + 8); buf.append(imageName); Runtime rt = Runtime.getRuntime(); // Try for a sequence of images. for (int i = 0; ; i++) { progressGauge.setValue(i % 10); // If cancelled, discard images and return immediately if (thread != mythread) { break; } // locate the next in the series of images. buf.setLength(namelen); buf.append(i); buf.append(".png"); String name = buf.toString(); images.addElement(createImage(name)); } } catch (IOException io_ex) { } } catch (SecurityException se_ex) { // no-retry, just put up the alert } // If cancelled, discard images and return immediately if (thread != mythread) { return; } // If any images, setup the images and display them. if (images.size() > 0) { frame.setImages(images); display.setCurrent(frame); } else { // Put up an alert saying image cannot be loaded alert.setString("Images could not be loaded."); display.setCurrent(alert, imageList); } } catch (OutOfMemoryError err) { int size = images.size(); if (size > 0) { images.setSize(size-1); } // If cancelled, discard images and return immediately if (thread != mythread) { return; } alert.setString("Not enough memory for all images."); // If no images are loaded, Alert and return to the list // Othersize, Alert and display the ones that were loaded. if (images.size() <= 0) { display.setCurrent(alert, imageList); } else { frame.setImages(images); display.setCurrent(alert, frame); } } } /** * Fetch the image. If the name begins with "http:" * fetch it with connector.open and http. * If it starts with "/" then load it from the * resource file. * @param name of the image to load * @return image created * @exception IOException if errors occuring doing loading */ private Image createImage(String name) throws IOException { if (name.startsWith("/")) { // Load as a resource with Image.createImage return Image.createImage(name); } else if (name.startsWith("http:")) { // Load from a ContentConnection HttpConnection c = null; DataInputStream is = null; try { c = (HttpConnection)Connector.open(name); int status = c.getResponseCode(); if (status != 200) { throw new IOException("HTTP Response Code = " + status); } int len = (int)c.getLength(); String type = c.getType(); if (!type.equals("image/png") && !type.equals("image/jpeg")) { throw new IOException("Expecting image, received " + type); } if (len > 0) { is = c.openDataInputStream(); byte[] data = new byte[len]; is.readFully(data); return Image.createImage(data, 0, len); } else { throw new IOException("Content length is missing"); } } finally { if (is != null) is.close(); if (c != null) c.close(); } } else { throw new IOException("Unsupported media"); } } /** * Open the store that holds the saved options. * If an error occurs, put up an Alert. */ void openOptions() { try { optionsStore = RecordStore.openRecordStore(optionsName, true); } catch (RecordStoreException ex) { alert.setString("Could not access options storage"); display.setCurrent(alert); optionsStore = null; } } /** * Save the options to persistent storage. * The options are retrieved ChoiceGroups and stored * in Record 1 of the store which is reserved for it. * The two options are stored in bytes 0 and 1 of the record. */ void saveOptions() { if (optionsStore != null) { byte[] options = new byte[2]; options[0] = (byte)frame.getStyle(); options[1] = (byte)frame.getSpeed(); try { optionsStore.setRecord(1, options, 0, options.length); } catch (InvalidRecordIDException ridex) { // Record 1 did not exist, create a new record (Should be 1) try { int rec = optionsStore.addRecord(options, 0, options.length); } catch (RecordStoreException ex) { alert.setString("Could not add options record"); display.setCurrent(alert); } } catch (RecordStoreException ex) { alert.setString("Could not save options"); display.setCurrent(alert); } } } /** * Restore the options from persistent storage. * The options are read from record 1 and set in * the frame and if the optionsForm has been created * in the respective ChoiceGroups. */ void restoreOptions() { if (optionsStore != null) { try { byte[] options = optionsStore.getRecord(1); if (options.length == 2) { frame.setStyle(options[0]); frame.setSpeed(options[1]); if (optionsForm != null) { borderChoice.setSelectedIndex(options[0], true); speedChoice.setSelectedIndex(options[1], true); } return; // Return all set } } catch (RecordStoreException ex) { // Ignore, use normal defaults } } } /** * Close the options store. */ void closeOptions() { if (optionsStore != null) { try { optionsStore.closeRecordStore(); optionsStore = null; } catch (RecordStoreException ex) { alert.setString("Could not close options storage"); display.setCurrent(alert); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -