this is a extra element for clear the floated element
J2ME:clip窗口可使用anchor--转
  • 12/31
  • 2008
程序设计 | 游戏开发 1105 次查看
  /**

  * drawClip 画一个图片的一部分。clip窗口可使用anchor。

  *

  * @param g

  *

  Graphics

  * @param x

  *

  int 要画的位置x

  * @param y

  *

  int 要画的位置y

  * @param image

  *

  Image 图片

  * @param clipX

  *

  int clip在图片中的坐标x

  * @param clipY

  *

  int clip在图片中的坐标y

  * @param clipWidth

  *

  int clip在图片中的宽度

  * @param clipHeight

  *

  int clip在图片中的高度

  * @param anchor

  *

  int 链接锚点

  */

  public static void drawClip(Graphics g, int x, int y, Image image,

  int clipX, int clipY, int clipWidth, int clipHeight, int anchor) {

  int[] old = { g.getClipX(), g.getClipY(), g.getClipWidth(),

  g.getClipHeight() }; //存储原来的初始Clip大小和坐标

  int offsetX = 0, offsetY = 0;

  switch (anchor) {

  // 对各种的anchor的处理就是对Graphics.TOP|Graphics.LEFT的相对偏移

  // 取各种情况下的相对于Graphics.TOP|Graphics.LEFT的X,Y偏移,最后

  // 以Graphics.TOP|Graphics.LEFT的anchor画出图形

  case Graphics.HCENTER | Graphics.TOP:

  offsetX = clipWidth / 2;

  break;

  case Graphics.RIGHT | Graphics.TOP:

  offsetX = clipWidth;

  break;

  case Graphics.LEFT | Graphics.VCENTER:

  offsetY = clipHeight / 2;

  break;

  case Graphics.HCENTER | Graphics.VCENTER:

  offsetX= clipWidth /2;

  offsetY = clipHeight / 2;

  break;

  case Graphics.RIGHT | Graphics.VCENTER:

  offsetX = clipWidth;

  offsetY = clipHeight /2;

  break;

  case Graphics.LEFT | Graphics.BOTTOM:

  offsetY = clipHeight;

  break;

  case Graphics.HCENTER | Graphics.BOTTOM:

  offsetX = clipWidth / 2;

  offsetY = clipHeight;

  break;

  case Graphics.RIGHT | Graphics.BOTTOM:

  offsetX = clipWidth;

  offsetY = clipHeight;

  break;

  }

  g.setClip(x - offsetX, y - offsetY, clipWidth, clipHeight);

  g.drawImage(image, x - clipX - offsetX, y - clipY - offsetY,

  Graphics.TOP | Graphics.LEFT);

  g.setClip(old[0], old[1], old[2], old[3]); //画完后恢复CLIP大小位置

  }