Java by API/javax.microedition.lcdui/Graphics — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:49, 31 мая 2010

Graphics.BASELINE

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class ClockMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  Display display;
  public void startApp() {
    Display display = Display.getDisplay(this);
    Displayable d = new ClockCanvas(10, 10, 10);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    d.addCommand(exitCommand);
    d.setCommandListener(this);
    display.setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
}
class ClockCanvas extends Canvas {
  private int hour;
  private int minute;
  private int second;
  protected int xCenter, yCenter;
  protected int clockRadius;
  int width = 0;
  int height = 0;
  ClockCanvas(int hour, int minute, int second) {
    this.hour = hour;
    this.minute = minute;
    this.second = second;
  }
  public void paint(Graphics g) {
    width = getWidth();
    height = getHeight();
    g.setGrayScale(255);
    g.fillRect(0, 0, width - 1, height - 1);
    g.setGrayScale(0);
    g.drawRect(0, 0, width - 1, height - 1);
    clockRadius = Math.min(width, height) - 20;
    xCenter = getWidth() / 2;
    yCenter = getHeight() / 2;
    g.drawArc(10, 12, clockRadius, clockRadius, 0, 360);
    g.drawString("12", xCenter, 0, Graphics.TOP | Graphics.HCENTER);
    g.drawString("9", 1, yCenter, Graphics.BASELINE | Graphics.LEFT);
    g.drawString("3", width - 1, yCenter, Graphics.BASELINE | Graphics.RIGHT);
    g.drawString("6", xCenter, height, Graphics.BOTTOM | Graphics.RIGHT);
  }
}





Graphics.BOTTOM

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class ClockMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  Display display;
  public void startApp() {
    Display display = Display.getDisplay(this);
    Displayable d = new ClockCanvas(10, 10, 10);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    d.addCommand(exitCommand);
    d.setCommandListener(this);
    display.setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
}
class ClockCanvas extends Canvas {
  private int hour;
  private int minute;
  private int second;
  protected int xCenter, yCenter;
  protected int clockRadius;
  int width = 0;
  int height = 0;
  ClockCanvas(int hour, int minute, int second) {
    this.hour = hour;
    this.minute = minute;
    this.second = second;
  }
  public void paint(Graphics g) {
    width = getWidth();
    height = getHeight();
    g.setGrayScale(255);
    g.fillRect(0, 0, width - 1, height - 1);
    g.setGrayScale(0);
    g.drawRect(0, 0, width - 1, height - 1);
    clockRadius = Math.min(width, height) - 20;
    xCenter = getWidth() / 2;
    yCenter = getHeight() / 2;
    g.drawArc(10, 12, clockRadius, clockRadius, 0, 360);
    g.drawString("12", xCenter, 0, Graphics.TOP | Graphics.HCENTER);
    g.drawString("9", 1, yCenter, Graphics.BASELINE | Graphics.LEFT);
    g.drawString("3", width - 1, yCenter, Graphics.BASELINE | Graphics.RIGHT);
    g.drawString("6", xCenter, height, Graphics.BOTTOM | Graphics.RIGHT);
  }
}





Graphics.DOTTED

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SetStrokeStyleGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(0);
    g.setStrokeStyle(Graphics.DOTTED);
    g.drawLine(0, height / 2, width - 1, height / 2);
  }
}





Graphics: drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class J2MEArcExample extends MIDlet {
  private Display display;
  private MyCanvas canvas;
  public J2MEArcExample() {
    display = Display.getDisplay(this);
    canvas = new MyCanvas(this);
  }
  protected void startApp() {
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
  public void exitMIDlet() {
    destroyApp(true);
    notifyDestroyed();
  }
}
class MyCanvas extends Canvas implements CommandListener {
  private Command exit;
  private J2MEArcExample arcExample;
  public MyCanvas(J2MEArcExample arcExample) {
    this.arcExample = arcExample;
    exit = new Command("Exit", Command.EXIT, 1);
    addCommand(exit);
    setCommandListener(this);
  }
  protected void paint(Graphics graphics) {
    graphics.setColor(255, 255, 255);
    graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.setColor(255, 0, 0);
    graphics.drawArc(0, 0, getWidth(), getHeight(), 180, 180);
  }
  public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
      arcExample.exitMIDlet();
    }
  }
}





Graphics: drawLine(int x1, int y1, int x2, int y2)

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SetColorGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(0xFFFF00);
    g.drawLine(0, height / 4, width - 1, height / 4);
  }
}





Graphics: drawString(String str, int x, int y, int anchor)

 

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class DrawStringGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(0);
    g.setStrokeStyle(Graphics.SOLID);
    g.drawString("Top left", 0, 0, Graphics.TOP | Graphics.LEFT);

  }
}





Graphics: fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class FillArcGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(0);
    g.setStrokeStyle(Graphics.SOLID);
    g.fillArc(0, 0, width/2, height/2, 0, 90);

  }
}





