Current location - Education and Training Encyclopedia - Graduation thesis - Ask for help and make a java gobang or Android bookkeeping app.
Ask for help and make a java gobang or Android bookkeeping app.
1. gobang board

[java] View plain copy printing?

Package cn.edu.ouc.five chess;

Import java.awt.color;

Import java.awt.cursor;

Import java.awt.dimension;

Import java.awt.graphics;

Import java.awt.graphics2d;

Import java.awt.image;

Import java.awt.radial gradientpin;

Import java.awt.renderinghints;

Import java.awt.toolkit;

Import java.awt.event.mouseevent;

Import java.awt.event.mouselistener;

Import java.awt.event.mousemotionlistener;

Import java.awt.geom.ellipse2d;

Import javax.swing. *;

/**

* Gobang-Chessboard

*/

The common chessboard extension JPanel realizes MouseListener {

Public static final int MARGIN = 30// margin

public static final int GRID _ SPAN = 35; //Grid spacing

Public static final int rows =15; //Number of chessboard rows

Public static final int cols =15; //Number of chessboard columns

Point[]chess list = new point [(rows+1) * (cols+1)]; //Initially, each array element is empty.

Boolean isBlack = true// The default start is black.

Boolean gameOver = false// Is the game over?

Intchescount//Current number of chessboards

Int xIndex, yIndex// The index of the chess piece that has just been placed.

Image img

Image shadow;

Color colortemp

Public chessboard () {

//set background(color . blue); //Set the background color to orange.

img=Toolkit.getDefaultToolkit()。 getImage(" board . jpg ");

shadows = toolkit . getdefaulttoolkit()。 getImage(" shadows . jpg ");

addmouse listener(this);

addMouseMotionListener(new MouseMotionListener(){

public void mouse dragged(mouse event e){

}

public void mouse moved(mouse event e){

int x 1 =(e . getx()-MARGIN+GRID _ SPAN/2)/GRID _ SPAN;

//Convert the coordinate position clicked by the mouse into a grid index.

int y 1 =(e . gety()-MARGIN+GRID _ SPAN/2)/GRID _ SPAN;

//The game is over and you can't get off.

//If you fall off the board, you can't get off.

//Chess pieces already exist at X and Y positions, and cannot be removed.

if(x 1 & lt; 0 | | x 1 & gt; ROWS | | y 1 & lt; 0 | | y 1 & gt; COLS | | game over | | find chess(x 1,y 1))

SetCursor (new cursor). DEFAULT _ cursor));

//Set to the default state

Else setCursor (new cursor (cursor. Hand _ cursor));

}

});

}

//draw

public void paint component(Graphics g){

super . paint component(g); //Draw a chessboard

int img width = img . getwidth(this);

int img height = img . get height(this); //Get the width and height of the picture.

int FWidth = getWidth();

int FH height = getHeight(); //Get the width and height of the window.

int x =(FWidth-img width)/2;

int y =(FH height-img height)/2;

g.drawImage(img,x,y,null);

for(int I = 0; I<= OK; I++){// Draw a horizontal line.

g.drawLine(MARGIN,MARGIN+i*GRID_SPAN,MARGIN+COLS*GRID_SPAN,MARGIN+I * GRID _ SPAN);

}

for(int I = 0; I<= COLSI++){// Draw a vertical line.

g.drawLine(MARGIN+i*GRID_SPAN,MARGIN,MARGIN+i*GRID_SPAN,MARGIN+ROWS * GRID _ SPAN);

}

//Draw chess pieces

for(int I = 0; Me & LTCHESSOUNTI++) {

//grid intersection x, y coordinates

int xPos=chessList[i]。 getX()* GRID _ SPAN+MARGIN;

int yPos=chessList[i]。 getY()* GRID _ SPAN+MARGIN;

g.setColor(chessList[i].getColor()); //Set the color

// g.fillOval(xPos-Point。 Diameter /2, yPos-Point.DIAMETER/2,

//point. Diameter, point. Diameter);

//g.drawImage (shadow, xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, point. Diameter, point. Diameter, empty);

colortemp=chessList[i]。 getColor();

if(colortemp==Color.black){

radial gradient paint paint = new radial gradient paint(xPos-Point。 Diameter /2+25, yPos-Point.DIAMETER/2+10,20, new floating []{0f, 1f}

, new color [] {color. White, color. Black});

((Graphics2D) g)。 SetPaint;

((Graphics2D) g)。 setRenderingHint(rendering hints。 KEY _ antialiasing, rendering hints. VALUE _ ANTIALIAS _ ON);

((Graphics2D) g)。 setRenderingHint(rendering hints。 KEY _ ALPHA _ interpolation, rendering hints. VALUE _ ALPHA _ interpolation _ default);

}

else if(color temp = = color . white){

radial gradient paint paint = new radial gradient paint(xPos-Point。 Diameter /2+25, yPos-Point.DIAMETER/2+10,70, newly floating []{0f, 1f}

, new color [] {color. White, color. Black});

((Graphics2D) g)。 SetPaint;

((Graphics2D) g)。 setRenderingHint(rendering hints。 KEY _ antialiasing, rendering hints. VALUE _ ANTIALIAS _ ON);

((Graphics2D) g)。 setRenderingHint(rendering hints。 KEY _ ALPHA _ interpolation, rendering hints. VALUE _ ALPHA _ interpolation _ default);

}

Ellipse2D e = new Ellipse2D。 Floating-point (xPos-Point. Diameter /2, yPos-Point.DIAMETER/2, 34, 35);

((Graphics2D) g)。 Fill (e);

//Mark the red rectangular box of the last chess piece.

If(i==chessCount- 1){// If it is the last chess piece,

g . set color(color . red);

g.drawRect(xPos-Point。 Diameter /2, yPos-Point.DIAMETER/2,

34, 35);

}

}

}

