Use the cache to store the screen content as an image

Source: Internet
Author: User
This article describes how to store the content on the mobile phone screen as an Image object. Here we think that a Canvas is displayed on the mobile phone screen. The thought of implementing this function is to use a buffer mechanism. We cannot directly obtain pixels on the Canvas, so we cannot directly obtain the Image object from the content on the Canvas. If you want to plot the content on the Canvas to an Image first, and the Image is not displayed on the screen, it is only displayed on the screen once after painting is completed. Experienced users may think of the dual-buffer mechanism, but it is not to use dual-buffer to solve the problem of flashing the screen, but to get the content of the current Canvas.

Let's write a simple canvas class to test this idea. simplecanvas is a subclass of canvas. To save the canvas content, we create an image with the same size as the canvas size.

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 (0 xFFFFFF );
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;
} We can see that the painting () method does not directly operate on the Canvas, but first draws the content to be drawn to an Image and then draws it to the Canvas. In this way, you can call the printMe () method when you want to capture the screen content and return offImage. Write a MIDlet to 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 to a form.

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.