Use caching in J2ME to store screen content as image

Source: Internet
Author: User
Tags return
Caching This article describes how to store the contents of the phone screen as an image object, where it is thought that a canvas is displayed on the phone screen. The idea of completing this function is to use a buffering mechanism. We can't get the pixels on the canvas directly, so we can't get the image object directly from the content on canvas. Convert the idea, if you want to draw the contents of the canvas first to a image, and this image does not appear on the screen, but only after the painting is completed one-time display to the screen. Experienced friends must have a double buffering mechanism, but here is not to use double buffering to solve the problem of the splash screen, but to get the current canvas content.

Let's write a simple canvas class to test the idea that Simplecanvas is a subclass of canvas, in order to preserve canvas content, we create an image that is the same size as the canvas.

Class Simplecanvas extends canvas{

int W;
int h;
Private Image offimage = null;
Private Boolean buffered = true;
Public Simplecanvas (Boolean _buffered) {
buffered = _buffered;
W = getwidth ();
h = getheight ();
if (buffered)
Offimage = Image.createimage (w,h);
}
protected void Paint (Graphics g) {

int color = G.getcolor ();
G.setcolor (0xFFFFFF);
G.fillrect (0,0,W,H);
G.setcolor (color);

Graphics save = g;
if (offimage!= null)
g = Offimage.getgraphics ();
Draw the Offimage
G.setcolor (128,128,0);
G.fillroundrect ((w-100)/2, (h-60)/2,100,60,5,3);
Draw the offimage to the canvas
Save.drawimage (offimage,0,0,graphics.top| Graphics.left);
}

Public Image Printme () {
return offimage;
}

You can see the paint () method, not directly to the canvas operation, but first to draw the content to a image, and then draw to the canvas. You can then call the Printme () method when you want to crawl the content of the screen, and return to Offimage. Write a MIDlet test this effect.

Package Com.j2medev;

Import javax.microedition.midlet.*;
Import javax.microedition.lcdui.*;

/**
*
* @author Mingjava
* @version
*/
public class Printscreen extends MIDlet implements commandlistener{

Private display display = NULL;
Private Simplecanvas canvas = new Simplecanvas (true);
Private command Printcommand = new Command ("Print", command.ok,1);

public void startApp () {
if (display = = NULL)
display = Display.getdisplay (this);
Canvas.addcommand (Printcommand);
Canvas.setcommandlistener (this);
Display.setcurrent (canvas);
}

public void Pauseapp () {}

public void Destroyapp (Boolean unconditional) {}

public void commandaction (Command command, displayable displayable) {
if (Command = = Printcommand) {
Form form = new form ("screen");
Form.append (Canvas.printme ());
Display.setcurrent (form);
}
}
}
Run Printscreen and select Print to display the current screen in a form. As shown in the following illustration:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.