Source code of java swing double play, swing play

Source: Internet
Author: User

Source code of java swing double play, swing play

Import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. toolkit; import java. awt. event. mouseEvent; import java. awt. event. mouseListener; import java. awt. image. bufferedImage; import javax. swing. JFrame; import javax. swing. JOptionPane; public class FiveChessFrame extends JFrame implements Runnable, MouseListener {/*****/private static final long serialVersionUID = 1L; // game interface size int wi Dth = Toolkit. getdefatooltoolkit (). getScreenSize (). width; int height = Toolkit. getdefatooltoolkit (). getScreenSize (). height; int x, y; // The mouse coordinate int [] [] allChess = new int [15] [15]; // Save the checker. 0 indicates no child, and 1 indicates sunspots, 2: Boolean isblack = true; // whether the current sub-child is a sunspot or a white word; true indicates a sunspot; false indicates a white sub-Boolean canPlay = true; // indicates whether the current game ends. String message = ""; String blackMessage = "unlimited"; String whiteMessage = "unlimited"; // Save the score, records the position of each step. int [] chessX = new int [255]; int [] ChessY = new int [255]; int countX, countY; // default value: int maxTime = 0; // maximum time int blackTime = 0; int whiteTime = 0; // Save the remaining time of the black and white side Thread timer = new Thread (this); public FiveChessFrame () {this. setTitle ("wuziqi game"); this. setSize (500,500); this. setLocation (width-500)/2, (height-500)/2); this. setResizable (false); this. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); this. setVisible (true); this. repaint (); this. AddMouseListener (this); timer. start (); // enable the timer thread timer. suspend () ;}// draw the chessboard interface public void paint (Graphics g) {// Double Buffer technology BufferedImage buf = new BufferedImage (500,500, BufferedImage. TYPE_INT_RGB); Graphics g1 = buf. createGraphics (); // create a paint brush g1.setColor (new Color (0,169,158); // set the paint Color g1.fill3DRect (375,375, true); for (int I = 0; I <= 15; I ++) {g1.setColor (Color. WHITE); g1.drawLine (25,375 + 60 + I * + + 60 + I * 25); // draw the g1.drawL Ine (43 + I * 25, 60, 43 + I * 25,375 + 60); // draw a checkboard vertical line} g1.setFont (new Font ("", Font. BOLD, 20); g1.drawString ("game info:" + message, 50, 50); g1.drawRect (30,440,180, 40); g1.drawRect (250,440,180, 40 ); // draw the border g1.setFont (new Font ("", 40,465) of the black time and white time string; g1.drawString (":" + blackMessage ); g1.drawString (":" + whiteMessage, 260,465); g1.drawRect (428,106,); g1.drawString ("",); // restart the start button g1.drawRect, 54, 20); g1.drawString ("game Settings", 432,120); // g1.drawRect (428,146, 54, 20); g1.drawString ("Game Description", 432,160 ); // Game Description button g1.drawRect (428,186, 54, 20); g1.drawString ("Quit game", 432,200); // exit game button g1.drawRect (428,246, 54, 20 ); g1.drawString ("regret", 442,260); // regret g1.drawRect (428,286, 54, 20); g1.drawString ("Admit defeat", 442,300); // for (int I = 0; I <15; I ++) {for (int j = 0; j <15; j ++) {// draw a solid blacklist if (allChess [I] [j] = = 1) {int tempX = I * 25 + 47; int tempY = j * 25 + 64; g1.setColor (Color. BLACK); g1.fillOval (tempX, tempY, 16, 16); g1.setColor (Color. BLACK); g1.drawOval (tempX, tempY, 16, 16);} // draw a solid white sub if (allChess [I] [j] = 2) {int tempX = I * 25 + 47; int tempY = j * 25 + 64; g1.setColor (Color. WHITE); g1.fillOval (tempX, tempY, 16, 16); g1.setColor (Color. BLACK); g1.drawOval (tempX, tempY, 16, 16) ;}} g. drawImage (buf, 0, 0, this) ;}@ Overridepubli C void mousePressed (MouseEvent e) {if (canPlay) {x = e. getX (); y = e. getY (); // obtain the mouse coordinate if (x >=55 & x <= 405 & y> = 72 & y <= 420) {// make x, y in the range of 0-15 if (x-55) % 25> 12) {x = (x-55)/25 + 1;} else {x = (x-55)/25 ;} if (y-72) % 25> 12) {y = (y-72)/25 + 1;} else {y = (y-72)/25 ;} // if (allChess [x] [y] = 0) {chessX [countX ++] = x; chessY [countY ++] = y; if (isblack) {allChess [x] [y] = 1; isblack = false; message = "";} else {allChess [x] [y] = 2; isblack = true; message = "black box Sub ";} this. repaint (); if (this. isWin () {if (allChess [x] [y] = 1) {JOptionPane. showMessageDialog (this, "Game ended, Black wins");} else {JOptionPane. showMessageDialog (this, "the game is over, the White wins");} this. canPlay = false; // identifies the end of the game }}// re-start the game if (e. getX () >=428 & e. getY () <= (428 + 54) & e. getY ()> = 66 & e. getY () <= (66 + 20) {int result = JOptionPane. showConfirmDialog (this, "Do you want to restart the game? "); // 0 indicates restarting if (result = 0) {restartGame () ;}// set the game countdown if (e. getX () >=428 & e. getX () <= (428 + 54) & e. getY () >=106 & e. getY () <= (106 + 20) {String time = JOptionPane. showInputDialog ("Enter the maximum game time (unit: minute). If you enter 0, there is no time limit:"); maxTime = Integer. parseInt (time) * 60; // converts a minute to a second to calculate if (maxTime <0) {JOptionPane. showMessageDialog (this, "The entered game time is incorrect. Please reset it! ");} Else if (maxTime = 0) {int result = JOptionPane. showConfirmDialog (this," The Game time is set successfully. Are you sure you want to start the game again? "); // Re-start if (result = 0) {restartGame (); timer. suspend (); // suspend the thread for later restart} else if (maxTime> 0) {int result = JOptionPane. showConfirmDialog (this, "The Game time is set successfully. Are you sure you want to start the game again? "); If (result = 0) {for (int I = 0; I <15; I ++) {for (int j = 0; j <15; j ++) {allChess [I] [j] = 0 ;}}for (int I = 0; I <15; I ++) {chessX [I] =-1; chessY [I] =-1;} countX = 0; countY = 0; message = ""; isblack = true; blackMessage = maxTime/3600 + ":" + (maxTime/60-maxTime/3600*60) + ":" + (maxTime-maxTime/60*60 ); whiteMessage = maxTime/3600 + ":" + (maxTime/60-maxTime/3600*60) + ":" + (maxTime-maxTime/60*60); blackTime = maxTime; whiteTime = maxTime; timer. Resume (); this. canPlay = true; this. repaint () ;}}// game description if (e. getX () >=428 & e. getY () <= (428 + 54) & e. getY () >=146 & e. getY () <= (146 + 20) {JOptionPane. showMessageDialog (this, "rule: vertical and horizontal pairs are connected to the five winners to win! ");} // Exit the game if (e. getX () >=428 & e. getX () <= (428 + 54) & e. getY () >=186 & e. getY () <= (186 + 20) {int result = JOptionPane. showConfirmDialog (this, "do you want to quit the game? "); If (result = 0) {System. exit (0) ;}// regret if (e. getX () >=428 & e. getX () <= (428 + 54) & e. getY () >=246 & e. getY () <= (246 + 20) {int result = JOptionPane. showConfirmDialog (this, (isblack = true? "Does the black party agree to regret it? ":" Do the white parties agree to regret it? "); // Result = 0 indicates repentance if (result = 0) {allChess [chessX [-- countX] [chessY [-- countY] = 0; if (isblack = true) {isblack = false;} else {isblack = true;} this. repaint (); // re-paint the board} // identify if (e. getX () >=428 & e. getX () <= (428 + 54) & e. getY () >=286 & e. getY () <= (286 + 20) {int result = JOptionPane. showConfirmDialog (this, "Do you want to admit defeat? "); If (result = 0) {JOptionPane. showMessageDialog (this," the game is over, "+ (isblack = true? "The black party admits defeat, and the white party wins! ":" The white team admits defeat. The black team wins! ") ;}}// Timing @ Overridepublic void run () {if (this. maxTime> 0) {while (true) {if (isblack) {blackTime --; if (blackTime = 0) {JOptionPane. showMessageDialog (this, "Black time-out, game end"); restartGame (); timer. suspend () ;}} else {whiteTime --; if (whiteTime = 0) {JOptionPane. showMessageDialog (this, "White-side timeout, game end"); restartGame (); timer. suspend () ;}} blackMessage = blackTime/3600 + ":" + (blackTime/60-blackTime/3600*60) + ":" + (blackTime-blackTime/60 * 60); whiteMessage = whiteTime/3600 + ":" + (whiteTime/60-whiteTime/3600*60) + ":" + (whiteTime-whiteTime/60*60); this. repaint (); try {Thread. sleep (1000); // You can synchronize the settings with the clock once in one second.} catch (InterruptedException e) {e. printStackTrace () ;}}}@ Overridepublic void mouseClicked (MouseEvent e) {// TODO Auto-generated method stub} @ Overridepublic void mouseReleased (MouseEvent e) {// TODO Auto-generated method stub} @ Overridepublic Void mouseEntered (MouseEvent e) {// TODO Auto-generated method stub} @ Overridepublic void mouseExited (MouseEvent e) {// TODO Auto-generated method stub} public void restartGame () {for (int I = 0; I <15; I ++) {for (int j = 0; j <15; j ++) {allChess [I] [j] = 0; // clear the chess pieces} // clear the records of the chess pieces coordinate (x, y) for (int I = 0; I <15; I ++) {chessX [I] = 0; chessY [I] = 0;} countX = 0; countY = 0; message = "black first "; blackMessage = "unlimited"; whiteMessage = "unlimited"; blackTime = maxT Ime; whiteTime = maxTime; isblack = true; canPlay = true; this. repaint ();} public boolean isWin () {boolean flag = false; // define a flag int count = 1; // save how many pieces of the same color are connected, the initial value is 1int color = allChess [x] [y]; // color = 1 (Sunday ). color = 2 (white sub) // determines whether there are five pieces connected horizontally. Feature: y values in allChess [x] [y] are the same count = this. checkCount (, color); if (count> = 5) {flag = true;} else {// determine the vertical count = this. checkCount (0, 1, color); if (count> = 5) {flag = true;} else {// upper right and lower left count = this. CheckCount (1,-1, color); if (count> = 5) {flag = true;} else {// bottom right, top left count = this. checkCount (, color); if (count> = 5) {flag = true ;}}} return flag;} public int checkCount (int xChange, int yChange, int color) {int count = 1; int tempX = xChange; int tempY = yChange; // Save the initial value // The global variable x, and y is the coordinates of the mouse clicks, after playing chess, the range of x and y has been changed to 0-15 (traverse the entire board and look for pawns of the same color) while (x + xChange> = 0 & x + xChange <15 & y + yChange> = 0 & y + yChange <15 & color = allChess [x + xChan Ge] [y + yChange]) {count ++; if (xChange! = 0) xChange ++; if (yChange! = 0) {if (yChange! = 0) {if (yChange> 0) yChange ++; else {yChange -- ;}}} xChange = tempX; yChange = tempY; while (x-xChange> = 0 & x-xChange <15 & y-yChange> = 0 & y-yChange <15 & color = allChess [x-xChange] [y-yChange]) {count ++; if (xChange! = 0) {xChange ++;} if (yChange! = 0) {if (yChange> 0) yChange ++; else {yChange -- ;}}return count;} public static void main (String [] args) {new FiveChessFrame ();}}

 

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.