⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pngviewer.java

📁 This example is written for version 1.0.3 of MIDP and CLDC The PngViewer program completes dow
💻 JAVA
字号:
/*--------------------------------------------------
| PngViewer.java
|
| Download and view a png file
|
| Copyright:  John W. Muchow
| Email:      john@corej2me.com
| Web:        www.corej2me.com
|
| Version: 1.0 - Original Release
| Version: 1.1 - Updated getImage() to use
|                readFully() and also support reading
|                one character at a time when length
|                is not available
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

/*--------------------------------------------------
* Main class definition
*-------------------------------------------------*/
public class PngViewer extends MIDlet implements CommandListener
{
  private Display display;
  private TextBox tbMain;
  private Form fmViewPng;
  private Command cmExit;
  private Command cmView;
  private Command cmdBack;

  /*--------------------------------------------------
  * Constructor for the class.
  *-------------------------------------------------*/
  public PngViewer()
  {
    display = Display.getDisplay(this);

    // Create the Main textbox with a maximum of 50 characters
    tbMain = new TextBox("Enter png url", "http://www.corej2me.com/scratch/spin.png", 50, 0);

    // Create commands and add to textbox
    cmView = new Command("View", Command.SCREEN, 1);
    cmExit = new Command("Exit", Command.SCREEN, 1);
    tbMain.addCommand(cmView );
    tbMain.addCommand(cmExit);

    // Set up a listener to "capture" button presses
    tbMain.setCommandListener(this);

    // ---------------------------------------

    // Create the form that will hold the png image
    fmViewPng = new Form("Png Image");

    // Create commands and add to form
    cmdBack = new Command("Back", Command.BACK, 1);
    fmViewPng.addCommand(cmdBack);

    // Set up a listener to "capture" button presses
    fmViewPng.setCommandListener(this);
  }

  /*--------------------------------------------------
  * Called by the Application Manager on the device
  * to start the MIDlet.
  *-------------------------------------------------*/
  public void startApp()
  {
    // Display the Main textbox
    display.setCurrent(tbMain);
  }

  /*--------------------------------------------------
  * A required method.
  * Does nothing in this MIDlet
  *-------------------------------------------------*/
  public void pauseApp()
  {
  }

  /*--------------------------------------------------
  * A required method.
  * Does nothing in this MIDlet
  *-------------------------------------------------*/
  public void destroyApp(boolean unconditional)
  {
  }

  /*--------------------------------------------------
  * Process events
  *-------------------------------------------------*/
  public void commandAction(Command c, Displayable s)
  {
    // If the Command button pressed was "Exit"
    if (c == cmExit)
    {
      destroyApp(false);
      notifyDestroyed();
    }
    else if (c == cmView)  // If the Command button pressed was "View"
    {
      // Remove anything that may be on the form
      if (fmViewPng.size() > 0)
        for (int i = 0; i < fmViewPng.size(); i++)
          fmViewPng.delete(i);

      try
      {
       // Get the image from the web and append to the form
        Image im;
        if ((im = getImage(tbMain.getString())) != null)
          fmViewPng.append(im);

        // Display the form with the image
        display.setCurrent(fmViewPng);
      }
      catch (Exception e)
      { 
        System.err.println("Msg: " + e.toString());
      }
    }
    else if (c == cmdBack)  // If the Command button pressed was "Back"
      display.setCurrent(tbMain);
  }

  /*--------------------------------------------------
  * Open an http connection and download a png file
  * into a byte array.
  *-------------------------------------------------*/
  private Image getImage(String url) throws IOException
  {
    ContentConnection connection = (ContentConnection) Connector.open(url);
    DataInputStream iStrm = connection.openDataInputStream();    
        
    Image im = null;

    try
    {
      // ContentConnection includes a length method
      byte imageData[];
      int length = (int) connection.getLength();
      if (length != -1)
      {
        imageData = new byte[length];

        // Read the png into an array        
        iStrm.readFully(imageData);
      }
      else  // Length not available...
      {       
        ByteArrayOutputStream bStrm = new ByteArrayOutputStream();       
        
        int ch;
        while ((ch = iStrm.read()) != -1)
          bStrm.write(ch);
        
        imageData = bStrm.toByteArray();
        bStrm.close();                
      }

      // Create the image from the byte array
      im = Image.createImage(imageData, 0, imageData.length);        
    }
    finally
    {
      // Clean up
      if (iStrm != null)
        iStrm.close();
      if (connection != null)
        connection.close();
    }
    return (im == null ? null : im);
  }
}

⌨️ 快捷键说明

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