Object-oriented design programming

Source: Internet
Author: User

Object-Oriented design method: The basic idea of Java object-oriented programming is to solve problems by constructing and organizing objects. Objects are good, and any object can be considered an object, for example, a car is an object. The object has two most basic properties: it has certain material foundation and certain function, these two basic attributes in the Java language to have certain storage unit and have certain function. After understanding the basic idea of the object-oriented programming of Java, in the later programming process, we should construct an object that people can understand more closely to the real physical world object. The Java object-oriented programming process is very simple. Here is a top-down approach to programming, where you first construct a class to handle the problems that need to be addressed, and then analyze and construct new classes based on the classes that you already have, such as the classes that were implemented before solving their problems, and try to break down the problem, and the classes that were initially constructed are organized by organizing these newly constructed classes, Existing classes and the instances generated by them to solve the specified problem. This process can continue recursively, using the newly constructed class until finally solving the specified problem, such as a Java application and a small application that must have a class as the entry to solve the problem. In computer language, object-oriented classes generally have three basic features: encapsulation, inheritance and polymorphism, these three features further simplify the Java solution Model, improve the reusability of the program, save the Code of the program, thereby improving the productivity of the software.

The chess plate consists of nine lines and 10 horizontal line crosses, the board has a total of 90 intersections, the chess is placed and activities at these intersections.

There is no line in the middle of the chessboard, called the "River", where the intersection line is called "nine". Nine lines, red from right to left with Chinese numerals one to Jiulai, black chess from right to left with Arabic numerals 1 to 9来 expressed. A total of 32 pieces, divided into red, black two groups, each group of 16, each sub-seven, its name and number are as follows: Red pieces: Handsome one, car, horse, cannon, phase, two, five soldiers. Black Pawn: Will be one, the car, horse, Cannon, elephant, two of each, five strokes.

In the game, by the Red Chess party first go, the two sides take turns each, until the victory or draw.

One side of the moves, a chess piece from one intersection to another empty intersection, or eat each other's pieces and occupy the fork point, are counted away. Each side walked one, called a round. Handsome (will) each with only one step, forward, backward, traverse can be, but not out of the "nine". Handsome and will not be allowed in the same straight line directly opposite, if one party has occupied first, the other party must avoid. Every one of the only allowed along the "nine" slash step, can go back. Phase (elephant) can not cross the "river", each oblique walk two steps, can go back, that is commonly known as phase (like) to walk the word. When there are different pieces in the center of the word, it is commonly known as the eye of the plug-phase. The horse every walk has been (or a horizontal) a slant, can go back, that is commonly known as "horse Walking day word." If you want to go in the direction of a long (or a horizontal) where, the other pieces block, commonly known as "limp horse leg", you can not go past. Each car can be straight into, straight back, cross-walk, unlimited number of steps. When the cannon does not eat the son, walk the same way as the car. Soldiers (strokes) before the "river", each with only one step forward straight, after the "river", each can take a step forward, but also can walk a step, but can not retreat. When walking a chess, if the pawn can go to the position of the other pieces exist, you can eat the pieces and occupy that position. Only the cannon eat must be separated by a pawn (regardless of which side of the) jump eat, that is commonly known as "puffing out septum." In addition to handsome (will), other pieces can be allowed to eat, or take the initiative to eat. One side's pawn attacks the other's handsome (will), and the next to eat it, called "General", or simply "will". The "General" party must immediately "shall", that is, the act of self-defence to dissolve the "will" state. If the "general" cannot be "should", even if the "will die". Turn to the side of the moves, handsome (will) although not the other "general", but is forbidden in a position no way to go, while other pieces of their own can not move, even if be "trapped."

a . Detailed Design

Chesspoint.java

The class generated by the file is responsible for creating a chess point object. The chess point object can judge whether there is a pawn on the point, specify the chess piece on the current point, get the chess piece on the current point, and move the chess piece on the current point.

Chesspiece.java

The class generated by the file is responsible for creating the pawn object. and draw the appearance of the pieces. A pawn object can get its own color, size, name, and can also set and get its own category, namely Red Chess and black chess.

Makechessmanual,java

The class generated by the file is responsible for creating the object that records the game, which can record the game into a linked table and display the game in a text area. When the undo, the object will modify the game and update the text area information.

Ruler.java

The class generated by the file is responsible for creating the "moves Rule" object, which is responsible for whether the user's user's walk conforms to the rules.

Demon.java

The class generated by the file is responsible for creating a "game Demo" object that can demonstrate the saved game.

Movestep.java

The object created by the class records a chess step.

two. Main code

public class Chinesechess {
public static void Main (String args[]) {
New Chessmainframe ("Chinese chess");
}
}

Class Chessmainframe extends JFrame implements ActionListener, MouseListener,
Runnable {
Players
JLabel play[] = new JLABEL[32];

Board
JLabel image;

window pane
Container con;

Tool bar
JToolBar Jmain;
Start again
JButton anew;
Undo
JButton repent;
Open it
JButton ShowOpen;
Save
JButton Showsave;
Exit
JButton exit;

Current information
JLabel text;

Save current operation
Vector Filevar;
Vector Var;

Rule class object (make Call method)
Chessrule rule;

/* Click Pawn ********************************* */
/* Chessmanclick = True flashes the pawn and responds to the thread */
/* Chessmanclick = false eat piece stop blinking and respond to thread */
Boolean Chessmanclick;

/* Control player moves *************************** */
/* Chessplayclick=1 Black chess moves */
/* chessplayclick=2 red moves default red chess */
/* chessplayclick=3 Neither side can moves * *
int chessplayclick = 2;

The thread that controls the flashing of a pawn
Thread Tmain;
The first click of a pawn to the thread response
static int man, I;

Chessmainframe () {
}

Chessmainframe (String Title) {
Obtained Strider citation
con = This.getcontentpane ();
Con.setlayout (NULL);
Instantiating rule classes
Rule = new Chessrule ();
Filevar = new Vector ();
Var = new Vector ();

Creating toolbars
Jmain = new JToolBar ();
Text = new JLabel ("warm welcome");
Text.settooltiptext ("hint message");
anew = new JButton ("newer game");
Anew.settooltiptext ("Re-start a new inning");
Exit = new JButton ("Exit");
Exit.settooltiptext ("Exit this procedure");
repent = new JButton ("undo");
Repent.settooltiptext ("Return to last moves position");
ShowOpen = new JButton ("open");
Showopen.settooltiptext ("Open previous chess");
Showsave = new JButton ("Save");
Showsave.settooltiptext ("Save current Game");

Add a component to a toolbar
Jmain.setlayout (New GridLayout (0, 6));
Jmain.add (anew);
Jmain.add (repent);
Jmain.add (ShowOpen);
Jmain.add (Showsave);
Jmain.add (exit);
Jmain.add (text);
Jmain.setbounds (0, 500, 450, 30);
Con.add (Jmain);

Add a pawn Tag
Drawchessman ();

/* Register Listener */

Register button Monitor
Anew.addactionlistener (this);
Repent.addactionlistener (this);
Exit.addactionlistener (this);
Showopen.addactionlistener (this);
Showsave.addactionlistener (this);

Registration Piece Mobile Monitoring
for (int i = 0; i <; i++) {
Con.add (Play[i]);
Play[i].addmouselistener (this);
}

Add a Board Label
Con.add (image = new JLabel (New ImageIcon ("Img/chess.png"));
Image.setbounds (0, 0, 446, 497);
Image.addmouselistener (this);

Register the form to turn off monitoring
This.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent we) {
System.exit (0);
}
});

Form Center
Dimension screensize = Toolkit.getdefaulttoolkit (). Getscreensize ();
Dimension framesize = This.getsize ();

if (Framesize.height > Screensize.height) {
Framesize.height = Screensize.height;
}
if (Framesize.width > Screensize.width) {
Framesize.width = Screensize.width;
}

This.setlocation ((screensize.width-framesize.width)/2-200,
(screensize.height-framesize.height)/2-290);

This.seticonimage (New ImageIcon ("Car 1.gif"). GetImage ());
This.setresizable (FALSE);
This.settitle (Title);
This.setsize (450, 550);
This.show ();
}

/* Add Pawn Method *********** */
public void Drawchessman () {
Process Control
int I, k;
Icon
Icon in;

Black pieces

Car
in = new ImageIcon ("img/car 2. GIF ");
for (i = 0, k = ten; I < 2; i++, k + = 385) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 10, 40, 40);
Play[i].setname ("Car 1");
}

Horse
in = new ImageIcon ("img/horse 2. GIF ");
for (i = 4, k =, I < 6; i++, k + = 285) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 10, 40, 40);
Play[i].setname ("Ma 1");
}

Phase
in = new ImageIcon ("img/Phase 2. GIF ");
for (i = 8, k = i++, I <; k + = 195) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 10, 40, 40);
Play[i].setname ("Phase 1");
}

Tips
in = new ImageIcon ("img/2. GIF ");
for (i = n, k = 155; i <; i++, k + = 95) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 10, 40, 40);
Play[i].setname ("Shi 1");
}

