收藏 分享(赏)

Java模板游戏代码.doc

上传人:精品资料 文档编号:8417800 上传时间:2019-06-25 格式:DOC 页数:17 大小:23.82KB
下载 相关 举报
Java模板游戏代码.doc_第1页
第1页 / 共17页
Java模板游戏代码.doc_第2页
第2页 / 共17页
Java模板游戏代码.doc_第3页
第3页 / 共17页
Java模板游戏代码.doc_第4页
第4页 / 共17页
Java模板游戏代码.doc_第5页
第5页 / 共17页
点击查看更多>>
资源描述

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;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报