先看下整体界面吧
再来说说这个所谓的创意。以前接触过一个算法,叫“八皇后问题”。算法描述:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。我是把这个算法描述变成我的游戏规则,让两个玩家摆放皇后,甲先摆放,乙再摆放,直到不能找到皇后位置为止。而不能找到位置的那一方就是输方。我是用java语言编写的,先整体构造平面结构,再在棋盘上面做文章。整体是用的GridLayout(0,2)的结构,即一行两列。左边一列是棋盘,也是我们游戏的重点,右边一列又分为了五行,放置了Jlabel,Jbutton等组件。
首先来谈一谈棋盘,用paint函数来绘制,通过坐标定位绘制国际象棋盘。那么如何实现皇后的摆放呢?我用了MouseListener来监听,override的函数是mouseClicked(),把鼠标监听add到棋盘上,即利用panel.addMouseListener(getmousepos)。mouseClicked()函数的具体功能是给出鼠标点击的位置坐标。我来说一下这个过程。当鼠标移到棋盘上,并且点击棋盘的某个位置的时候,会得到我点击的位置的坐标。利用这个坐标我们就可以paint一个皇后图像,以记录玩家所找到的皇后位置。每点击一次,整个平面就repaint一次,展示到目前为止所找到的皇后位。
可以看一下paint这部分的代码
class paintPanel extends Panel { public Image redimage,blueimage; public ImageObserver ImageObserver; { try { redimage = ImageIO.read(new FileInputStream("redqueen.png")); blueimage = ImageIO.read(new FileInputStream("bluequeen.png")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void paint(Graphics g) { int i,j,tmp; //public Image image; //public ImageObserver ImageObserver; /*{ try { if(color==0) { image = ImageIO.read(new FileInputStream("redqueen.png")); } else { image = ImageIO.read(new FileInputStream("bluequeen.png")); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ for(i=1;i<=9;i++) { g.drawLine(70,70*i,630,70*i); } for(i=1;i<=9;i++) { g.drawLine(70*i,70,70*i,630); } for(i=1;i<=8;i++) { if(i%2==1) { tmp=1; } else { tmp=2; } for(;tmp<=8;tmp+=2) { g.setColor(Color.black); g.fillRect(70*tmp,70*i,70,70); } } if(start) { for(i=0;i<9;i++) { //g.drawImage(image, 10, 10, 20, 20, 0, 0, 30, 30,ImageObserver); for(j=0;j<9;j++) { if(pos[i][j]!=0) { if(color[i][j]==1) { g.drawImage(blueimage, 5+70*i, 5+70*j,60+70*i,60+70*j, 0, 0, 70, 70,ImageObserver); } else { g.drawImage(redimage, 5+70*i, 5+70*j,60+70*i,60+70*j, 0, 0, 70, 70,ImageObserver); } } } } } } }
接下来谈一谈平面右半部分的组件。JLabel就是用来显示文本内容的,不赘述,我重点谈一下JButton。可以看到我用了两个JButton组件,它们都add了一个actionlistener,用来记录先行放选择的队伍颜色,展示一下actionlistener部分的代码:
ActionListener getredColor= new ActionListener() { @Override public void actionPerformed(ActionEvent buttonevent) { // TODO Auto-generated method stub chess.time=0; first_qblankLabel_two.setForeground(Color.red); first_qblankLabel_two.setText("选择"); first_qblankLabel_four.setForeground(Color.red); first_qblankLabel_four.setText("红队"); } }; ActionListener getblueColor= new ActionListener() { @Override public void actionPerformed(ActionEvent buttonevent) { // TODO Auto-generated method stub chess.time=1; first_qblankLabel_two.setForeground(Color.blue); first_qblankLabel_two.setText("选择"); first_qblankLabel_four.setForeground(Color.blue); first_qblankLabel_four.setText("蓝队"); } };
基本上几个重要的代码部分就是这些了,那么程序如何按部就班的一一实现这些功能呢?public run()函数里面都写明白了。注意:在构造界面的时候,按照你设计的摆放组件的顺序来add到画布中,先整体规划,再规划细节。比如Fourq这个部分我在内部又分成了好几个部分,依次将设计的组件add进去。
public void run() { Fourq.add(first_qblankLabel_one); Fourq.add(first_qblankLabel_two); Fourq.add(first_qblankLabel_three); Fourq.add(first_qblankLabel_four); Fourq.add(first_qblankLabel_five); Fourq.add(redButton); Fourq.add(second_qblankLabel_one); Fourq.add(second_qblankLabel_two); Fourq.add(blueButton); Fourq.add(second_qblankLabel_three); Fourq.add(last_qblankLabel_one); Fourq.add(last_qblankLabel_two); Fourq.add(last_qblankLabel_three); Fourq.add(last_qblankLabel_four); Fourq.add(last_qblankLabel_five); p.add(Onepanel); p.add(TwoLabel); p.add(ThreeLabel); p.add(Fourq); p.add(FiveLabel); panel.addMouseListener(getmousepos); redButton.addActionListener(getredColor); blueButton.addActionListener(getblueColor); chess.frame.add(panel); chess.frame.add(p); chess.frame.setSize(700,500); chess.frame.setVisible(true); }
下面我截几个游戏过程图:
任何建议可以e-mail联系我:2293294379@qq.com
整体代码:
import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Image; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.ImageObserver; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.*; public class paintChess { public JFrame frame; boolean start=false; public int time; protected int getX; protected int getY; public int[][] pos=new int[9][9]; public int[][] color=new int[9][9]; @SuppressWarnings("serial") class paintPanel extends Panel { public Image redimage,blueimage; public ImageObserver ImageObserver; { try { redimage = ImageIO.read(new FileInputStream("redqueen.png")); blueimage = ImageIO.read(new FileInputStream("bluequeen.png")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void paint(Graphics g) { int i,j,tmp; //public Image image; //public ImageObserver ImageObserver; /*{ try { if(color==0) { image = ImageIO.read(new FileInputStream("redqueen.png")); } else { image = ImageIO.read(new FileInputStream("bluequeen.png")); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ for(i=1;i<=9;i++) { g.drawLine(70,70*i,630,70*i); } for(i=1;i<=9;i++) { g.drawLine(70*i,70,70*i,630); } for(i=1;i<=8;i++) { if(i%2==1) { tmp=1; } else { tmp=2; } for(;tmp<=8;tmp+=2) { g.setColor(Color.black); g.fillRect(70*tmp,70*i,70,70); } } if(start) { for(i=0;i<9;i++) { //g.drawImage(image, 10, 10, 20, 20, 0, 0, 30, 30,ImageObserver); for(j=0;j<9;j++) { if(pos[i][j]!=0) { if(color[i][j]==1) { g.drawImage(blueimage, 5+70*i, 5+70*j,60+70*i,60+70*j, 0, 0, 70, 70,ImageObserver); } else { g.drawImage(redimage, 5+70*i, 5+70*j,60+70*i,60+70*j, 0, 0, 70, 70,ImageObserver); } } } } } } } public static void main(String[] args)throws Exception { final paintChess chess=new paintChess(); chess.frame=new JFrame(); chess.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); chess.frame.setLayout(new GridLayout(0,2)); final JPanel p=new JPanel(new GridLayout(5,1)); final JPanel Onepanel=new JPanel(new GridLayout(2,1)); final JLabel OneFirst=new JLabel(); OneFirst.setFont(new java.awt.Font("Dialog",1,20)); OneFirst.setForeground(Color.black); OneFirst.setText("规则:在棋盘上摆放皇后,使得任意两个皇后不在同一行、同一列、同一斜线上。双方交错摆放,"); final JLabel OneSecond=new JLabel(); OneSecond.setFont(new java.awt.Font("Dialog",1,20)); OneSecond.setForeground(Color.black); OneSecond.setText("直到某方不能再找出一个皇后位,则该方输。游戏开始之前,先行一方先选择代表的队伍。"); Onepanel.add(OneFirst); Onepanel.add(OneSecond); final JLabel TwoLabel=new JLabel(); TwoLabel.setSize(100, 40); TwoLabel.setFont(new java.awt.Font("Dialog",1,40)); TwoLabel.setForeground(Color.blue); TwoLabel.setText("八皇后棋类游戏(directed by lrr from CQUPT)"); final JLabel ThreeLabel=new JLabel(); final JPanel Fourq=new JPanel(new GridLayout(3,5)); final JLabel first_qblankLabel_one=new JLabel(); final JLabel first_qblankLabel_two=new JLabel(); first_qblankLabel_two.setFont(new java.awt.Font("Dialog",1,30)); final JLabel first_qblankLabel_three=new JLabel(); final JLabel first_qblankLabel_four=new JLabel(); first_qblankLabel_four.setFont(new java.awt.Font("Dialog",1,30)); final JLabel first_qblankLabel_five=new JLabel(); final JButton redButton=new JButton(); redButton.setSize(5,5); redButton.setFont(new java.awt.Font("Dialog",1,20)); redButton.setText("红队"); final JLabel second_qblankLabel_one=new JLabel(); final JLabel second_qblankLabel_two=new JLabel(); final JButton blueButton=new JButton(); blueButton.setSize(5,5); blueButton.setFont(new java.awt.Font("Dialog",1,20)); blueButton.setText("蓝队"); final JLabel second_qblankLabel_three=new JLabel(); final JLabel last_qblankLabel_one=new JLabel(); final JLabel last_qblankLabel_two=new JLabel(); final JLabel last_qblankLabel_three=new JLabel(); final JLabel last_qblankLabel_four=new JLabel(); final JLabel last_qblankLabel_five=new JLabel(); final JLabel FiveLabel=new JLabel(); final paintPanel panel=chess.new paintPanel(); SwingUtilities.invokeLater(new Runnable() { MouseListener getmousepos=new MouseListener() { @Override public void mouseClicked(MouseEvent event) { chess.getX=event.getX(); chess.getY=event.getY(); System.out.println(" X:"+chess.getX+" Y:"+chess.getY); chess.pos[chess.getX/70][chess.getY/70]=1; chess.color[chess.getX/70][chess.getY/70]=chess.time%2; chess.time++; chess.start=true; panel.repaint(); //System.out.println("yes"); } public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } }; ActionListener getredColor= new ActionListener() { @Override public void actionPerformed(ActionEvent buttonevent) { // TODO Auto-generated method stub chess.time=0; first_qblankLabel_two.setForeground(Color.red); first_qblankLabel_two.setText("选择"); first_qblankLabel_four.setForeground(Color.red); first_qblankLabel_four.setText("红队"); } }; ActionListener getblueColor= new ActionListener() { @Override public void actionPerformed(ActionEvent buttonevent) { // TODO Auto-generated method stub chess.time=1; first_qblankLabel_two.setForeground(Color.blue); first_qblankLabel_two.setText("选择"); first_qblankLabel_four.setForeground(Color.blue); first_qblankLabel_four.setText("蓝队"); } }; public void run() { Fourq.add(first_qblankLabel_one); Fourq.add(first_qblankLabel_two); Fourq.add(first_qblankLabel_three); Fourq.add(first_qblankLabel_four); Fourq.add(first_qblankLabel_five); Fourq.add(redButton); Fourq.add(second_qblankLabel_one); Fourq.add(second_qblankLabel_two); Fourq.add(blueButton); Fourq.add(second_qblankLabel_three); Fourq.add(last_qblankLabel_one); Fourq.add(last_qblankLabel_two); Fourq.add(last_qblankLabel_three); Fourq.add(last_qblankLabel_four); Fourq.add(last_qblankLabel_five); p.add(Onepanel); p.add(TwoLabel); p.add(ThreeLabel); p.add(Fourq); p.add(FiveLabel); panel.addMouseListener(getmousepos); redButton.addActionListener(getredColor); blueButton.addActionListener(getblueColor); chess.frame.add(panel); chess.frame.add(p); chess.frame.setSize(700,500); chess.frame.setVisible(true); } }); } }