J2's simple drawing example

Source: Internet
Author: User

Main class MIDlet:
Package com. lvke. wuwu;

Import javax. microedition. lcdui. display;
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;

Public class pointexample extends MIDlet {

Display display;
Myclass canvas;
 
Public pointexample (){
Display = display. getdisplay (this );
Canvas = new myclass (this );
}
Protected void Startapp () throws midletstatechangeexception {
Display. setcurrent (canvas );
}
Protected void destroyapp (Boolean B) throws midletstatechangeexception {

}

Protected void pauseapp (){

}

Public void midletexit (){
Try {
This. destroyapp (false );
} Catch (midletstatechangeexception e ){
E. printstacktrace ();
}
This. policydestroyed ();
}

}
 

Function implementation class:

Package com. lvke. wuwu;

Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. commandlistener;
Import javax. microedition. lcdui. displayable;
Import javax. microedition. lcdui. graphics;

Public class myclass extends canvas implements commandlistener {

Private command exit;
Private command arase;
Private Boolean araseflag = false;
Private Boolean isfirstpaint;
Private int SX = 0, Sy = 0, Cx = 0, Cy = 0;
Private pointexample point;
Public myclass (pointexample point ){
This. Point = point;
Exit = new command ("exit", command. Exit, 1 );
Arase = new command ("arase", command. screen, 1 );

This. addcommand (exit );
This. addcommand (arase );
This. setcommandlistener (this );
Isfirstpaint = true;

}
Public void commandaction (command C, displayable d ){
If (C = exit ){
Point. midletexit ();
} Else if (C = arase ){
Araseflag = true;
Repaint ();
}
}

Protected void paint (Graphics g ){
If (araseflag | isfirstpaint ){
G. setcolor (255,255,255 );
G. fillrect (0, 0, this. getwidth (), this. getheight ());
Araseflag = isfirstpaint = false;
SX = 0; Sy = 0; Cx = 0; Cy = 0;
Return;
}
G. setcolor (255, 0, 0 );
System. out. println ("SX =" + SX + ": Sy =" + Sy + ": Cx =" + cx + ": Cy =" + cy );
G. drawline (sx, Sy, CX, CY );
SX = Cx;
Sy = Cy;
}
// Trigger an event when you press the mouse or click the screen
Protected void pointerpressed (int x, int y ){
SX = X;
Sy = y;
}

// This event is triggered when you drag the mouse or draw a screen.
Protected void pointerdragged (int x, int y ){
Cx = X;
Cy = y;
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.