《java拼图游戏完整代码》由会员分享,可在线阅读,更多相关《java拼图游戏完整代码(12页珍藏版)》请在金锄头文库上搜索。
1、/ Cell.java package cn.itcast.picture.ui; import javax.swing.Icon; import javax.swing.JButton; /* * 图片小方格类 */ public class Cell extends JButton /带有图片的小方格 public Cell(Icon icon) super(icon); /设置小方格大小 this.setSize(150, 150); /带有图片和文字的小方格 public Cell(String text, Icon icon) super(text, icon); /设置小方格大小
2、this.setSize(150, 150); this.setHorizontalTextPosition(CENTER);/设置文字水平居中显示 this.setVerticalTextPosition(CENTER);/设置文字垂直居中显示 / public void move(String direction) switch (direction) case “UP“: this.setLocation(this.getBounds().x,this.getBounds().y-150); break; case “DOWN“: this.setLocation(this.getBou
3、nds().x,this.getBounds().y+150); break; case “LEFT“: this.setLocation(this.getBounds().x-150,this.getBounds().y); break; case “RIGHT“: this.setLocation(this.getBounds().x+150,this.getBounds().y); break; default: break; /PictureCanvas.java package cn.itcast.picture.ui; import java.awt.Rectangle; impo
4、rt java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.JPanel; /* * 拼图类 */ public class PictureCanvas extends JPanel implements MouseListener /静态变量 public static int pictureID = 1;/图片 ID public static int st
5、epNum = 0;/步数 private Cell cell; private boolean hasAddActionListener = false;/表示是否为小方格添加了点击监听, 有为 true private Rectangle nullCell; /构造方法 public PictureCanvas() initCanvas(); /初始化 public void initCanvas() /设置拼图区的位置 this.setLayout(null);/帧布局 /创建 12 个小方格,并添加到拼图区 cell = new Cell12; for(int i = 0;i comb
6、oBox; private JTextField name; public static JTextField step; private JButton start; / 空参数构造方法 public PictureMainFrame() /super(); init();/界面初始化 addComponent();/添加组建 addPreviewImage();/添加预览图片与拼图图片 addActionListener();/为组建添加事件监听 private void addActionListener() /数值提示 addNumberInfo.addActionListener(n
7、ew ActionListener() Override public void actionPerformed(ActionEvent e) /完成数字提示的显示 canvas.reloadPictureAddNumber(); ); clearNumberInfo.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) /完成数值提示的清除 canvas.reloadPictureClearNumber(); ); comboBox.addItemListener(
8、new ItemListener() Override public void itemStateChanged(ItemEvent e) /获取选择图片的序号 int num = comboBox.getSelectedIndex(); / 更新预览区 PictureCanvas.pictureID = num+1; preview.repaint();/重新绘制预览区界面 /更新拼图区 canvas.removeAll(); canvas.initCanvas(); canvas.repaint(); /canvas.reloadPictureClearNumber(); /更新游戏状态区
9、 name.setText(“图片名称:“+comboBox.getSelectedItem(); PictureCanvas.stepNum = 0; step.setText(“步数:“+0); /更新按钮区 clearNumberInfo.setSelected(true); ); start.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) / 更新游戏状态区 步数:0 PictureCanvas.stepNum = 0; step.setText(“步数
10、:0“); /将小方格位置随机打乱 canvas.start(); ); private void addPreviewImage() /创建面板,包含拼图区和预览区 JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1,2);/设置成 1X2 的表格布局 canvas = new PictureCanvas(); canvas.setBorder(new TitledBorder(“拼图区“); preview = new PicturePreview(); preview.setBorder(new TitledBord
11、er(“预览区“); panel.add(canvas,BorderLayout.WEST); panel.add(preview,BorderLayout.EAST); /- /把面板显示在主界面中 this.add(panel,BorderLayout.CENTER); private void addComponent() JPanel panel = new JPanel(); panel.setBackground(Color.PINK); panel.setLayout(new GridLayout(1, 2); JPanel leftPanel = new JPanel(); l
12、eftPanel.setBackground(Color.PINK); leftPanel.setBorder(new TitledBorder(“按钮区“); addNumberInfo = new JRadioButton(“数字提示“,false); addNumberInfo.setBackground(Color.PINK); clearNumberInfo = new JRadioButton(“清楚提示“,true); clearNumberInfo.setBackground(Color.PINK); comboBox = new JComboBox(items); combo
13、Box.setPreferredSize(new Dimension(100, 24); start = new JButton(“开始“); start.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); start.setBackground(Color.PINK); ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(addNumberInfo); buttonGroup.add(clearNumberInfo); leftPanel.add(addNum
14、berInfo); leftPanel.add(clearNumberInfo); leftPanel.add(new JLabel(“选择图片:“); leftPanel.add(comboBox); leftPanel.add(start); JPanel rightPanel = new JPanel(); rightPanel.setBackground(Color.PINK); rightPanel.setBorder(new TitledBorder(“游戏状态“); rightPanel.setLayout(new GridLayout(1,2); name = new JTex
15、tField(“图片名称:小女孩“); step = new JTextField(“步数:0“); name.setEditable(false); step.setEditable(false); rightPanel.add(name, BorderLayout.WEST); rightPanel.add(step, BorderLayout.EAST); panel.add(leftPanel, BorderLayout.WEST); panel.add(rightPanel, BorderLayout.EAST); this.add(panel, BorderLayout.NORTH
16、); private void init() this.setBounds(new Rectangle(150, 25, 1000, 720); this.setTitle(“拼图游戏“); this.setResizable(false); /设置窗口默认关闭操作 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /PicturePreview.java package cn.itcast.picture.ui; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JPanel;