| |
"Programming Games in J2ME"
Vol. 7, Issue 6, p. 90
Listing 1
javax.microedition.lcdui.*;
public class gameCanvas extends Canvas
{
...
Image backBuffer;
Graphics backBufferGraphics;
...
public gameCanvas()
{
....
// initialize the back-buffer to be the same dimension as the canvas,
// and get the Graphics of the buffer for off-screen drawing
backBuffer = Image.createImage( getWidth(), getHeight() );
backBufferGraphics = backBuffer.getGraphics();
....
}
....
public void paint(Graphics g)
{
...
// do all the Graphics drawing onto the back-buffer
backBufferGraphics.draw...
...
// now "flip" the back-buffer to the front
// by drawing the whole back-buffer image onto the screen.
// since individual Graphics call is "double-buffered",
// this wonÕt create any "tearing" on the screen.
g.drawImage( backBuffer, 0, 0 Graphics.TOP|Graphics.LEFT );
}
....
}
Listing 2
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
try
{
// open http connection
c = (HttpConnection)Connector.open( url, Connector.READ_WRITE );
// Set the request method and headers
c.setRequestMethod( HttpConnection.POST );
c.setRequestProperty( "User-Agent" , "Profile/MIDP-1.0 Configuration/CLDC-1.0" );
c.setRequestProperty( "Content-Language" , "en-US" );
c.setRequestProperty( "Connection" , "close" );
// write request
os = c.openOutputStream();
....
// get response
is = c.openInputStream();
...
...
}
finally
{
try{ os.close(); }catch( Exception e ){}
try{ is.close(); }catch( Exception e ){}
try{ c.close(); }catch( Exception e ){}
}
Listing 3
StreamConnection connection = null;
InputStream is = null;
OutputStream os = null;
try
{
Connection aConnection = Connector.open( "socket://" + host
+ ":" + port);
connection = (StreamConnection) aConnection;
os = connection.openOutputStream();
..
is = connection.openInputStream();
...
}
finally
{
try{ os.close(); }catch( Exception e ){}
try{ is.close(); }catch( Exception e ){}
try{ c.close(); }catch( Exception e ){}
}
|
|