Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

J2ME like Sprite on Android

For my useless project of the month I'm working on a 'emulator' to run J2ME programs on Android. But now I'm stuck with the J2ME Sprite implementation. Specifically the transformations used in it.

In my Sprite I have a bitmap with three character images. I would like to paint the second frame mirrored or rotated 90 degrees. What would be the best way for it?

I have following code that paints the given frame without any transformations.

frameX, frameY are frame position coordinates on give sprite bitmap.

Rect src = new Rect(frameX, frameY, frameX + spriteWidth, frameY + spriteHeight);
Rect dst = new Rect(paintX, paintY, paintX + spriteWidth, paintY + spriteHeight);
canvas.drawBitmap(image, src, dst, null);

As I understand I need to make some matrix magic on the canvas, but I have not been able to figure this out :)

like image 518
JaanusSiim Avatar asked Jan 24 '23 04:01

JaanusSiim


2 Answers

You do know that Microemulator, an open source project, makes it possible to run J2ME code on Android, right?

http://www.microemu.org/

You could always have a look and see what they do.

like image 99
David N. Welton Avatar answered Feb 03 '23 17:02

David N. Welton


Went with splitting sprite into frames and using transformations with single image:

public final void paint(final Canvas canvas) {
  final Bitmap painted = images[frame];
  final Matrix matrix = createTransformationMatrix(transform);
  matrix.postTranslate(spriteX, spriteY);
  canvas.drawBitmap(painted, matrix, null);
}

private Matrix createTransformationMatrix(final int transform2) {
  final Matrix result = new Matrix();
  switch (transform2) {
  case TRANS_NONE:
    break;
  case TRANS_MIRROR_ROT180:
    result.setScale(-1, 1);
    result.postTranslate(getWidth(), 0);
    result.postRotate(180);
    break;
  case TRANS_MIRROR:
    result.setScale(-1, 1);
    result.postTranslate(getWidth(), 0);
    break;
  case TRANS_ROT180:
    result.postRotate(180);
    break;
  case TRANS_MIRROR_ROT270:
    result.setScale(-1, 1);
    result.postTranslate(getWidth(), 0);
    result.postRotate(270);
    break;
  case TRANS_ROT90:
    result.postRotate(90);
    break;
  case TRANS_ROT270:
    result.postRotate(270);
    break;
  case TRANS_MIRROR_ROT90:
    result.setScale(-1, 1);
    result.postTranslate(getWidth(), 0);
    result.postRotate(90);
    break;
  }
  return result;
}

Works like a charm :)

like image 22
JaanusSiim Avatar answered Feb 03 '23 17:02

JaanusSiim