Graphics: fillRect(int x, int y, int width, int height)

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SizeMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  Display display;
  public void startApp() {
    Display display = Display.getDisplay(this);
    Displayable d = new SizeCanvas();
    exitCommand = new Command("Exit", Command.EXIT, 1);
    d.addCommand(exitCommand);
    d.setCommandListener(this);
    display.setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
}
class SizeCanvas extends Canvas {
  int width = 0;
  int height = 0;
  public void paint(Graphics g) {
    width = getWidth();
    height = getHeight();
    g.setGrayScale(255);
    g.fillRect(0, 0, width - 1, height - 1);
    g.setGrayScale(0);
    g.drawString("Stroke Style:" + g.getStrokeStyle(), 0, 60, Graphics.TOP | Graphics.LEFT);
    g.drawRect(0, 0, width - 1, height - 1);
    g.setStrokeStyle(Graphics.DOTTED);
    g.drawLine(00, 40, 60, 60);
  }
}





Graphics: fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class J2MEPacer extends MIDlet {
  public void startApp() {
    Displayable d = new PacerCanvas();
    d.addCommand(new Command("Exit", Command.EXIT, 0));
    d.setCommandListener(new CommandListener() {
      public void commandAction(Command c, Displayable s) {
        notifyDestroyed();
      }
    });
    Display.getDisplay(this).setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
}
class PacerCanvas extends Canvas {
  public void paint(Graphics g) {
    int w = getWidth();
    int h = getHeight();
    g.setColor(0xffffff);
    g.fillRect(0, 0, w, h);
    g.setColor(0x000000);
    g.drawRect(12, 12, 20, 20);
    g.fillRoundRect(33, 33, 20, 20, 5, 5);
    g.drawArc(8, 88, 20, 20, 0, 360);
  }
}





Graphics.HCENTER

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class ClockMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  Display display;
  public void startApp() {
    Display display = Display.getDisplay(this);
    Displayable d = new ClockCanvas(10, 10, 10);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    d.addCommand(exitCommand);
    d.setCommandListener(this);
    display.setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
}
class ClockCanvas extends Canvas {
  private int hour;
  private int minute;
  private int second;
  protected int xCenter, yCenter;
  protected int clockRadius;
  int width = 0;
  int height = 0;
  ClockCanvas(int hour, int minute, int second) {
    this.hour = hour;
    this.minute = minute;
    this.second = second;
  }
  public void paint(Graphics g) {
    width = getWidth();
    height = getHeight();
    g.setGrayScale(255);
    g.fillRect(0, 0, width - 1, height - 1);
    g.setGrayScale(0);
    g.drawRect(0, 0, width - 1, height - 1);
    clockRadius = Math.min(width, height) - 20;
    xCenter = getWidth() / 2;
    yCenter = getHeight() / 2;
    g.drawArc(10, 12, clockRadius, clockRadius, 0, 360);
    g.drawString("12", xCenter, 0, Graphics.TOP | Graphics.HCENTER);
    g.drawString("9", 1, yCenter, Graphics.BASELINE | Graphics.LEFT);
    g.drawString("3", width - 1, yCenter, Graphics.BASELINE | Graphics.RIGHT);
    g.drawString("6", xCenter, height, Graphics.BOTTOM | Graphics.RIGHT);
  }
}





Graphics.LEFT

 

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class KeyEventsMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  Display display;
  Displayable d;
  public void startApp() {
    Display display = Display.getDisplay(this);
    d = new KeyEventsCanvas();
    exitCommand = new Command("Exit", Command.EXIT, 1);
    d.addCommand(exitCommand);
    d.setCommandListener(this);
    display.setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
}
class KeyEventsCanvas extends Canvas {
  int width = 0;
  int height = 0;
  String aMessage = "message";
  public void paint(Graphics g) {
    width = getWidth();
    height = getHeight();
    g.setGrayScale(255);
    g.fillRect(0, 0, width - 1, height - 1);
    g.setGrayScale(0);
    g.drawRect(0, 0, width - 1, height - 1);
    g.drawString(aMessage, 10, 10, Graphics.TOP | Graphics.LEFT);
  }
  protected void keyPressed(int keyCode) {
    aMessage = getKeyName(keyCode);
    if (aMessage.equals("2"))
      aMessage = "2";
    repaint();
  }
}





Graphics: setClip(int x, int y, int width, int height)

 
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
public class J2MEClippingRegion extends MIDlet {
  private Display display;
  private MyCanvas canvas;
  public J2MEClippingRegion() {
    display = Display.getDisplay(this);
    canvas = new MyCanvas(this);
  }
  protected void startApp() {
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
  public void exitMIDlet() {
    destroyApp(true);
    notifyDestroyed();
  }
  class MyCanvas extends Canvas implements CommandListener {
    private Command exit;
    private J2MEClippingRegion clippingRegion;
    private Image image = null;
    public MyCanvas(J2MEClippingRegion clippingRegion) {
      this.clippingRegion = clippingRegion;
      exit = new Command("Exit", Command.EXIT, 1);
      addCommand(exit);
      setCommandListener(this);
      try {
        image = Image.createImage(70, 70);
        Graphics graphics = image.getGraphics();
        graphics.setColor(255, 0, 0);
        graphics.fillArc(10, 10, 60, 50, 180, 180);
      } catch (Exception error) {
        Alert alert = new Alert("Failure", "Creating Image", null, null);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert);
      }
    }
    protected void paint(Graphics graphics) {
      if (image != null) {
        graphics.setColor(255, 255, 255);
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics.setClip(35, 35, 40, 40);
        graphics.drawImage(image, 30, 30, Graphics.VCENTER | Graphics.HCENTER);
      }
    }
    public void commandAction(Command command, Displayable display) {
      if (command == exit) {
        clippingRegion.exitMIDlet();
      }
    }
  }
}





Graphics: setColor(int RGB)

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SetColorGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(0xFFFF00);
    g.drawLine(0, height / 4, width - 1, height / 4);
  }
}





