Java program of horse racing game

This is the time to do a little game, probably after the program starts, the user will enter the login screen, enter your user name and password, select Log in horse racing will enter into the interface, then you can see the racecourse and the number of horses the user can select any of a number of horses to bet, enter a valid bet amount (initial amount because the system is set up, it should not bet more than the initial amount). After the betting is completed the user can choose to start the game, the user must not carry out any operations during the race, after the race, you can re-new round of horse racing, the same method.

Program flow chart:

A, horse racing module
wherein the module comprises a circuit portion and to draw horses, horses, and to add to the track by using the Draw category Jpanel panel.
Part of the code:
class the Draw the extends the JPanel
{
int X = 0;
String S;
int W, H;
public void Paint (Graphics G)
{
, super.paint (G);
this.setBackground (Color.WHITE);
W = this.getWidth ();
H = this.getHeight ();
g.setColor (as Color.BLACK.);
g.drawLine (66, H / 2-44,666, H / 2-44);
g.drawLine (66, H / 2 + 40,666 , H / 2 + 40);
g.setColor (as Color.BLACK.);
g.drawLine (66,0,66, H);
g.setColor (Color.red);
g.drawLine (666,0,666, H);
g.drawRect (36 + x, h / 2-10,30,20); // show horses
g.setColor (Color.BLACK); // text display
g.drawString (s, 26, h / 2-12 );
}
}

Second, the bet areas module
a. Horse betting module
mainly analog implementation of horses and betting selection, radio button increase JRadioButton horses.

B. Bet Amount module
TextField input bet amount, compared to the total amount after receiving tfget.

C. Stake changing module
while controlling the amount of variable tz change.

Code:
public class the extends the JFrame the Run the implements the ActionListener
{
......
the JPanel P3; // bet area
JButton jb; // start button
the JFrame Frame;
the DateUtil the DateUtil new new D1 = ();
Boolean Cotrol; // change the bet variable
int no ; // ranking number
String s; // symbol betting events radio
JRadioButton b1, b2, b3, b4 ; // bet radio button
ButtonGroup bg; // note button, so that it is a radio
JTextArea ta; / / Jockey text display
JLabel money; // total amount bet label
TextField tf; // bet amount input window
int tz; // variable amount of bets change
String tfget; variable transfer the total amount of bets //
FlowLayout ly; / / layout variables
}
public void the actionPerformed (the ActionEvent E)
{
IF (bg.getSelection ()! = null)
{// bet radio buttons to confirm
if (e.getActionCommand () == "start")
{
Tfget tf.getText = ();
the try {
IF (the Integer.parseInt (tfget)> 0 && the Integer.parseInt (tfget) <= TZ) // String converted int
{// amount bet confirmation
ta.setText ( "the start of the race "+ '\ n-');
new new Racing (h1 of, the this) .start ();
new new Racing (H2, the this) .start ();
new new Racing (H3, the this) .start ();
new new Racing (H4, the this ) .start ();
jb.setText ( "restart"); // Racing key label change
b1.setEnabled (false); // after the race starts, inhibit operation
b2.setEnabled (to false);
b3.setEnabled (to false) ;
b4.setEnabled (to false);
tf.setEditable (to false);
jb.setEnabled (to false);
}
the else
{
JOptionPane.showMessageDialog (null, "bet amount error, please place your bet");// Information dialog
}
}
the catch (Exception EX)
{
JOptionPane.showMessageDialog (null, "betting format error, please re-bet");
}
}
}
the else
{
JOptionPane.showMessageDialog (null, "please place your bet Pick");
}
three thread starts and an end module
. A thread start and end some modules
module code:
public void the actionPerformed (the ActionEvent E)
{
......
ta.setText ( "game started" + '\ n-');
new new Racing (h1 of, the this) .start ();
new new Racing (H2, the this) .start ();
new new Racing (h3, the this) .start ();
new new Racing (h4, the this) .start ();
jb.setText ( "restart"); // Jockey key label change
b1.setEnabled (false ); // after the race starts, inhibit operation
b2.setEnabled (to false);
b3.setEnabled (to false);
b4.setEnabled (to false);
tf.setEditable (to false);
jb.setEnabled (to false);
}
......
IF (e.getActionCommand () == "restart")
{// restart, reset the corresponding control
RESTAR (h1 of, the this);
RESTAR (H2, the this);
RESTAR (H3, the this);
RESTAR (H4, the this);
Cotrol = to true;
NO = 0;
ta.setText ( "");
tf.setEditable (to true);
b1.setEnabled (to true);
b2.setEnabled (to true);
b3.setEnabled (to true);
b4.setEnabled (to true);
jb.setText ( "start");
}
}
class Racing the extends the thread
{// thread motion control horses
the Draw a;
the Run R & lt;
the DateUtil the DateUtil new new D = ();
Racing (the Draw H, the Run B)
{
this.a = H;
this.r = B;
}

B. The initial match the system time acquisition module
initial thread run start time, by using the Calendar class to get the system time at the start of the game, using getNow_HMS () method to get the game.
Code Module:
Import Classes in java.util. ;
Public class the DateUtil {
public static String getNow_M () {
Calendar Calendar.getInstance C = ();
String = String.valueOf minute (c.get (Calendar.MINUTE));
IF (minute. length () ==. 1) {
minute = "0" + minute;
}
String MSl = minute;
return MSl;
}
public static String getNow_S () {
Calendar Calendar.getInstance C = ();
String SECOND String.valueOf = (C .get (Calendar.SECOND));
IF (second.length () ==. 1) {
SECOND = "0" + SECOND;
}
String = SECOND MS2;
return MS2;
}
public int getNow_HMS(){
String s1,s2;
s1=getNow_M();
s2=getNow_S();
int a=Integer.parseInt(s1)
60+Integer.parseInt(s2);
return a;
}
public static void main(String[] args) {
DateUtil d;
d=new DateUtil();
d.getNow_HMS();
}
}
public int Time(){
int oo=d1.getNow_HMS();
return oo;
}

C. acquisition time of end of the game system module
when the end of the game, when the thread terminates, to obtain the system time at the end of the game by using the Calendar class, wherein getNow_HMS () method to get the end time of the game.
Module code:
public void RUN ()
{int T;
int O, Y;
O = r.Time ();
......
Y = d.getNow_HMS ();
int Z = YO;
ta.append ( "when used:" + z + "s" + '\ n-');
}

Fourth, the game result display area modules
in a spatial distribution JTextArea (), append () displayed by the process of ranking horses, and other information used.

Fifth, the horse racing game run results

Guess you like

Origin www.cnblogs.com/jingxinbk/p/12416402.html