Stroke
in = new ImageIcon ("img/soldier 2. GIF ");
for (i = +, k = ten; i <; i++, k + = 96.5) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 160, 40, 40);
Play[i].setname ("Stroke 1" + i);
}

Gun
in = new ImageIcon ("img/Cannon 2. GIF ");
for (i = n, k =; i <; i++, k + = 289) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 110, 40, 40);
Play[i].setname ("Cannon 1" + i);
}

Will
in = new ImageIcon ("img/handsome 2. GIF ");
PLAY[30] = new JLabel (in);
Play[30].setbounds (205, 10, 40, 40);
Play[30].setname ("will 1");

Red Pawn

Car
in = new ImageIcon ("img/car 1. GIF ");
for (i = 2, k = ten; I < 4; i++, k + = 385) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 450, 40, 40);
Play[i].setname ("Car 2");
}

Horse
in = new ImageIcon ("img/horse 1. GIF ");
for (i = 6, k =, I < 8; i++, k + = 285) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 450, 40, 40);
Play[i].setname ("Ma 2");
}

Phase
in = new ImageIcon ("img/Phase 1. GIF ");
for (i = ten, k = i++, I <; k + = 195) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 450, 40, 40);
Play[i].setname ("Phase 2");
}

Tips
in = new ImageIcon ("img/1. GIF ");
for (i = +, k = 155; i <; i++, k + = 95) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 450, 40, 40);
Play[i].setname ("Shi 2");
}

Soldiers
in = new ImageIcon ("img/stroke 1. GIF ");
for (i = +, k = ten; i <; i++, k + = 96.5) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 300, 40, 40);
Play[i].setname ("Bing 2" + i);
}

Gun
in = new ImageIcon ("img/Cannon 1. GIF ");
for (i = 289, k = i++, I <;
Play[i] = new JLabel (in);
Play[i].setbounds (k, 350, 40, 40);
Play[i].setname ("Cannon 2" + i);
}

Handsome
in = new ImageIcon ("img/" will 1. GIF ");
PLAY[31] = new JLabel (in);
Play[31].setbounds (205, 450, 40, 40);
Play[31].setname ("Shuai 2");
}

public class Chinesechess {
public static void Main (String args[]) {
New Chessmainframe ("Chinese chess");
}
}

Class Chessmainframe extends JFrame implements ActionListener, MouseListener,
Runnable {
Players
JLabel play[] = new JLABEL[32];

Board
JLabel image;

window pane
Container con;

Tool bar
JToolBar Jmain;
Start again
JButton anew;
Undo
JButton repent;
Open it
JButton ShowOpen;
Save
JButton Showsave;
Exit
JButton exit;

Current information
JLabel text;

Save current operation
Vector Filevar;
Vector Var;

Rule class object (make Call method)
Chessrule rule;

/* Click Pawn ********************************* */
/* Chessmanclick = True flashes the pawn and responds to the thread */
/* Chessmanclick = false eat piece stop blinking and respond to thread */
Boolean Chessmanclick;

/* Control player moves *************************** */
/* Chessplayclick=1 Black chess moves */
/* chessplayclick=2 red moves default red chess */
/* chessplayclick=3 Neither side can moves * *
int chessplayclick = 2;

The thread that controls the flashing of a pawn
Thread Tmain;
The first click of a pawn to the thread response
static int man, I;

Chessmainframe () {
}

Chessmainframe (String Title) {
Obtained Strider citation
con = This.getcontentpane ();
Con.setlayout (NULL);
Instantiating rule classes
Rule = new Chessrule ();
Filevar = new Vector ();
Var = new Vector ();

Creating toolbars
Jmain = new JToolBar ();
Text = new JLabel ("warm welcome");
Text.settooltiptext ("hint message");
anew = new JButton ("newer game");
Anew.settooltiptext ("Re-start a new inning");
Exit = new JButton ("Exit");
Exit.settooltiptext ("Exit this procedure");
repent = new JButton ("undo");
Repent.settooltiptext ("Return to last moves position");
ShowOpen = new JButton ("open");
Showopen.settooltiptext ("Open previous chess");
Showsave = new JButton ("Save");
Showsave.settooltiptext ("Save current Game");

Add a component to a toolbar
Jmain.setlayout (New GridLayout (0, 6));
Jmain.add (anew);
Jmain.add (repent);
Jmain.add (ShowOpen);
Jmain.add (Showsave);
Jmain.add (exit);
Jmain.add (text);
Jmain.setbounds (0, 500, 450, 30);
Con.add (Jmain);

Add a pawn Tag
Drawchessman ();

/* Register Listener */

Register button Monitor
Anew.addactionlistener (this);
Repent.addactionlistener (this);
Exit.addactionlistener (this);
Showopen.addactionlistener (this);
Showsave.addactionlistener (this);

Registration Piece Mobile Monitoring
for (int i = 0; i <; i++) {
Con.add (Play[i]);
Play[i].addmouselistener (this);
}

Add a Board Label
Con.add (image = new JLabel (New ImageIcon ("Img/chess.png"));
Image.setbounds (0, 0, 446, 497);
Image.addmouselistener (this);

Register the form to turn off monitoring
This.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent we) {
System.exit (0);
}
});

Form Center
Dimension screensize = Toolkit.getdefaulttoolkit (). Getscreensize ();
Dimension framesize = This.getsize ();

if (Framesize.height > Screensize.height) {
Framesize.height = Screensize.height;
}
if (Framesize.width > Screensize.width) {
Framesize.width = Screensize.width;
}

This.setlocation ((screensize.width-framesize.width)/2-200,
(screensize.height-framesize.height)/2-290);

This.seticonimage (New ImageIcon ("Car 1.gif"). GetImage ());
This.setresizable (FALSE);
This.settitle (Title);
This.setsize (450, 550);
This.show ();
}

/* Add Pawn Method *********** */
public void Drawchessman () {
Process Control
int I, k;
Icon
Icon in;

Black pieces

Car
in = new ImageIcon ("img/car 2. GIF ");
for (i = 0, k = ten; I < 2; i++, k + = 385) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 10, 40, 40);
Play[i].setname ("Car 1");
}

Horse
in = new ImageIcon ("img/horse 2. GIF ");
for (i = 4, k =, I < 6; i++, k + = 285) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 10, 40, 40);
Play[i].setname ("Ma 1");
}

Phase
in = new ImageIcon ("img/Phase 2. GIF ");
for (i = 8, k = i++, I <; k + = 195) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 10, 40, 40);
Play[i].setname ("Phase 1");
}

Tips
in = new ImageIcon ("img/2. GIF ");
for (i = n, k = 155; i <; i++, k + = 95) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 10, 40, 40);
Play[i].setname ("Shi 1");
}

Stroke
in = new ImageIcon ("img/soldier 2. GIF ");
for (i = +, k = ten; i <; i++, k + = 96.5) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 160, 40, 40);
Play[i].setname ("Stroke 1" + i);
}

Gun
in = new ImageIcon ("img/Cannon 2. GIF ");
for (i = n, k =; i <; i++, k + = 289) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 110, 40, 40);
Play[i].setname ("Cannon 1" + i);
}

Will
in = new ImageIcon ("img/handsome 2. GIF ");
PLAY[30] = new JLabel (in);
Play[30].setbounds (205, 10, 40, 40);
Play[30].setname ("will 1");

Red Pawn

Car
in = new ImageIcon ("img/car 1. GIF ");
for (i = 2, k = ten; I < 4; i++, k + = 385) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 450, 40, 40);
Play[i].setname ("Car 2");
}

Horse
in = new ImageIcon ("img/horse 1. GIF ");
for (i = 6, k =, I < 8; i++, k + = 285) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 450, 40, 40);
Play[i].setname ("Ma 2");
}

Phase
in = new ImageIcon ("img/Phase 1. GIF ");
for (i = ten, k = i++, I <; k + = 195) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 450, 40, 40);
Play[i].setname ("Phase 2");
}

Tips
in = new ImageIcon ("img/1. GIF ");
for (i = +, k = 155; i <; i++, k + = 95) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 450, 40, 40);
Play[i].setname ("Shi 2");
}

Soldiers
in = new ImageIcon ("img/stroke 1. GIF ");
for (i = +, k = ten; i <; i++, k + = 96.5) {
Play[i] = new JLabel (in);
Play[i].setbounds (k, 300, 40, 40);
Play[i].setname ("Bing 2" + i);
}

Gun
in = new ImageIcon ("img/Cannon 1. GIF ");
for (i = 289, k = i++, I <;
Play[i] = new JLabel (in);
Play[i].setbounds (k, 350, 40, 40);
Play[i].setname ("Cannon 2" + i);
}

Handsome
in = new ImageIcon ("img/" will 1. GIF ");
PLAY[31] = new JLabel (in);
Play[31].setbounds (205, 450, 40, 40);
Play[31].setname ("Shuai 2");
}

three. Concluding remarks

In this experiment I met a lot of difficulties, found a lot of problems, it is in the problem-solving period I slowly became familiar with the basic knowledge of Java, only slowly learned how to follow the given requirements to design a suitable java system.

Object-oriented design programming

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.