Java Programming (12.3)----Listener Primary application: Gobang

Source: Internet
Author: User

1. Making Gobang Game software

Because the teacher has been basically finished, the time to redo a lot faster ... But I still feel the confusion of thinking ... Which way on which side first, which method in which side of the good question too tangled ...

First the window inner class: The constructor drawing of the mouse adapter window

Package Com.lovo.homework2;import Java.awt.color;import Java.awt.graphics;import java.awt.image;import Java.awt.event.mouseadapter;import Java.awt.event.mouseevent;import Java.awt.image.bufferedimage;import Javax.swing.jframe;import javax.swing.joptionpane;/** * Class: my gobang window * * @author Abe */public class Myframerenju extends J Frame {private Myboardrenju board = new Myboardrenju ();p rivate boolean isblack = true;private Image offimage = new Buffere DIMAGE (800,BUFFEREDIMAGE.TYPE_INT_RGB);//double-buffered private Boolean isgameover = false;/** * Inner class: Mouse Adapter * * @author Abe */pu Blic class Mymouseadapter extends Mouseadapter {@Overridepublic void mousepressed (MouseEvent e) {//override the Click Mouse method if (!isgameo ver) {int x = E.GETX (); int y = e.gety (); if (x > y && x < 775 && 775) {i NT i = (x-25)/50;int j = (y-25)/50;if (Board.move (i, J, Isblack)) {repaint (); if (Board.win (i, J, Isblack)) {Joptio Npane.showmessagedialog (null,isblack? "The Black side wins!" ":" White wins "); Isgameover = true;} Isblack =!isblack;}}}} /** * Constructor */public Myframerenju () {this.settitle ("Gobang"); This.setsize (n); this.setresizable (false); This.setdefaultcloseoperation (Exit_on_close); This.setlocationrelativeto (null); This.setlayout (null); This.getcontentpane (). SetBackground (New Color (180, 125, 12)); Mymouseadapter L = new Mymouseadapter (); This.addmouselistener (l);} /** * Override method Draw all NEWG are double buffered to remove splash screen required */@Overridepublic void Paint (Graphics g) {Graphics NEWG = Offimage.getgraphics (); Super.pai NT (NEWG); Board.draw (NEWG); G.drawimage (offimage, 0, 0, +, +, NULL);} public static void Main (string[] args) {new Myframerenju (). setvisible (True);}}


Then the position panel, draw pieces, moves, judge the outcome

Package Com.lovo.homework2;import Java.awt.basicstroke;import Java.awt.color;import java.awt.graphics;import Java.awt.graphics2d;import Java.awt.shape;import Java.awt.stroke;import javax.xml.bind.annotation.adapters.collapsedstringadapter;/** * Class: Gobang Board * * @author Abe * */public class Myboardrenju { Private int[][] p = new INT[15][15]; Assign a value of public void Draw (Graphics g) {G.setcolor (Color.Black) to each intersection; Graphics2D g2d = (graphics2d) g; The strong turn G is assigned to the G2dg2d.setstroke (new Basicstroke (5)) to the 2D type, and the G.drawrect (The new Basicstroke (1)); G.filloval (392, 392, 16, 16); Painting Tianyuan star G.filloval (195, 195, ten); G.filloval (195, 595, ten); G.filloval (595, 195, ten); G.filloval (595, 595, 10, 10 ); for (int i = 0; i <; i + = 50) {//Draw horizontal Ordinate line g.drawline (+, + I,, + i); G.drawline (+ I, +, + I, 7 50);} for (int i = 0; i < p.length; i++) {//Draw chess on board for (int j = 0; J < P.length; J + +) {if (p[i][j]! = 0) {G.setcolor (p[ I][J] = = 1? Color.black:Color.WHITE); G.fillovAl (+ i *, + J * 50, 50, 50);}}} /** * Moves */public boolean move (int i, Int J, Boolean isblack) {if (p[i][j] = = 0) {P[i][j] = Isblack? 1:2;return true;} return false;} /** * Method: Judgment winner */public Boolean win (int i, Int J, Boolean isblack) {int currentcolor = Isblack? 1:2;if (Counth (i, J, cur Rentcolor) >= 5 | | Countv (i, J, CurrentColor) >= 5| | CountX1 (i, J, CurrentColor) >= 5| | CountX2 (i, J, CurrentColor) >= 5) {return true;} return false;} private int Counth (int i, int j, int currentcolor) {int counter = 1;int tempi = i;while (--tempi >= 0 && p[temp I][J] = = CurrentColor) {counter++;} Tempi = I;while (++tempi <= p.length && p[tempi][j] = = CurrentColor) {counter++;} return counter;} private int Countv (int i, int j, int currentcolor) {int counter = 1;int TEMPJ = j;while (--tempj >= 0 && p[i][t EMPJ] = = CurrentColor) {counter++;} TEMPJ = J;while (++tempj <= p.length && P[I][TEMPJ] = = CurrentColor) {counter++;} return counter;} Private inT countX1 (int i, int j, int currentcolor) {int counter = 1;int Tempi = I;int TEMPJ = j;while (--tempj >= 0 &&- -tempi >= 0 && P[TEMPI][TEMPJ] = = CurrentColor) {counter++;} Tempi = I;TEMPJ = J;while (++tempj <= p.length && ++tempi <= p.length&& P[TEMPI][TEMPJ] = = CurrentCo LOR) {counter++;} return counter;} private int countX2 (int i, int j, int currentcolor) {int counter = 1;int Tempi = I;int TEMPJ = j;while (--tempj >= 0 &A mp;& ++tempi >= 0 && P[TEMPI][TEMPJ] = = CurrentColor) {counter++;} Tempi = I;TEMPJ = J;while (++tempj <= p.length &&--tempi <= p.length&& P[TEMPI][TEMPJ] = = CurrentCo LOR) {counter++;} return counter;}}



Java Programming (12.3)----Listener Primary application: Gobang

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.