Public void mouse pressed (mouse event e) {//Called when the mouse is pressed on the component.

//Game over, no more downloads.

If(gameOver) returns;

String colorName=isBlack? "black chess": "white chess";

//Convert the coordinate position clicked by the mouse into a grid index.

xIndex =(e . getx()-MARGIN+GRID _ SPAN/2)/GRID _ SPAN;

yIndex =(e . gety()-MARGIN+GRID _ SPAN/2)/GRID _ SPAN;

//If you fall off the board, you can't get off.

if(xIndex & lt; 0 ||| Xindex & gt line number ||| y index & lt0 ||| y index & gt cols)

Return;

//If a chess piece already exists in the X and Y positions, it cannot be removed.

If(findChess(xIndex, yIndex)) returns;

//Processing that can be completed in progress

Point ch = new point (xIndex, yIndex, isBlack? Color.black: color. white);

chess list[ches count++]= ch;

repaint(); //Notify the system to repaint.

//If you win, you will be prompted. You can't continue playing chess.

if(isWin()){

String msg=String.format ("Congratulations, %s won!" ,color name);

joptionpane . showmessagedialog(this,msg);

gameOver = true

}

isBlack=! isBlack

}

//Override the method of mouseListener

public void mouse clicked(mouse event e){

//Called when the mouse button is clicked on the component.

}

public void mouse enter(mouse event e){

//Called when the mouse enters the component.

}

public void mouse exited(mouse event e){

//Called when the mouse leaves the component.

}

public void mouse released(mouse event e){

//Called when the mouse button is released on the component.

}

//Find out whether there is a chess piece with index x, y in the chess piece array.

private boolean findChess(int x,int y){

For (point c: chessList){

If (c! = null & amp& ampgetx()= = x & amp; & ampc.getY()==y)

Return true

}

Returns false

}

Private Boolean value isWin(){

int continue count = 1; //Number of consecutive fragments

//Look horizontally to the west

for(int x = xIndex- 1; x & gt=0; x - ){

Color c=isBlack? Color. Black: color. White;

if(getChess(x,yIndex,c)! =null){

continue count++;

} Otherwise,

Break;

}

//Look east horizontally

for(int x = xIndex+ 1; x & lt= COLSx++){

Color c=isBlack? Color. Black: color. White;

if(getChess(x,yIndex,c)! =null){

continue count++;

} Otherwise,

Break;

}

if(continue count & gt; =5){

Return true

} Otherwise,

continue count = 1;

//Continue another vertical search.

//Search up

for(int y = yIndex- 1; y & gt=0; y - ){

Color c=isBlack? Color. Black: color. White;

if(getChess(xIndex,y,c)! =null){

continue count++;

} Otherwise,

Break;

}

//Look down vertically

for(int y = yIndex+ 1; Y<= line; y++){

Color c=isBlack? Color. Black: color. White;

if(getChess(xIndex,y,c)! = empty)

continue count++;

other

Break;

}

if(continue count & gt; =5)

Return true

other

continue count = 1;

//Continue the search in another case: diagonal.

//Looking for the Northeast

for(int x=xIndex+ 1,y = yIndex- 1; y & gt= 0 & amp& ampx & lt= COLSx++,y - ){

Color c=isBlack? Color. Black: color. White;

if(getChess(x,y,c)! =null){

continue count++;

}

else break

}

//looking for the southwest

for(int x=xIndex- 1,y = yIndex+ 1; X>= 0 & amp& ampy & lt=; lt = line; x -,y++){

Color c=isBlack? Color. Black: color. White;

if(getChess(x,y,c)! =null){

continue count++;

}

else break

}

if(continue count & gt; =5)

Return true

Otherwise continue count =1;

//Continue the search in another case: diagonal.

//looking for the northwest

for(int x=xIndex- 1,y = yIndex- 1; x & gt= 0 & amp& ampy & gt=0; x -,y - ){

Color c=isBlack? Color. Black: color. White;

if(getChess(x,y,c)! = empty)

continue count++;

else break

}

//Look for the southeast direction

for(int x=xIndex+ 1,y = yIndex+ 1; X<= COLS & amp& ampy & lt=; lt = line; x++,y++){

Color c=isBlack? Color. Black: color. White;

if(getChess(x,y,c)! = empty)

continue count++;

else break

}

if(continue count & gt; =5)

Return true

Otherwise continue count =1;

Returns false

}

GetChess(int xIndex, int yIndex, Color color){

For (point p: chesslist) (

If (p! = null & amp& ampp . getx()= xIndex & amp; & ampp.getY()==yIndex

& amp& ampp.getColor()==color)

Return p;

}

Returns null

}

public void restartGame(){

//Clear chess pieces

for(int I = 0; I< chesslist. length; i++){

chess list[I]= null;

}

//Restore game-related variable values

isBlack = true

GameOver = false// Is the game over?

ches count = 0; //The current number of chessboards

repaint();

}

//regret chess

Public void goback(){

if(chessCount==0)

Return;

chess list[chess count- 1]= null;

ches count-;

if(ches count & gt; 0){

xIndex = chess list[chess count- 1]。 getX();

yIndex = chess list[chess count- 1]。 getY();

}

isBlack=! isBlack

repaint();

}

//Rectangular dimension

Public dimension getPreferredSize(){

Returns the new dimension (MARGIN*2+GRID_SPAN*COLS, MARGIN*2

+GRID _ SPAN * ROWS);

}

}