Code explanation for the entire process of rapid development of the j2e-based jigsaw puzzle game (6) -- welcome interface, wait interface after the game ends, and add username word Interface

Source: Internet
Author: User

Welcome Page,

Code:

/*
* Created on 2004-6-20
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Package cn.edu. xtu. tilepuzzle. UI;

Import cn.edu. xtu. tilepuzzle. gamedb;
Import java. Io. ioexception;

Import javax. microedition. lcdui .*;

/**
* @ Author p2800
*
* Todo to change the template for this generated type comment go to window-
* Preferences-Java-code style-code templates
*/
Public class welcomeui extends canvas
{

Public int width;
Public int height;
// Private image;
// Private string message = database. Author;
Private string message = gamedb. Author;
Private string school = gamedb. School;

Public welcomeui ()
{
This. setfullscreenmode (true );
Width = This. getwidth ();
Height = This. getheight ();
// System. Out. println (width + "," + height );
// Image = createimage ("/welcome.png ");

}

Public Image createimage (string name)
{
Image image = NULL;
Try
{
Image = image. createimage (name );
}
Catch (ioexception E)
{

}
Return image;
}

Public void setmessage (string message)
{
This. Message = message;
This. repaint ();
}

Public void setschool (string School)
{
This. School = School;
This. repaint ();
}

/*
* (Non-javadoc)
*
* @ See javax. microedition. lcdui. displayable # Paint (javax. microedition. lcdui. Graphics)
*/
Protected void paint (Graphics g)
{
// System. Out. println ("welcomeui start plotting ......");
// Todo auto-generated method stub
// Arg0.setcolor (128,128,255 );
// Arg0.drawrect (5, 5, width-10, height-10 );
/*
If (image! = NULL)
{
Arg0.drawimage (image, (width-image.getWidth ()/2,
(Height-image.getHeight ()/2, graphics. Top | graphics. Left );
}
*/
G. setcolor (255,255,190 );
G. fillrect (0, 0, width, height );

G. setcolor (255, 0, 0 );
G. setfont (font. getfont (font. face_system, Font. style_bold, Font. size_large ));
If (message! = NULL)
{
G. drawstring (message, width/2, height/3, graphics. Bottom | graphics. hcenter );
}
If (school! = NULL)
{
G. drawstring (school, width/2, height * 2/3, graphics. Bottom | graphics. hcenter );
}

}

}

After the game ends,

Code:

Package cn.edu. xtu. tilepuzzle. UI;

Import java. util .*;
Import javax. microedition. lcdui .*;

Import cn.edu. xtu. tilepuzzle. gamedb;
Import cn.edu. xtu. tilepuzzle. contorler. mainuicontroller;
Import cn.edu. xtu. tilepuzzle. model. boardmodel;

