本文实例讲述了 java 实现的五子棋游戏代码 , 分享给大家供大家参考 具体代码如下
paquete gyb.exam; import java.awt.bordayout; import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.graphics; import java.awt.point; import java.awt.toolkit; import java.awt.event.accionEvent; importar.awt.event.event.event.event.event. java.awt.event.mouseadapter; import java.awt.event.mouseEvent; import java.util.arrayList; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.swing.jmenuit; import javax.Joption.Joption javax.swing.jpanel; import javax.swing.border.titledborder; Public Class Mainframe extiende Jframe {private static int big = 15; Private estático final int uno = 25; Private estático final int inicio = 10; privado estático int x = big*inicio/2; privado estático int y = big*inicio/2; Private int hl = big*one+start; privado isblack de booleano estático; CanvasFrame Canvas = new CanvasFrame (); Canvasframe puede; Gráficos G; público mainframe () {init (); } punto de punto privado; Private ArrayList <Point> BlackList = New ArrayList <Point> (); Private ArrayList <Point> whitelist = new ArrayList <Point> (); private void init () {jMenubar bar = new JMenubar (); setJMenubar (bar); setSize (600,600); settitle ("五子棋"); Jmenu menú = new JMenu ("Juego"); bar.add (menú); JMenuitem start = new JMenuitem ("inicio"); JMenuitem End = new JMenuitem ("棋盘大小"); end.addactionListener (new ActionListener () {public void ActionPerformed (ActionEvent E) {}}); JMenuitem Renew = new JMenuitem ("重新开始"); JMenuitem EXIT = new JMenuitem ("退出"); exit.AdDactionListener (new ActionListener () {public void ActionPerformed (ActionEvent E) {System.exit (0);}}); renovar.AdDactionListener (new ActionListener () {public void ActionPerformed (ActionEvent E) {BlackList.Clear (); Whitelist.Clear ();}}); menú.add (inicio); menú.add (renovar); menú.add (final); menú.add (salida); setLayout (new BorderLayout ()); agregar (borderLayout.center, createCenter ()); Agregar (BorderLayout.east, CreateEast ()); } private boolean iswin (ArrayList <Point> list) {int x; int y; int idx = 0; bucle: para (punto p: list) {x = px; y = py; idx = 0; while (list.contains (nuevo punto (x, y))) {idx ++; x = x-one; if (idX == 5) bucle de interrupción; } x = px; y = py; idx = 0; while (list.contains (nuevo punto (x, y))) {idx ++; y = y-one; if (idX == 5) bucle de interrupción; } x = px; y = py; idx = 0; while (list.contains (nuevo punto (x, y))) {idx ++; x = x-one; y = y-one; if (idX == 5) bucle de interrupción; } x = px; y = py; idx = 0; while (list.contains (nuevo punto (x, y))) {idx ++; x = x-one; y = y+uno; if (idX == 5) bucle de interrupción; }} return idx == 5; } private jpanel createEast () {jpanel pane = new jpanel (); BorderLayout border = new BorderLayout (); pane.setLayout (borde); panele.setBorder (nuevo tituladoBorDer ("五子棋")); Icon Image = new ImageCon (getClass (). GetResource ("mm.png")); pane.add (borderLayout.north, new Jlabel (imagen, jlabel.center)); pane.add (borderLayout.center, ballcolor ()); pane.add (BorderLayout.South, Button ()); Panel de regreso; } private jpanel ballcolor () {jpanel pane = new jPanel (); pane.setLayout (new BorderLayout ()); can = new CanvasFrame () {public void Paint (Graphics g) {g.setColor (color.pink); G.FillRect (1, 1,200,300); G.SetFont (nueva fuente ("Sans", Font.Bold, 20)); G.SetColor (color.black); G.Filloval (55, 20,25,30); G.DrawString ("黑子", 10, 43); G.SetColor (color.white); G.Filloval (55, 60,25,30); G.DrawString ("白子", 10, 83); if (isblack) {g.setColor (color.black); G.DrawString ("黑方落子", 5, 120); } else {g.setColor (color.white); G.DrawString ("白方落子", 5,120); } can.Repaint (); }}; pane.add (borderLayout.center, Can); Panel de regreso; } botón privado jpanel () {jpanel pane = new jpanel (); BordLayout bor = new BorderLayout (); JButton Start = new JButton ("开始"); JButton End = new JButton ("结束"); end.addactionListener (new ActionListener () {public void ActionPerformed (ActionEvent E) {System.exit (0);}}); pane.setLayout (BOR); bor.setvGap (5); panele.setBorder (nuevo tituladoBorder ("")); pane.add (borderLayout.north, inicio); panele.add (BorderLayout.South, Fin); Panel de regreso; } private jpanel createCenter () {jpanel pane = new jPanel (); Canvas.setPreferedSize (nueva dimensión (HL+10, HL+10)); Canvas.AddMouseListener (new Mouseadapter () {public void MouseClicked (MouseEvent e) {loop: if (e.getButton () == mouseevent.Button1) {x = e.getx (); y = e.gety (); int x = (x/uno)*one; int y = (y/uno)*uno; punto = nuevo (x, x,); if (! (BlackList.Contains (Point) || Whitelist.Add (punto); isblack =! isBlack; panel.add (lienzo); Panel de regreso; } public static void main (string [] args) {mainframe frame = new mainframe (); Frame.center (marco); Frame.pack (); Frame.SetVisible (verdadero); } public void Center (JFRame Frame) {// Toolkit 是当前 绘图系统集合工具包 Toolkit Toolkit = Toolkit.getDefaultToolkit (); Dimension Screen = Toolkit.getScreensize (); // 屏幕的宽高 int FrameWidth = Frame.getWidth (); int framehigh = frame.getheight (); int x = (screen.width-framewidth)/2; int y = (Screen.Height-Framehigh)/2; Frame.setLocation (x, y); } class Canvasframe extiende jpanel {public void Paint (Graphics g) {G.SetColor (color.pink); G.Fillrect (0, 0, HL+20, HL+20); G.SetColor (color.black); for (int i = start; i <= hl; i = i+one) {for (int j = start; j <= hl; j = j+one) {g.drawline (i, j, hl, j); }} para (int i = start; i <= hl; i = i+one) {for (int j = start; j <= hl; j = j+one) {g.drawline (i, j, i, hl); }} G.SetColor (color.yellow); g.drawrect (inicio/2, inicio/2, hl+1, hl+1); G.SetColor (color.black); para (Point Point: Blacklist) {G.Filloval (Point.x, Point.y, 20, 25); } G.SetColor (color.white); para (Point Point: Whitelist) {G.Filloval (Point.x, Point.y, 20, 25); }}}}效果图 :
以上所述就是本文的全部内容了 希望能够对大家熟练掌握 希望能够对大家熟练掌握 java 有所帮助。