1、魔板游戏代码public class PuzzleFrame extends JFrame implements ActionListenerPuzzlePad puzzlePad;JButton 开始;JMenuBar bar;JMenu fileMenu;JMenuItem 初级,高级;JRadioButton 数字玩法,图像玩法;ButtonGroup group=null;Container con=null;public PuzzleFrame()bar=new JMenuBar();fileMenu=new JMenu(“魔板游戏“);初级=new JMenuItem(“初级“);
2、高级=new JMenuItem(“高级“);fileMenu.add(初级);fileMenu.add(高级);bar.add(fileMenu);setJMenuBar(bar);初级.addActionListener(this);高级.addActionListener(this);开始=new JButton(“开始玩“);开始.addActionListener(this);group=new ButtonGroup();数字玩法=new JRadioButton(“数字玩法“,true);图像玩法=new JRadioButton(“图像玩法“,false);group.add(
3、数字玩法);group.add(图像玩法);puzzlePad=new PuzzlePad(3,3,50,50); con=getContentPane();con.add(puzzlePad,BorderLayout.CENTER);JPanel pNorth=new JPanel();pNorth.add(数字玩法);pNorth.add(图像玩法); pNorth.add(开始);con.add(pNorth,BorderLayout.NORTH);con.validate();addWindowListener(new WindowAdapter() public void windo
4、wClosing(WindowEvent e)System.exit(0););setVisible(true);setBounds(100,50,440,360);validate();public void actionPerformed(ActionEvent e) if(e.getSource()=开始)if(数字玩法.isSelected()puzzlePad.随机排列数字();else if(图像玩法.isSelected()puzzlePad.随机排列图像(); else if(e.getSource()=初级)con.remove(puzzlePad);puzzlePad=ne
5、w PuzzlePad(3,3,50,50); con.add(puzzlePad,BorderLayout.CENTER);con.validate();this.validate();else if(e.getSource()=高级)con.remove(puzzlePad);puzzlePad=new PuzzlePad(4,4,50,50); con.add(puzzlePad,BorderLayout.CENTER);con.validate();this.validate();public static void main(String args)new PuzzleFrame()
6、; MobanFrame类:import java.awt.*;import java.awt.event.*;import .*;import java.applet.Applet;import javax.imageio.ImageIO;import java.io.File;import java.awt.image.*;import javax.swing.JOptionPane;public class MobanFrame extends Frame implements ActionListener,MouseListener,ItemListenerMobanSquare po
7、sition; Point startPoint=new Point(100,130); Point rightStartPoint=new Point(400,90); int num; Point pointMove; int totalStep=0; String selectedImage=“狗“; String gamingImage=“ “; boolean startGame=false; int squareNumber=3;int level=1; int squareSize=80;Image sourceImage; Image spaceSourceImage; Ima
8、ge spaceImage;Image myImage; Graphics2D ggg ; /*-*/*显示在界面上的一些组件*/*-*/TextField tfx;TextField tfy;MenuBar mnbMyMenuBar; Menu mnChooseLevel;MenuItem mniLevel1, mniLevel2;Label lbStep; Label lbSuccess=new Label(“);Button btnStartGame;Choice chChoiceImage;public MobanFrame()super(“魔板游戏“);pointMove=new P
9、oint4; pointMove0=new Point(-1,0);pointMove1=new Point(1,0);pointMove2=new Point(0,-1);pointMove3=new Point(0,1);setLayout(new FlowLayout();chChoiceImage=new Choice();chChoiceImage.add(“狗“);chChoiceImage.add(“鸡“);chChoiceImage.add(“鱼“);chChoiceImage.add(“熊猫“);chChoiceImage.add(“大象“);chChoiceImage.ad
10、d(“长颈鹿“);chChoiceImage.add(“牛“);chChoiceImage.add(“小猫“);chChoiceImage.add(“小猫 2“);chChoiceImage.add(“羊“);chChoiceImage.add(“猴 “);chChoiceImage.add(“数字“);mniLevel1=new MenuItem(“简单“);mniLevel2=new MenuItem(“困难“);mnChooseLevel=new Menu(“难度“);mnbMyMenuBar=new MenuBar();tfx=new TextField(8);tfy=new Text
11、Field(8);tfx.setText(“0“);tfy.setText(“1“);tfx.setVisible(false);tfy.setVisible(false);lbStep=new Label(“已走步数: “+Integer.toString(totalStep);btnStartGame=new Button(“开始游戏“);mnChooseLevel.add(mniLevel1);mnChooseLevel.add(mniLevel2);mnbMyMenuBar.add(mnChooseLevel);this.setMenuBar(mnbMyMenuBar);add(tfx
12、);add(tfy);add(lbSuccess);add(lbStep);add(chChoiceImage);add(btnStartGame);mniLevel1.addActionListener(this);mniLevel2.addActionListener(this);btnStartGame.addActionListener(this);chChoiceImage.addItemListener(this);this.addMouseListener(this);addWindowListener(new WindowAdapter() public void window
13、Closing(WindowEvent e) System.exit(0););ProInit();GetImage();GetRandom();setSize(600,300);setVisible(true);public void GetImage()myImage=new ImagesquareNumber*squareNumber;ImageFilter cropFilter1; ImageFilter cropFilter2; ImageFilter cropFilter3;Toolkit tool=getToolkit();sourceImage=tool.createImage
14、(selectedImage+“.jpg“); spaceSourceImage=tool.createImage(“space.jpg“);sourceImage=sourceImage.getScaledInstance(squareSize*squareNumber,squareSize*squareNumber,Image.SCALE_DEFAULT); cropFilter1 =new CropImageFilter(0,0,squareSize*squareNumber,squareSize*squareNumber);sourceImage=createImage(new Fil
15、teredImageSource(sourceImage.getSource(),cropFilter1);for(int i=0;i=startPoint.xpublic void mouseExited(MouseEvent e)public void mouseEntered(MouseEvent e)public void mousePressed(MouseEvent e)public void mouseReleased(MouseEvent e)public static void main(String args) new MobanFrame();MobanSquare类:i
16、mport java.awt.*;import java.awt.event.*;public class MobanSquarepublic int x,y;private int order;private int nowSpaceOrder;private boolean squareHere;public boolean arrive;private Image squareImage;public MobanSquare(int px,int py,int order,int spaceOrder)this.x=px;this.y=py;this.order=order;this.n
17、owSpaceOrder=spaceOrder;if (this.order=this.nowSpaceOrder)arrive=true;elsearrive=false;if (this.order=this.nowSpaceOrder|this.order=-1)squareHere=false;elsesquareHere=true;public boolean GetSquareHere() return this.squareHere;public void SetSquareHere(boolean b)this.squareHere=b;public int GetOrder() return this.order;public void SetOrder(int order) this.order=order;if (this.order=this.nowSpaceOrder)arrive=true;squareHere=false;elsearrive=false;squareHere=true;public Image GetImage()return this.squareImage;public void SetImage(Image ima) this.squareImage=ima;