Public class waitcanvasui extends canvas implements commandlistener {

Mainuicontroller;
Display display;
Boardmodel;
Displayable;

Private int mcount, mmaximum;
Private int minterval;

Private int mwidth, mheight, MX, my, mradius;
Private string mmessage;
Command OK;
Command cancel;

Public waitcanvasui (mainuicontroller, boardmodel, display, displayable dB ){
This. boardmodel = boardmodel;
This. Display = display;
This. displayable = dB;
This. mainuicontroller = mainuicontroller;

Mcount = 0;
Mmaximum = 36;
Minterval = 100;

Mwidth = getwidth ();
Mheight = getheight ();

// Calculate the radius.
Int halfwidth = (mwidth-mradius)/2;
Int halfheight = (mheight-mradius)/2;
Mradius = math. Min (halfwidth, halfheight );

// Calculate the location.
MX = halfwidth-mradius/2;
My = halfheight-mradius/2;

OK = new command ("add", command. OK, 1 );
Cancel = new command ("return", command. Cancel, 0 );
This. addcommand (OK );
This. addcommand (cancel );
Setcommandlistener (this );

}

Public void setflag (Boolean run ){
// Create a timer to update the display.
If (run ){
Timertask task = new timertask (){
Public void run (){
Mcount = (mcount + 1) % mmaximum;
Repaint ();
}
};
Timer timer = new timer ();

Timer. Schedule (task, 0, minterval );
}
}

Public void paint (Graphics g ){
Int Theta =-(mcount * 360/mmaximum );

// Clear the whole screen.
G. setcolor (255,255,190 );
G. fillrect (0, 0, mwidth, mheight );

// Now draw the pinwheel.
G. setcolor (128,128,255 );
G. drawarc (MX, my, mradius, mradius, 0,360 );
G. fillarc (MX, my, mradius, mradius, theta + 90, 90 );
G. fillarc (MX, my, mradius, mradius, theta + 270, 90 );

// Draw the message, if there is a message.
If (mmessage! = NULL ){
G. drawstring (mmessage, mwidth/2, mheight, graphics. Bottom
| Graphics. hcenter );
}

G. setcolor (255, 0, 0 );

G. setfont (gamedb. monospace_bold_large_font );
// Draw a rectangle that fills the full screen color of the mobile phone
// G. fillrect (0, 0, getwidth (), getheight ());
// System. Out. println ("screen width =" + getwidth () during victory ());
// System. Out. println ("congratulations, pass !, String width = "+ tempfont. stringwidth (" congratulations, pass! "));

G. drawstring (boardmodel. cheated? "Cheater! ":" Congratulations, you have passed! "), (Getwidth ()-gamedb. monospace_bold_large_font.stringwidth (" Congratulations, you're done! ")/2, getheight () * 1/12, graphics. Left | graphics. Top );

G. setfont (gamedb. monospace_bold_medium_font );
// System. Out. println ("in total:" + boardmodel. sumttime );
String timestring = "in total:" + boardmodel. gettimestringbys (boardmodel. sumtime );
G. drawstring (timestring, (getwidth ()-gamedb. monospace_bold_medium_font.stringwidth (timestring)/2, getheight () * 2/12, graphics. Left | graphics. Top );

G. setfont (gamedb. monospace_plain_medium_font );
String messagestring = "do you need to add your name to the game ranking? ";
G. drawstring (messagestring, (getwidth ()-gamedb. monospace_plain_medium_font.stringwidth (messagestring)/2, getheight () * 10/12, graphics. Left | graphics. Top );
}

Public void commandaction (command C, displayable arg1 ){
If (C = OK ){
// System. Out. println ("OK ");
Addusernameui = new addusernameui (mainuicontroller, boardmodel, display, displayable );
Display. setcurrent (addusernameui );
// Mainuicontroller. handlecommand_id (menuidx );
} Else if (C = Cancel ){
// System. Out. println ("cancel ");
This. display. setcurrent (mainuicontroller. mainmenuui );
// Mainuicontroller. handlecommand_id (6 );
}

}

}

Added the username text interface,

Code:

Package cn.edu. xtu. tilepuzzle. UI;

Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. commandlistener;
Import javax. microedition. lcdui. display;
Import javax. microedition. lcdui. displayable;
Import javax. microedition. lcdui. graphics;
Import javax. microedition. lcdui. image;
Import javax. microedition. lcdui. textbox;
Import javax. microedition. lcdui. textfield;

Import cn.edu. xtu. tilepuzzle. gamedb;
Import cn.edu. xtu. tilepuzzle. contorler. mainuicontroller;
Import cn.edu. xtu. tilepuzzle. model. boardmodel;
Import cn.edu. xtu. tilepuzzle. model. recordstoreoperations;

Public class addusernameui extends canvas implements commandlistener {
Mainuicontroller;
Display display;
Boardmodel;
Displayable dB;
Image image;
Textbox nametextbox;

Private command OK;
Private command cancel;

Public addusernameui (mainuicontroller, boardmodel, display, displayable dB ){
// This. setfullscreenmode (true );
This. boardmodel = boardmodel;
This. Display = display;
This. DB = dB;
This. mainuicontroller = mainuicontroller;
Init ();
}

Public void Init (){

Nametextbox = new Textbox ("enter your name", null, 255, textfield. Any );

OK = new command ("OK", command. OK, 1 );
Cancel = new command ("quit", command. Cancel, 0 );

Nametextbox. addcommand (OK );
Nametextbox. addcommand (cancel );
Nametextbox. setcommandlistener (this );

}

Protected void paint (Graphics g ){
This. display. setcurrent (nametextbox );
}

Public void commandaction (command C, displayable dB ){
If (C = OK ){
Try {
String strstring = nametextbox. getstring () + ";" + (boardmodel. columns + "X" + boardmodel. rows ). tostring () + ";" + boardmodel. sumtime/1000;
Recordstoreoperations. addstrtorecordstroe (strstring, gamedb. recordstorepeopleresult );
Mainuicontroller. showpeopleinfolistui. INIT ();
Mainuicontroller. showpeopleinfolistui. repaint ();
This. display. setcurrent (mainuicontroller. mainmenuui );
} Catch (exception e ){
E. printstacktrace ();
}

} Else if (C = Cancel ){
This. display. setcurrent (mainuicontroller. mainmenuui );
// Mainuicontroller. handlecommand_id (6 );
// System. Out. println ("cancel ");
}
}

}

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.