www.pudn.com > j2me-game.rar > SCanvas.java


import javax.microedition.lcdui.*; 
 
public class SCanvas extends Canvas { 
  private Display display; 
   
  public SCanvas(Display d) { 
    super(); 
    display = d; 
  } 
 
  void start() { 
    display.setCurrent(this); 
    repaint(); 
  } 
 
  public void paint(Graphics g) { 
    // Clear the canvas 
    g.setColor(0, 0, 0);        // black 
    g.fillRect(0, 0, getWidth(), getHeight()); 
    g.setColor(255, 255, 255);  // white 
 
    // Draw the available screen size 
    int y = 0; 
    String screenSize = "Screen size: " + Integer.toString(getWidth()) + " x " + 
      Integer.toString(getHeight()); 
    g.drawString(screenSize, 0, y, Graphics.TOP | Graphics.LEFT); 
 
    // Draw the number of available colors 
    y += Font.getDefaultFont().getHeight(); 
    String numColors = "# of colors: " + Integer.toString(display.numColors()); 
    g.drawString(numColors, 0, y, Graphics.TOP | Graphics.LEFT); 
 
    // Draw the number of available alpha levels 
    y += Font.getDefaultFont().getHeight(); 
    String numAlphas = "# of alphas: " + Integer.toString(display.numAlphaLevels()); 
    g.drawString(numAlphas, 0, y, Graphics.TOP | Graphics.LEFT); 
 
    // Draw the amount of total and free memory 
    Runtime runtime = Runtime.getRuntime(); 
    y += Font.getDefaultFont().getHeight(); 
    String totalMem = "Total memory: " + Long.toString(runtime.totalMemory() / 1024) + "KB"; 
    g.drawString(totalMem, 0, y, Graphics.TOP | Graphics.LEFT); 
    y += Font.getDefaultFont().getHeight(); 
    String freeMem = "Free memory: " + Long.toString(runtime.freeMemory() / 1024) + "KB"; 
    g.drawString(freeMem, 0, y, Graphics.TOP | Graphics.LEFT); 
  } 
}