How to move and rotate images with the buttons in j2s

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.