Graphics: setGrayScale(int value)

 

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SetGrayScaleGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(0);
    g.setGrayScale(100);
    g.drawLine(0,  2, width - 1, height / 2);
  }
}





Graphics: setStrokeStyle(int style)

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SetStrokeStyleGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(0);
    g.setStrokeStyle(Graphics.DOTTED);
    g.drawLine(0, height / 2, width - 1, height / 2);
  }
}





Graphics.SOLID

 

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class DrawArcGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(0);
    g.setStrokeStyle(Graphics.SOLID);
        g.drawArc(0, 0, width/2, height/2, 0, 90);

  }
}





Graphics.TOP

 

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class KeyEventsMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  Display display;
  Displayable d;
  public void startApp() {
    Display display = Display.getDisplay(this);
    d = new KeyEventsCanvas();
    exitCommand = new Command("Exit", Command.EXIT, 1);
    d.addCommand(exitCommand);
    d.setCommandListener(this);
    display.setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
}
class KeyEventsCanvas extends Canvas {
  int width = 0;
  int height = 0;
  String aMessage = "message";
  public void paint(Graphics g) {
    width = getWidth();
    height = getHeight();
    g.setGrayScale(255);
    g.fillRect(0, 0, width - 1, height - 1);
    g.setGrayScale(0);
    g.drawRect(0, 0, width - 1, height - 1);
    g.drawString(aMessage, 10, 10, Graphics.TOP | Graphics.LEFT);
  }
  protected void keyPressed(int keyCode) {
    aMessage = getKeyName(keyCode);
    if (aMessage.equals("2"))
      aMessage = "2";
    repaint();
  }
}





Graphics: translate(int x, int y)

 

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class DisplayBigImageTest extends MIDlet implements CommandListener {
  private Display display;
  private TestCanvas tc;
  private Command leftCommand = new Command("Left", Command.SCREEN, 1);
  private Command rightCommand = new Command("Right", Command.SCREEN, 1);
  public DisplayBigImageTest() {
    Image img = null;
    try {
      img = Image.createImage("/10.png");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    tc = new TestCanvas(img);
    tc.addCommand(rightCommand);
    tc.addCommand(leftCommand);
    tc.setCommandListener(this);
    display = Display.getDisplay(this);
  }
  public void startApp() throws MIDletStateChangeException {
    display.setCurrent(tc);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable d) {
    int stepX = tc.getWidth() / 4;
    int stepY = tc.getHeight() / 4;
    if (d == tc && c == leftCommand) {
      tc.increaseXY(stepX, 0);
      tc.repaint();
    } else if (d == tc && c == rightCommand) {
      tc.increaseXY(-stepX, 0);
      tc.repaint();
    }
  }
  class TestCanvas extends Canvas {
    private Image img;
    private int transX = 0;
    private int transY = 0;
    public TestCanvas(Image img) {
      this.img = img;
      transX = 0;
      transY = 0;
    }
    public void increaseXY(int x, int y) {
      transX += x;
      transY += y;
    }
    public void paint(Graphics g) {
      int width = this.getWidth();
      int height = this.getHeight();
      g.setGrayScale(255);
      g.fillRect(0, 0, width - 1, height - 1);
      g.setGrayScale(0);
      g.drawRect(0, 0, width - 1, height - 1);
      g.translate(transX, transY);
      g.drawImage(img, 0, 0, g.TOP | g.LEFT);
    }
  }
}





Graphics.VCENTER

 
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
public class ImageVerticalHorizontalGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    try {
      Image image = Image.createImage("/h.png");
      g.drawImage(image, width/2, height/2, Graphics.VCENTER | Graphics.HCENTER);
    } catch (IOException ex) {
      g.setColor(0xffffff);
      g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
      return;
    }
  }
}