Object-Oriented Programming

Source: Internet
Author: User

Class detail Design:

1. Create a chessboard class, draw the style of the board, add various buttons on the board panel and corresponding HU trigger events. At the same time pay attention to the design of the panel, so that the panel design as far as possible reasonable beauty.

A. Drawing of the Chessboard

public void Draw_qipan (Graphics G)// Draw Board 15*15{

G.setcolor (Color.lightgray);

G.fill3drect (10,10,300,300,true);

G.setcolor (Color.Black);

for (int i=1;i<16;i++) {

G.drawline (20,20*i,300,20*i);

G.drawline (20*i,20,20*i,300);

}

}

B. Add button and button Hu plan

Button B1=new button (" start ");

Button B2=new button (" Reset game ");

Label Lblwin=new label ("");

Checkbox Ckbhb[]=new checkbox[3];

button exist = New button (" exit ");

public void init () {

Ckbhb[0]=new Checkbox (" white ", Ckghb,false);

Ckbhb[1]=new Checkbox (" black ", Ckghb,false);

Ckbhb[2]=new Checkbox (" watch ", CKGHB, false);

}

C. Mouse piece hu corresponding Trigger event

public void mouseclicked (MouseEvent e) {

Graphics G=getgraphics ();

int x1,y1;

X1=e.getx ();

Y1=e.gety ();

if (E.getx () <20 | | e.getx () >300 | | e.gety () <20 | | e.gety () >300) {

Return

}

if (x1%20>10) {

x1+=10;

}

if (y1%20>10) {

y1+=10;

}

x1=x1/20*20;

y1=y1/20*20;

Set_qizi (X1,Y1);

m*= (-1);

}

D. trigger events for buttons

public void actionperformed (ActionEvent e) {

Graphics G=getgraphics ();

if (E.getsource () ==b1) {

Game_start ();

}

else{

Game_re ();

}

if (E.getsource () ==exist) {

Game_re ();

Lblwin.settext (Get_qizi_color (Color_qizi) + " lost !");

intgame_start=0;

}

2. Write a function to judge the position of the Lazi and draw the corresponding black and white pieces

public void Set_qizi (int x,int y)//Lazi {

if (intgame_start==0)//judgment game not started {

Return

}

if (intgame_body[x/20][y/20]!=0) {

Return

}

Graphics G=getgraphics ();

if (color_qizi==1)//Determine the sunspot or the white child {

G.setcolor (Color.Black);

color_qizi=0;

}

else{

G.setcolor (Color.White);

Color_qizi=1;

}

G.filloval (x-10,y-10,20,20);

intgame_body[x/20][y/20]=color_qizi+1;

3. Judging the outcome

if (game_win_1 (X/20,Y/20))// judgment win or lose 1{

Lblwin.settext (Get_qizi_color (Color_qizi) + " won !");

intgame_start=0;

}

if (Game_win_2 (X/20,Y/20))// judgment win or lose 2{

Lblwin.settext (Get_qizi_color (Color_qizi) + " won !");

intgame_start=0;

}

if (Game_win_3 (X/20,Y/20))// judgment win or lose 3{

Lblwin.settext (Get_qizi_color (Color_qizi) + " won !");

intgame_start=0;

}

if (Game_win_4 (X/20,Y/20))// judgment win or lose 4{

Lblwin.settext (Get_qizi_color (Color_qizi) + " won !");

intgame_start=0;

}  }

Specific implementation

1. Create a Board class

public class Wcychess extends Applet implements actionlistener,mouselistener,mousemotionlistener,itemlistener{

int color_qizi=0;//flag Color Identification 0: White sub 1: Sunspots

int intgame_start=0;//game start Flag 0 not started 1 in-game

int intgame_body[][]=new int[16][16]; Set the board piece status

int m=-1;

Button B1=new button ("Start");

Button B2=new button ("Reset Game");

Label Lblwin=new label ("");

Checkbox Ckbhb[]=new checkbox[3];

button exist = New button ("Exit");

CheckboxGroup ckghb=new CheckboxGroup ();

Netchatclient chat=new netchatclient ();

public void init () {

SetLayout (NULL);

Addmouselistener (this);

Add (B1);

B1.setbounds (330,50,80,30);

B1.addactionlistener (this);

Add (B2);

B2.setbounds (330,90,80,30);

B2.addactionlistener (this);

Ckbhb[0]=new Checkbox ("white", ckghb,false);

Ckbhb[0].setbounds (320,20,60,30);

Ckbhb[1]=new Checkbox ("Black", ckghb,false);

Ckbhb[1].setbounds (380,20,60,30);

Ckbhb[2]=new Checkbox ("Watch", CKGHB, false);

Add (ckbhb[0]);

Add (ckbhb[1]);

Add (ckbhb[2]);

Ckbhb[0].additemlistener (this);

Ckbhb[1].additemlistener (this);

Add (Lblwin);

Lblwin.setbounds (330,180,80,30);

Lblwin.setbackground (color.red);

Lblwin.settext ("Victor! ");//not shown?

Ckbhb[2].setbounds (440, 20, 60, 30);

Add (exist);

Exist.setbounds (330,130,80,30);

Exist.addactionlistener (this);

Add (chat); Chat.setbounds (20, 500, 300, 300);

Chat.frame ();

Chat.setvisible (TRUE);

GAME_START_CSH ();

SetSize (500,600);

SetVisible (TRUE);

}

public void itemstatechanged (ItemEvent e) {

if (Ckbhb[0].getstate ())//Select a sunspot or a white child {

color_qizi=0;

}

else{

Color_qizi=1;

}

}

