阅读背景:

Java 五子棋源程序 实现了基本的功能

来源:互联网 
还有一个要注意的地方,这个得在jdk1.7的环境下编译运行,还得有jmf包
//This is a Gobang program designed by 焦仁瑜 who is a Chinese.Yet,it is a easy work to many black-horse,//however I am proud of me! It costs me a lot of time to do it. During my design of this work I was also //learned many things about Java, and I will continue learning Java until I could undercontroll it! Thank you!import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*; import java.io.File; import javax.media.Manager; import javax.media.MediaLocator; import javax.media.Player; class BlackChess extends Canvas //黑色棋子{public BlackChess(){}public void paint(Graphics g){g.setColor(Color.black);g.fillOval(0,0,24,24);}}class WhiteChess extends Canvas //白色棋子{public WhiteChess(){}public void paint(Graphics g){g.setColor(Color.white);g.fillOval(0,0,24,24);}}class itemListener implements ItemListener{//建立一个监听器类Music music=null;String para=null;static boolean record;public void itemStateChanged(ItemEvent e){if (e.getStateChange() == ItemEvent.SELECTED) {//当选项改变时,会触发两次,JComboBox jcb = (JComboBox) e.getSource(); //一是取消旧的选择,二是添加新的选择int k=jcb.getSelectedIndex();//当前控件的选项索引para=new String(e.getItem().toString());//获得所选择的项的字符串switch(para){case "MUSIC-1":case "MUSIC-2":if(music!=null){music.stop();music=null;}switch(k){case 1:music=new Music("E:\acm\java\music.mp3");break;case 2:music=new Music("E:\acm\java\music2.mp3");break;}break;case "red":case "blue":case "default":switch(k){case 1:Windows.setBground(new Color(159,71,38));break;case 2:Windows.setBground(Color.red);break;case 3:Windows.setBground(Color.blue);break;}break;}}     }}class Music//构造音乐类{Player player=null;public Music(String str) {  File mufile = new File(str); try { if (player == null) { if (mufile.exists()) { MediaLocator locator = new MediaLocator("file:" + mufile.getAbsolutePath()); player = Manager.createRealizedPlayer(locator); player.prefetch(); } } player.start();// 开始播放 } catch (Exception e) { e.getStackTrace();     } } public void stop(){player.stop();player=null;}}class Background extends JPanel{//棋盘大小为25*25public Background(int x, int y, int width, int height, Color color){setBounds(x,y,width,height);setBackground(color);}public void paint(Graphics g){super.paint(g);//这个必须要用,不然背景颜色就画不出来,其实我也不知道这其中的缘由,还有待继续深入研究g.setColor(Color.green);for(int i=0;i<=24;i++){//绘制棋盘g.drawLine(30,30+30*i,750,30+30*i);g.drawLine(30+30*i,30,30+30*i,750);}g.fillOval(30+30*12-5,30+30*12-5,10,10);//绘制棋盘里的五个标志点g.fillOval(30+30*6-5,30+30*6-5,10,10);g.fillOval(30+30*6-5,30+30*18-5,10,10);g.fillOval(30+30*18-5,30+30*6-5,10,10);g.fillOval(30+30*18-5,30+30*18-5,10,10);}}class GobangMenu extends JPanel//棋盘右面的设置菜单,可以选择音乐,棋盘背景颜色{JTextField current;//提示当前该哪一方走和标志哪一方输赢1,2,3,4public GobangMenu(int x, int y, int width, int height, Color color){setLayout(null);setBounds(780,0,220,780);setBackground(Color.YELLOW);JComboBox music=new JComboBox();music.addItem("背景音乐");music.addItem("MUSIC-1");music.addItem("MUSIC-2");music.setBounds(10,30,150,30);music.addItemListener(new itemListener());add(music);JComboBox bgcolor=new JComboBox();bgcolor.addItem("背景颜色");bgcolor.addItem("default");bgcolor.addItem("red");bgcolor.addItem("blue");bgcolor.setBounds(10,70,150,30);bgcolor.addItemListener(new itemListener());add(bgcolor);current=new JTextField("黑方先下");current.setBounds(10,110,150,30);current.setEditable(false);add(current);JRadioButton type1=new JRadioButton();type1.setBounds(10,150,20,20);type1.setSelected(true);add(type1);JRadioButton type2=new JRadioButton();type2.setBounds(10,180,20,20);add(type2);ButtonGroup group=new ButtonGroup();group.add(type1);group.add(type2);JLabel label1=new JLabel("人-人");JLabel label2=new JLabel("人-机");label1.setBounds(30,150,100,20);label2.setBounds(30,180,100,20);add(label1);add(label2);}    public void setText(int a[][],int record){switch(record){case 0:current.setText("该黑方下");break;case 1:current.setText("该白方下");break;case 3:current.setText("黑方胜");JOptionPane.showMessageDialog(this,"Black one win!");break;case 4:current.setText("白方胜");JOptionPane.showMessageDialog(this,"White one win!");break;}}public void paint(Graphics g){super.paint(g);}}class Windows extends JPanel implements MouseListener,ActionListener//创建一个面板,存放先前两个面板{static Background panel1;static GobangMenu panel2;JButton reset,exit;   int a[][];//记录当前格的棋子int current=0;//为零择黑方下,1则白方下int i,j;static int begin;//标记游戏是否已经开始,0代表没有开始,1代表开始public Windows(){setBounds(0,0,1000,800);a= new int[26][26];init(a);//把数组a初始化为零,标记该位置没有棋子panel1=new Background(0,0,780,780,new Color(159,71,38));panel2=new GobangMenu(780,0,220,780,Color.black);panel1.addMouseListener(this);reset=new JButton("Play Again");reset.setBounds(10,220,150,30);reset.addActionListener(this);panel2.add(reset);exit=new JButton("Exit");exit.setBounds(10,260,150,30);exit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}});panel2.add(exit);add(panel1);add(panel2);}public static void setBground(Color color){if(begin==0)panel1.setBackground(color);}public void paintComponent(Graphics g){panel1.repaint();}public void actionPerformed(ActionEvent e){begin=0;init(a);panel1.removeAll();current=0;panel2.setText(a,current);}public void init(int a[][]){for(i=0;i<26;i++)for(j=0;j<26;j++)a[i][j]=0;}public void mouseClicked(MouseEvent e){//鼠标单击事件intx=e.getX();inty=e.getY();begin=1;if(current!=3&¤t!=4){if(x>15&&y>15&&x<765&&y<765&&a[(x+15)/30][(y+15)/30]==0){if(current==0){BlackChess black=new BlackChess();panel1.add(black);black.setBounds(30*((x+15)/30)-12,30*((y+15)/30)-12,24,24);a[(x+15)/30][(y+15)/30]=1;current=1;}else{WhiteChess white=new WhiteChess();panel1.add(white);white.setBounds(30*((x+15)/30)-12,30*((y+15)/30)-12,24,24);current=0;a[(x+15)/30][(y+15)/30]=2;}current=judge(x,y,a);panel2.setText(a,current);}}else{panel2.setText(a,current);}}public void mousePressed(MouseEvent e){}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}public void mouseReleased(MouseEvent e){}   public int judge(int x, int y, int a[][]){//判断输赢int i,j;int x1,y1;int count=0;x1=(x+15)/30;y1=(y+15)/30;for(i=x1;a[i][y1]==a[x1][y1]&&i<=26;i++)count++;for(i=x1-1;a[i][y1]==a[x1][y1]&&i>0;i--)count++;if(count>=5)return (a[x1][y1]+2);count=0;for(i=y1;a[x1][i]==a[x1][y1]&&i<=26;i++)count++;for(i=y1-1;a[x1][i]==a[x1][y1]&&i>0;i--)count++;if(count>=5)return (a[x1][y1]+2);count=0;for(i=x1,j=y1;a[i][j]==a[x1][y1]&&i<=26&&j<=26;i++,j++)count++;for(i=x1-1,j=y1-1;a[i][j]==a[x1][y1]&&i>0&&j>0;i--,j--)count++;if(count>=5)return (a[x1][y1]+2);count=0;for(i=x1,j=y1;a[i][j]==a[x1][y1]&&i<=26&&j>0;i++,j--)count++;for(i=x1-1,j=y1+1;a[i][j]==a[x1][y1]&&i>0&&j<=26;i--,j++)count++;if(count>=5)return (a[x1][y1]+2);return 2-a[x1][y1];}}public class  Gobang extends JFrame//还有一个问题没有解决,就是窗口一缩小,原来画的棋盘和按钮都不见啦!{public Gobang(){setSize(1000,800);setVisible(true);setLayout(null);Windows panel=new Windows();add(panel);panel.repaint();setDefaultLookAndFeelDecorated(false);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {Gobang gobang=new Gobang();}}//This 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: