J2ME game stacked (entry) --- five daughter chess)

xiaoxiao2021-03-17  214

The current structure is as follows (part):

Start J2ME's main method you need to start MIDLET (must inherit MIDlet class)

FIVELOVE.JAVA

/ * * Created on 2005-5-7 * * TODO to Change the Template for this generated file go to * window - preferences - java - code style - code templates * / package com.leqi.midlet;

Import javax.microedition.lcdui.display; import javax.microedition.midlet.midlet; import javax.microedition.midlet.midletStateChangeException;

Import com.leqi.common.Constants;

/ ** * @Author Leqi * Create 2005-5-7 * * msn: lesqi1027@hotmail.com * * QQ: 26826880 * * mail: fixpo.le@gmail.com * * Todo to change the template for this generated Type Comment Go to * Window - Preferences - Java - Code Style - Code Templates * / Public Class Fivelove Extends MIDlet {

/ ** * * / private Display display; private Chessboard chessboard; private MainMenu mainMenu; public Fivelove () {super (); // TODO Auto-generated constructor stub display = Display.getDisplay (this); mainMenu = new MainMenu (this , Constants.main_menu_title, constants.list_implicit;}

/ * (Non-Javadoc) * @see javax.microedition.midlet.MIDlet # startApp () * / protected void startApp () throws MIDletStateChangeException {// TODO Auto-generated method stub display.setCurrent (mainMenu);}

/ * (non-javadoc) * @see javax.microedition.midlet.midlet # Pauseapp () * / protected void Pauseapp () {// Todo auto-generated method stub}

/ * (Non-Javadoc) * @see javax.microedition.midlet.MIDlet # destroyApp (boolean) * / protected void destroyApp (boolean arg0) throws MIDletStateChangeException {// TODO Auto-generated method stub

} Public Display getDisplay () {return this.display;} public void exitMIDlet () {try {destroyApp (true);} catch (MIDletStateChangeException e) {// TODO Auto-generated catch block e.printStackTrace ();} notifyDestroyed ( );}} In which the constructor is running only once when the program is started, so the initialization of the first interface is in the secondary process.

Public FIVELOVE () {Super (); // Todo Auto-generated constructor stub display = display.getdisplay (this); mainmenu = new mainmenu (this, constants.main_menu_title, constants.list_implicit);}

Avoid every initialization operation

In STARTAPP () is the way to call every time you enter. Repeat

As for another approach, it is all basic functions, such as naming!

-------------------------------------------------- --------------------

Interface used by the main menu

MainMenu.java

Need to inherit the List to implement the performance of the page, and implement CommandListener to press the button into

Row monitoring

/ * * Created on 2006-3-14 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / package com.leqi.midlet;

Import java.io.ioException;

import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition. LCDUI.Image; import javax.microedition.lcdui.imageItem; import javax.microedition.lcdui.list; import javax.microedition.lcdui.stringItem; import javax.microedition.lcdui.TextBox;

Import com.leqi.common.Constants;

/ ** * @Author leqi * crete 2006-3-14 * * msn: lesqi1027@hotmail.com * * QQ: 26826880 * * mail: fixpo.le@gmail.com * * Todo to change the template for this generated Type Comment Go to * Window - Preferences - Java - Code Style - CDE Templates * / Public Class Mainmenu Extends List Immandlistener {

private Command exit; private Command menu; private Command help; private Command start; private Command back; private TextBox helpMesg; private Form formHelp; private StringItem content; private Image image; private ImageItem imageItem; private Fivelove gobang; private Chessboard chessboard; public MainMenu (FIVELOVE GOBANG, STRING STR, INT I) {Super (STR, I); this.gobang = Gobang; Exit = New Command ("exit", command.exit, 1); start = new command ("start", Command .Ok, 1); Back = New Command ("back", command.back, 2); help = new command ("info", command.help, 3); FormHelp = New form ("info"); content = new StringItem ( "", Constants.MAIN_HELP); try {image = Image.createImage (Constants.MAIN_IMAGE); imageItem = new ImageItem (null, image, ImageItem.LAYOUT_BOTTOM | ImageItem.LAYOUT_CENTER, "My Image"); formHelp.append (Content); formHelp.append (imageItem); formHelp.addCommand (back);} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace ();} this.append (Constants.FIVE_LOVE, null); this.append (Constants.fight_lord, null); this.addcommand (help); this.addcommand (start); this.addcommand (exit); FormHelp.setCommandListener (this); setcommandlistener (this);} / ** @Param arg0 * @Param Arg1 * / Public MainMenu (String Str, INT I) {Super (STR, I); // Todo Auto-generated constructor stub}

/ ** * @Param arg0 * @Param arg1 * @Param arg2 * @Param arg3 * / public mainmenu (string arg0, int arg1, string [] arg2, image [] arg3) {super (arg0, arg1, arg2, arg3 ); // TODO Auto-generated constructor stub} / * (non-Javadoc) * @see javax.microedition.lcdui.CommandListener # commandAction (javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable) * / public void commandAction (Command command, Displayable displayable) {// TODO Auto-generated method stub if (command == start) {if (this.getSelectedIndex () == 0) {chessboard = new Chessboard (gobang); Display.getDisplay ( GoBang) .SetCurrent (Chessboard);}} else if (command == exit) {gobang.exitmidlet ();} else if (command == help) {Display.getDisplay (Gobang) .Setcurrent (FormHelp);} else IF (Command == BACK) {Display.getDisplay (Gobang) .Setcurrent (this);}

}

}

-------------------------------------------------- ----------------------------

Five-child chess game main interface

Chessboard.java

This class inherits the Cavans class, with the page with a low-level picture processing, at the same time

CommandListener interface, listens on the operations on the page

/ * * Created on 2005-5-7 * * TODO to Change the Template for this generated file go to * window - preferences - java - code style - code templates * / package com.leqi.midlet;

// import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; 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.TextBox; import javax.microedition.midlet.MIDletStateChangeException ; // import javax.microedition.rms.RecordStore; // import javax.microedition.rms.RecordStoreException; // import javax.microedition.rms.RecordStoreNotFoundException; import com.leqi.chess.Chessman; import com.leqi.chess. Judgeover; import com.leqi.chess.recordstorechesman; import com.leqi.common.Constants;

/ ** * @Author Leqi * Create 2005-5-7 * * msn: lesqi1027@hotmail.com * * QQ: 26826880 * * mail: fixpo.le@gmail.com * * Todo to change the template for this generated Type Comment go to * window - preferences - java - code style - code templates * / public class chessboard extends canvas imports commandListener {

/ ** * * /

PRIVATE COMMAND EXIT;

PRIVATE COMMAND START;

PRIVATE COMMAND STOP;

PRIVATE COMMAND MENU;

PRIVATE COMMAND CANCLE;

Private command back;

Private command help;

Private textbox helpmesg;

PRIVATE FIVELOVE GOBANG; Private Alert alert;

/ * * Coordinate point on the board * /

Private INT Height = GetHeight ();

Private int width = getwidth ();

Private int a = height / 13; private int b = a * 2; private int c = a * 3; private int d = a * 4; private int E = a * 5; private int f = a * 6; private int G = a * 7; private int h = a * 8; private int = a * 9; private int j = a * 10; private int k = a * 11; private int L = a * 12; private int a = Width / 13; private int b = a * 2; private int c = a * 3; private int = a * 4; private int E = a * 5; private INT f = a * 6; private int g = a * 7; private int h = a * 8; private int = a * 9; private INT j = a * 10; private int k = a * 11; private int L = a * 12;

/ * * The landscape, longitudinal, cell radius and diameter A are the diameter of the lateral diameter A as longitudinal diameter * / private int RW = A / 2;

Private int RH = a / 2; private int initx = g; private int inity = f; private bolean fire = false; // Indicates whether the button is activated private bolean overce = true; / / indicates whether it is performed / * * false Representing Black * True represents chalk * / private boolean color = false; // private int index_x; // private int index_y; public shssboard (firelove gobang) {super (); this.gobang = Gobang; exit = new Command "Exit", command.exit, 1); stop = new command ("stop", command.stop, 2); back = new command ("back", command.back, 2); can = new command ("CANCLE ", Command.cancel, 2); help = new command (" Help ", command.help, 3); helpmesg = new textbox (" online help ", constants.chessman_help, 81, 0); helpmesg.addcommand (back) ; RecordStoreChessman.deleteChessman (); this.addCommand (stop); this.addCommand (cancle); this.addCommand (help); this.addCommand (exit); setCommandListener (this); helpMesg.setCommandListener (this); // TODO Auto-generated constructor stub}

/ * * (Non-Javadoc) * * @see javax.microedition.lcdui.Canvas # paint (javax.microedition.lcdui.Graphics) * / protected void paint (Graphics graphics) {// TODO Auto-generated method stub graphics. SetColor (213, 197, 170); Graphics.FillRect (0, 0, Width, Height); Graphics.SetColor (0, 0, 0); Graphics.drawline (A, A, L, A); Graphics.Drawline A, B, L, B); Graphics.drawline (A, C, L, C); Graphics.drawline (A, D, L, D); Graphics.drawline (A, E, L, E); Graphics. Drawline (A, F, L, F); Graphics.drawline (A, G, L, G); Graphics.Drawline (A, H, L, H); Graphics.drawline (A, I, L, I); Graphics.drawline (A, J, L, J); Graphics.drawline (A, K, L, K); Graphics.drawline (A, L, L, L); Graphics.drawline (A, A, A, L ); Graphics.drawline (B, A, B, L); Graphics.Drawline (C, A, C, L); Graphics.drawline (D, A, D, L); Graphics.Drawline (E, A, E , L); Graphics.drawline (F, A, F, L); Graphics.drawline (G, A, G, L); Graphics.Drawline (H, A, H, L); Graphics.Drawline (I, A , i, l); graphics.drawline (J, A, J, L); Graphics.drawline (K, A, K, L); Graphics.drawline (L, A, L, L); Graphics.SetColor (223, 37, 17); Graphics.DrawRect (INITX - RW, INITY- RH, A, A);

Chessman [] chessmans = recordstorechesman.read (); if (chessmans! = Null) {for (int x = 0; x

New Alert ("Game Over", "White Wins", NULL, ALERTTYPE.INFO; Alert.SetTimeout; Alert.dCommand (Exit); Gobang.getDisPlay (). setCurrent (alert);} // add to over color =! color; fire = false; overce = false;}}} / * * (non-javadoc) * * @see javax.microedition.lcdui.commandlistener # commandAction (javax.microedition.lcdui.command , * javax.microedition.lcdui.Displayable) * / public void commandAction (Command command, Displayable displayable) {// TODO Auto-generated method stub if (command == exit) {try {RecordStoreChessman.deleteChessman (); gobang.startApp ();} catch (MIDletStateChangeException e) {// TODO Auto-generated catch block e.printStackTrace ();}} else if (command == help) {Display.getDisplay (gobang) .setCurrent (helpMesg);} else if (Command == BACK) {Display.getDisplay (Gobang) .Setcurrent (this);} else if (command == stop) { Gobang.PauseApp ();}}

/ * * (Non-javadoc) * * @see javax.microedition.lcdui.canvas # keypressed (int) * / protected void keypressed (int key) {switch (getGameAction (key)) {copy canvas.up: inity = inity - a; if (inity L) {INITY = L;} Fire = False; overce = true; break; case canvas.left: initX = initX - a; if (INITX L) {INITX = L;} fire = false; overce = true; break; cascas.fire: fire = true; Break;} repaint ();}} ------------ -----------------------------------------

Class used to describe each point information of the chessboard

Chessman.java

Recommend four attributes, coordinates, the scene, the color of the chess, there is already a chess piece

/ * * Created on 2005-5-9 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * / package com.leqi.chess;

/ ** * @Author Leqi * Create 2005-5-9 * * msn: lesqi1027@hotmail.com * * QQ: 26826880 * * mail: fixpo.le@gmail.com * * Todo to change the template for this generated TYPE comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class Chessman {private int x; private int y; private boolean haveChess; / * * true white * false black * / private boolean color; / ** * @Return Returns the color. * / Public boolean ISCOLOR () {Return Color;} / ** * @Param Color The color to set. * / Public void setcolor (boolean color) {this.color = color;} . / ** * @return Returns the haveChess * / public boolean isHaveChess () {return haveChess;} / ** * @param haveChess The haveChess to set * / public void setHaveChess (boolean haveChess) {this.haveChess = haveChess.; } / ** * @Return Returns the x. * / Public int getX () {return x;} / ** * @Param x the x to set. * / Public void setx (int x) {this.x = x } / ** * @Return Returns the y. * / Public int game () {RE Turn Y;} / ** * @Param y to set. * / public void sety {this.y = y;}} ---------------- ----------------------

RecordStroechessman.java

This class calls the RecordStore class as a storage medium in the game, where

Viewed, store, close, delete.

/ * * Created on 2005-5-9 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * / package com.leqi.chess;

Import java.io.bytearrayinputstream; import java.io.byteArrayoutputstream; import java.io.DataInputStream; import java.io.Dataoutputstream; import java.io ioException;

import javax.microedition.rms.InvalidRecordIDException; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; import javax.microedition.rms.RecordStoreFullException; import javax.microedition.rms.RecordStoreNotFoundException; import javax.microedition. RMS.Recordstorenotopenexception; import com.leqi.common.constants;

/ ** * @Author Leqi create 2005-5-9 * * msn: lesqi1027@hotmail.com * * QQ: 26826880 * * mail: fixpo.le@gmail.com * * Todo to change the template for this generated type comment Go to WinDow - * Preferences - Java - Code Style - Code Templates * / Public Class RecordStorechesman {

Private static recordstore recordingstore = null;

/ ** * @param chessman * / public static void storeChessman (Chessman chessman) {try {recordstore = RecordStore.openRecordStore (Constants.CHESSMAN_STORE, true);} catch (RecordStoreFullException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (RecordStoreNotFoundException e) {// TODO Auto-generated catch block e.printStackTrace ();} catch (RecordStoreException e) {// TODO Auto-generated catch block e.printStackTrace ();}

bTE [] OutputRecord;

BYTEARRAYOUTPUTSTREAM OUTPUTSTREAM = New byteArrayoutputStream (); DataOutputStream OutputDataStream = New DataOutputStream (OutputStream);

try {outputDataStream.writeInt (chessman.getX ()); outputDataStream.writeBoolean (chessman.isHaveChess ()); outputDataStream.writeInt (chessman.getY ()); outputDataStream.writeBoolean (chessman.isColor ());

outputDataStream.flush (); outputRecord = outputStream.toByteArray (); recordstore.addRecord (outputRecord, 0, outputRecord.length); outputStream.reset (); outputStream.close (); outputDataStream.close (); recordstore.closeRecordStore () ;} catch (IOException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (RecordStoreNotOpenException e) {// TODO Auto-generated catch block e.printStackTrace ();} catch (RecordStoreFullException e) { // Todo auto-generated catch block e.printStackTrace ();} catch (recordstoreException e) {// Todo auto-generated catch block e.printStackTrace ();}}

public static void closeChessman () {try {recordstore.closeRecordStore ();} catch (RecordStoreNotFoundException e) {// TODO Auto-generated catch block e.printStackTrace ();} catch (RecordStoreException e) {// TODO Auto-generated catch Block E.PrintStackTrace ();}}

public static void deleteChessman () {if (! RecordStore.listRecordStores () = null) {try {RecordStore.deleteRecordStore (Constants.CHESSMAN_STORE);} catch (RecordStoreNotFoundException e) {// TODO Auto-generated catch block e.printStackTrace () } Catch (recordstoreExcection e) {// Todo auto-generated catch block E.PrintStackTrace ();}}}}}}}}}}}}

public static Chessman [] read () {try {recordstore = RecordStore.openRecordStore (Constants.CHESSMAN_STORE, true);} catch (RecordStoreFullException e) {// TODO Auto-generated catch block e.printStackTrace ();} catch (RecordStoreNotFoundException e ) {// TODO Auto-generated catch block e.printStackTrace ();} catch (RecordStoreException e) {// TODO Auto-generated catch block e.printStackTrace ();} byte [] byteInputData = new byte [10]; ByteArrayInputStream InputStream = New ByteArrayInputStream (ByteinputData); DataInputStream InputDataStream = New DataInputStream (InputStream);

try {Chessman [] chessmans = null; int num = recordstore.getNumRecords (); if (num <1) {return chessmans;} else {chessmans = new Chessman [num]; int tmpX; int tmpY; boolean tmpHaveChess; boolean tmpColor For (int x = 1; x <= Num; x ) {chessman chessman = new chessman (); recordstore.getRecord (x, byteinputdata, 0);

TMPX = INPUTDATASTREAM.READINT (); tmphaveches = inputDataStream.readBoolean (); tmpy = inputdatastream.readint (); tmpcolor = inputdatathastream.readboolean ();

InputStream.reset (); Chessman.SetX (TMPX); Chessman.SetHaveches; Chessman.Sety (TMPY); chessman.setcolor; chessmans [x - 1] = chessman;} inputstream.close (); inputDataStream.close (); recordstore.closeRecordStore (); return chessmans;}} catch (RecordStoreNotOpenException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (InvalidRecordIDException e) {// TODO Auto-generated catch block e.printStackTrace ();} catch (RecordStoreException e) {// TODO Auto-generated catch block e.printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace (); } Return NULL;}

}

-----------------------------------

Judgeover.java

Such, used to determine whether the current situation has met the wins, which needs to be judged from eight directions.

/ * * Created on 2005-5-10 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / package com.leqi.chess;

/ ** * @Author leqi * crete 2005-5-10 * * msn: lesqi1027@hotmail.com * * QQ: 26826880 * * mail: fixpo.le@gmail.com * * Todo to change the template for this generated TYPE Comment Go to * Window - Preferences - Java - Code Style - Code Templates * / Public Class Judgeover {

/ ** * Decision does it have formed five sub-* @Param chessmans * @Param chessman * @Param a * @Param a * @return * / private byte judgeup (Chessman [] Chessmans, Int x, int y, boolean Color , Int A, Int A, Byte Num) {for (INT Z = 0; Z

/ ** * Judging whether the left has formed a five sub-* @Param Chessmans * @Param Chessman * @Param A * @Param a * @return * / private byte judgeleft (Chessman [] Chessmans, Int x, int y, boolean color , Int A, Int A, Byte Num) {for (INT Z = 0; Z

/ ** * Judgment Whether to form a five sub-@ @Param chessmans * @Param chassman * @param a * @Param a * @return * / private byte judgeupleft (Chessman [] Chessmans, int X, int y, boolean color , Int A, Int A, Byte Num) {for (INT Z = 0; Z

/ ** * Judging whether there is a five sub-* @Param Chessman * @Param Chessman * @ @Param A * @param a * @Param A * @Return * / Private Byte JudgeUpright (Chessman [] chessmans, int x, int y, boolean color , Int A, Int A, Byte Num) {for (INT Z = 0; Z

/ ** * * @Param chessmans * @Param chessman * @Param a * @Param a * @Return 0 representative did not divide the winning and negative 1 generation Black Basheng 2 represents white square wins * /

Public by Judgement (Chessman) Chessmans, Chessman Chessman, Int A, Int a) {int x = chessman.getx (); int y = chessman.gety (); boolean color = insman.iscolor (); byte num = 1 BYTE TOTLEUPDOWN = 1; Byte Totleleri = 1; Byte TotleleUprido = 1; Byte TotleriUpledo = 1; IF ((TotleUpdown = Judgeup (Chessmans, X, Y, Color, A, A, TotleUpdown) == 5 || Judgedown Chessmans, X, Y, Color, A, A, TotleUpdown == 5 || (Totleleri = Judgelef (Chessmans, X, Y, Color, A, A, Totleleri) == 5 || Judgeright (Chessmans, X, Y, Color, A, A, Totleleri == 5 || (Totleleuprido = JudgeUpleft (Chessmans, X, Y, Color, A, A, TotleleUprido) == 5 | | JudgedownRight (Chessmans, X, Y, Color, A, a, Totleleuprido) == 5 || (Totleriupledo = JudgeuPright (Chessmans, X, Y, Color, A, A, TotleriUpledo) == 5 || JudgedownLeft (Chessmans, X, Y, Color, A, A, TotleriUpledo) == 5) {if (color) {return 2;} else {return 1;}} else {return 0;}}} ------------------- --------- -------

GameClock.java

Such, used to make the game time control. This method has not been called now, and it will be called later.

Need to control the Java.util.Timertask class

/ * * Created on 2006-3-16 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / package com.leqi.common;

Import java.util.timertask;

/ ** * @Author leqi * crete 2006-3-16 * * msn: lesqi1027@hotmail.com * * QQ: 26826880 * * mail: fixpo.le@gmail.com * * Todo to change the template for this generated TYPE Comment Go to * Window - Preferences - Java - Code Style - Code Templates * / Public Class GameClock Extends Timertask {

private int timeLeft = 60; // default time private boolean pause = false; / ** * * / public GameClock () {super (); // TODO Auto-generated constructor stub} public GameClock (int timeLeft) {this.timeLeft = timeLeft;.} / ** * @return Returns the timeLeft * / public int getTimeLeft () {return timeLeft;} / ** * @param timeLeft The timeLeft to set * / public void setTimeLeft (int timeLeft) {this.. Timeleft = Timeleft;} / * (non-javadoc) * @see java.lang.Runnable # run () * / public void run () {// Todo auto-generated method stub if (! pause) {Timeleft -; }} Public void pause () {pause = true;} public void resume () {pause = false;

}

转载请注明原文地址:https://www.9cbs.com/read-129688.html

New Post(0)