public void mousepressed (MouseEvent e) {}

public void mouseclicked (MouseEvent e) {

Graphics G=getgraphics ();

int x1,y1;

X1=e.getx ();

Y1=e.gety ();

if (E.getx () <20 | | e.getx () >300 | | e.gety () <20 | | e.gety () >300) {

Return

}

if (x1%20>10) {

x1+=10;

}

if (y1%20>10) {

y1+=10;

}

x1=x1/20*20;

y1=y1/20*20;

Set_qizi (X1,Y1);

m*= (-1);

}

public void actionperformed (ActionEvent e) {

Graphics G=getgraphics ();

if (E.getsource () ==b1) {

Game_start ();

}

else {

Game_re ();

}

if (E.getsource () ==exist) {

Game_re ();

Lblwin.settext (Get_qizi_color (Color_qizi) + "Lost!");

intgame_start=0;

}

}

public void mouseentered (MouseEvent e) {}

public void mouseexited (MouseEvent e) {}

public void mousereleased (MouseEvent e) {}

public void mousedragged (MouseEvent e) {}

public void mousemoved (MouseEvent e) {}

public void Paint (Graphics g) {

Draw_qipan (g);

}

2 The realization of judging and losing function

if (game_win_1 (X/20,Y/20)) {//Judge win or lose 1

Lblwin.settext (Get_qizi_color (Color_qizi) + " won !");

intgame_start=0;

}

if (Game_win_2 (X/20,Y/20)) {// judge win or lose 2

Lblwin.settext (Get_qizi_color (Color_qizi) + " won !");

intgame_start=0;

}

if (Game_win_3 (X/20,Y/20))// judgment win or lose 3{

Lblwin.settext (Get_qizi_color (Color_qizi) + " won !");

intgame_start=0;

}

if (Game_win_4 (X/20,Y/20))// judgment win or lose 4{

Lblwin.settext (Get_qizi_color (Color_qizi) + " won !");

intgame_start=0;

}

Determine winning and losing program implementation in detail process

public boolean game_win_1 (int x,int y) {// horizontal judgment win or lose

int x1,y1,t=1;

X1=x;

Y1=y;

for (int i=1;i<5; i++) {

if (x1>15) {

Break

}

if (Intgame_body[x1+i][y1]==intgame_body[x][y]) {

T+=1;

}

else{

Break

}

}

for (int i=1;i<5; i++) {

if (x1<1)

Break

}

if (Intgame_body[x1-i][y1]==intgame_body[x][y])

T+=1;

}

else{

Break

}

}

if (t>4) {

return true;

}

else{

return false;

}

}

public boolean game_win_2 (int x,int y) {// portrait to determine win or lose

int x1,y1,t=1;

X1=x;

Y1=y;

for (int i=1;i<5; i++) {

if (x1>15) {

Break

}

if (Intgame_body[x1][y1+i]==intgame_body[x][y]) {

T+=1;

}

else{

Break

}

}

for (int i=1;i<5; i++) {

if (x1<1) {

Break

}

if (Intgame_body[x1][y1-i]==intgame_body[x][y]) {

T+=1;

}

else{

Break

}

}

if (t>4) {

return true;

}

else{

return false;

}

}

public boolean game_win_3 (int x,int y)// left oblique judgment win or lose {

int x1,y1,t=1;

X1=x;

Y1=y;

for (int i=1;i<5; i++) {

if (x1>15) {

Break

}

if (Intgame_body[x1+i][y1-i]==intgame_body[x][y]) {

T+=1;

}

else{

Break

}

}

for (int i=1;i<5; i++) {

if (x1<1) {

Break

}

if (Intgame_body[x1-i][y1+i]==intgame_body[x][y]) {

T+=1;

}

else{

Break

}

}

if (t>4) {

return true;

}

else{

return false;

}}

public boolean game_win_4 (int x,int y) {// left oblique judging win or loss

int x1,y1,t=1;

X1=x;

Y1=y;

for (int i=1;i<5; i++) {

if (x1>15) {

Break

}

if (Intgame_body[x1+i][y1+i]==intgame_body[x][y]) {

T+=1;

}

Else

{

Break

}

}

for (int i=1;i<5; i++) {

if (x1<1) {

Break

}

if (Intgame_body[x1-i][y1-i]==intgame_body[x][y]) {

T+=1;

}

else{

Break } }

if (t>4) {

return true;

}

else{

return false;

}

}

Object-Oriented 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.