CanvasImplementation.java
Upload User: haobig99
Upload Date: 2022-06-15
Package Size: 369k
Code Size: 5k
Category:

J2ME

Development Platform:

Java

  1. /*
  2.  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4.  *
  5.  * This code is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License version 2 only, as
  7.  * published by the Free Software Foundation.  Sun designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Sun in the LICENSE file that accompanied this code.
  10.  *
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * version 2 for more details (a copy is included in the LICENSE file that
  15.  * accompanied this code).
  16.  *
  17.  * You should have received a copy of the GNU General Public License version
  18.  * 2 along with this work; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22.  * CA 95054 USA or visit www.sun.com if you need additional information or
  23.  * have any questions.
  24.  */
  25. package com.sun.lwuit.impl.midp;
  26. import com.sun.lwuit.Display;
  27. import javax.microedition.lcdui.Canvas;
  28. import javax.microedition.lcdui.Command;
  29. import javax.microedition.lcdui.CommandListener;
  30. import javax.microedition.lcdui.Displayable;
  31. /**
  32.  * An implementation of LWUIT based on game canvas, this is the default implementation
  33.  * class for LWUIT which most customizers should extend to enhance.
  34.  *
  35.  * @author Shai Almog
  36.  */
  37. public class CanvasImplementation extends GameCanvasImplementation {
  38.     private class C extends Canvas  implements CommandListener, Runnable {
  39.         private javax.microedition.lcdui.Image backBuffer;
  40.         private boolean done;
  41.         C() {
  42.             setFullScreenMode(true);
  43.         }
  44.         
  45.         public void run() {
  46.             while (!done) {
  47.                 synchronized (getDisplayLock()) {
  48.                     try {
  49.                         getDisplayLock().wait(50);
  50.                     } catch (InterruptedException ex) {
  51.                         ex.printStackTrace();
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.         public void setDone(boolean done) {
  57.             this.done = done;
  58.             synchronized (getDisplayLock()) {
  59.                 getDisplayLock().notify();
  60.             }
  61.         }
  62.         public void commandAction(Command c, Displayable d) {
  63.             if (d == currentTextBox) {
  64.                 display.setCurrent(this);
  65.                 if (c == CONFIRM_COMMAND) {
  66.                     // confirm
  67.                     String text = currentTextBox.getString();
  68.                     Display.getInstance().onEditingComplete(currentTextComponent, text);
  69.                 }
  70.                 currentTextBox = null;
  71.                 setDone(true);
  72.             }
  73.         }
  74.         public javax.microedition.lcdui.Graphics getGraphics() {
  75.             if(backBuffer == null || backBuffer.getWidth() != getWidth() || backBuffer.getHeight() != getHeight()) {
  76.                 backBuffer = javax.microedition.lcdui.Image.createImage(getWidth(), getHeight());
  77.             }
  78.             return backBuffer.getGraphics();
  79.         }
  80.         public void paint(javax.microedition.lcdui.Graphics g) {
  81.             if(backBuffer != null) {
  82.                 g.drawImage(backBuffer, 0, 0, javax.microedition.lcdui.Graphics.TOP | javax.microedition.lcdui.Graphics.LEFT);
  83.             }
  84.         }
  85.         
  86.         protected void keyPressed(final int keyCode){
  87.             CanvasImplementation.this.keyPressed(keyCode);
  88.         }
  89.         protected  void keyReleased(final int keyCode){
  90.             CanvasImplementation.this.keyReleased(keyCode);
  91.         }
  92.         protected  void pointerDragged(final int x, final int y){
  93.             CanvasImplementation.this.pointerDragged(x, y);
  94.         }
  95.         protected  void pointerPressed(final int x,final int y){
  96.             CanvasImplementation.this.pointerPressed(x, y);
  97.         }
  98.         protected  void pointerReleased(final int x,final int y){
  99.             CanvasImplementation.this.pointerReleased(x, y);
  100.         }
  101.         protected void sizeChanged(int w, int h){
  102.             CanvasImplementation.this.sizeChanged(w, h);
  103.         }
  104.     }
  105.     
  106.     /**
  107.      * @inheritDoc
  108.      */
  109.     protected Canvas createCanvas() {
  110.         return new C();
  111.     }
  112.     /**
  113.      * @inheritDoc
  114.      */
  115.     public void flushGraphics(int x, int y, int width, int height) {
  116.         getCanvas().repaint(x, y, width, height);
  117.     }
  118.     /**
  119.      * @inheritDoc
  120.      */
  121.     public void flushGraphics() {
  122.         getCanvas().repaint();
  123.     }
  124.     
  125.     /**
  126.      * @inheritDoc
  127.      */
  128.     public Object getNativeGraphics() {
  129.         return ((C)getCanvas()).getGraphics();
  130.     }
  131. }