Today, I wrote a driver genie, and a few lines of code are complete. Although simple, it is very useful, many games developed using java and android use more or less the simple code.
Packagecom. csdn. code;
Importjava. awt. Color;
Importjava. awt. Frame;
Importjava. awt. Graphics;
Importjava. awt. event. WindowAdapter;
Importjava. awt. event. javaswevent;
Public classDriveElf extends Frame {
Private int x = 40; // the initial position of the genie on the canvas-abscissa
Private int y = 40; // the initial position of the genie on the canvas-ordinate
Private int arc = 320; // angle of combination of Sprite mouth
Public DriveElf (){
SetBounds (200,200,600,500 );
SetVisible (true );
SetBackground (Color. cyan );
AddWindowListener (new WindowAdapter (){
Public voidwindowClosing (WindowEvent e ){
System. exit (0 );
}
});
New Thread (){
Public void run (){
While (true ){
// Two for loops are used to move the genie on the canvas.
For (inti = 0; I <40; I ++ ){
Arc + = 1;
X + = 10;
Y + = 3;
}
Try {
Thread. sleep (50 );
} Catch (effectione ){
E. printStackTrace ();
}
Repaint ();
For (inti = 0; I <40; I ++ ){
Arc-= 1;
X-= 8;
Y-= 1;
Try {
Thread. sleep (100 );
} Catch (effectione ){
E. printStackTrace ();
}
Repaint ();
}
}
}
}. Start ();
}
// Spray the painting method for drawing on the drawing board
Public void paint (Graphics g ){
G. setColor (Color. red );
G. fillArc (x, y, 90, 90, 0, arc); // draw the sprite body
G. setColor (Color. gray );
G. fillArc (x + 50, y + 10, 20, 20, 0,360); // draw the sprite
G. setColor (Color. black );
G. fillArc (x + 56, y + 15, 10, 10, 0,360); // draw an eye ball
}
Public static void main (String [] args ){
New DriveElf ();
}
}