Main Methods: image. createimage (IMG, X, Y, width, height, rotation );
Parameter: IMG, Target Image
X, Y, width, and height indicate part of the IMG of the target image.
Rotation is the rotation angle.
/**
* @ Author jcuckoo
* @ Creation date:
* @ Version: V 1.0
*/
Public class imagecanvas extends canvas {
// Image coordinates
Private int x = 20;
Private int y = 30;
// Image Rotation Angle
Private int rotation = Sprite. trans_none;
Private image IMG;
Private image img1;
Public imagecanvas (){
Try {
IMG = image. createimage ("/test.png ");
} Catch (ioexception e ){
IMG = NULL;
}
}
Protected void paint (Graphics g ){
Img1 = image. createimage (IMG, 0, 0, IMG. getwidth (), IMG. getheight (), rotation );
// Re-draw the cloth
G. setcolor (255,255,255 );
G. fillrect (0, 0, getwidth (), getheight ());
// Draw an image
G. drawimage (IMG, 0, 0, 0 );
G. drawimage (img1, X, Y, 0 );
}
Protected void keypressed (INT keycode ){
Int gamecode = getgameaction (keycode );
Switch (gamecode ){
Case canvas. Up:
Y = Y-3;
If (Y <0) y = 0;
Break;
Case left:
X = X-3;
If (x <0) x = 0;
Break;
Case right:
X = x + 3;
If (x> getwidth ()-IMG. getwidth () x = getwidth ()-IMG. getwidth ();
Break;
Case down:
Y = Y + 3;
If (Y> getheight ()-IMG. getheight () y = getheight ()-IMG. getheight ();
Break;
Case game_a:
Rotation = Sprite. trans_rot90;
Break;
Case game_ B:
Rotation = Sprite. trans_rot180;
Break;
Case game_c:
Rotation = Sprite. trans_rot270;
Break;
Case game_d:
Rotation = Sprite. trans_none;
Break;
}
Repaint ();
}
Protected void keyreleased (INT keycode ){
}
Protected void keyrepeated (INT keycode ){
Int gamecode = getgameaction (keycode );
Switch (gamecode ){
Case canvas. Up:
Y = Y-3;
If (Y <0) y = 0;
Break;
Case left:
X = X-3;
If (x <0) x = 0;
Break;
Case right:
X = x + 3;
If (x> getwidth ()-IMG. getwidth () x = getwidth ()-IMG. getwidth ();
Break;
Case down:
Y = Y + 3;
If (Y> getheight ()-IMG. getheight () y = getheight ()-IMG. getheight ();
Break;
}
Repaint ();
}
}