1. 程式人生 > >Java小項目之拼圖遊戲

Java小項目之拼圖遊戲

jcombobox ava 初始 tcl als check sel 標題 field

首先聲明,代碼是自創的,如有雷同,不勝榮幸!

先談談思路

  1.設計界面。

  2.素材的處理。

  3.設計圖片加載區域的圖片加載處理類。

  4.設計按鈕組中的按鈕初始化方法。

  5.設計按鈕組中的隨機圖片加載方法。

  6.設計重置按鈕方法。

  7.設計按鈕監聽器。

  8.設計判定勝利的條件的方法。

1.界面的設計:

  a.使用了Windows Builder插件,安裝網頁:WindoswBuilder - http://download.eclipse.org/windowbuilder/WB/integration/4.6/

  b.頁面的整體整體樣式:

技術分享

   一個JFram中添加三個帶標題的JPanel,將整個分割為遊戲選項區域,圖片展示區域和遊戲區域。每個區域都有各自的控件,其中需要註意的是圖片展示區域放置了一個JLable來加載展示圖片。

  c.代碼:我將源碼放在了com.rookie.view包下,並且類名為GameView。

  1 package com.rookie.view;
  2 
  3 import java.awt.EventQueue;
  4 
  5 import javax.swing.JFrame;
  6 import javax.swing.JPanel;
  7 import javax.swing.border.EmptyBorder;
  8 import javax.swing.JMenuBar;
  9 import javax.swing.JMenu;
 10 import javax.swing.JMenuItem;
11 import javax.swing.JOptionPane; 12 13 14 import java.awt.event.ActionListener; 15 import java.util.Timer; 16 import java.util.TimerTask; 17 import java.awt.event.ActionEvent; 18 import javax.swing.GroupLayout; 19 import javax.swing.GroupLayout.Alignment; 20 import javax.swing.border.TitledBorder;
21 22 import com.rookie.dao.PicloadDao; 23 import com.rookie.dao.GameDao; 24 25 import javax.swing.UIManager; 26 import java.awt.Color; 27 import javax.swing.LayoutStyle.ComponentPlacement; 28 import javax.swing.JLabel; 29 import javax.swing.JRadioButton; 30 import javax.swing.ButtonGroup; 31 import javax.swing.JComboBox; 32 import javax.swing.DefaultComboBoxModel; 33 import javax.swing.JTextField; 34 import javax.swing.JButton; 35 import javax.swing.SwingConstants; 36 37 public class GamerView extends JFrame { 38 39 /** 40 * 41 */ 42 private static final long serialVersionUID = 1L; 43 private JPanel mainPanel; 44 private final ButtonGroup buttonGroup = new ButtonGroup(); 45 private static JTextField textField_time; 46 private static JButton bt_GameBegin = null; 47 private static JLabel jl_loadImage = null; 48 private static JComboBox comboBox_SelectPic = null; 49 private static JRadioButton rb_simple = null; 50 private static JRadioButton rb_difficulty = null; 51 private static JPanel panel_beginGame = null; 52 private static GameDao gameChoseDao; 53 private static int time = 0; 54 private static Timer timer; 55 /** 56 * Launch the application. 57 */ 58 public static void main(String[] args) { 59 EventQueue.invokeLater(new Runnable() { 60 public void run() { 61 try { 62 GamerView frame = new GamerView(); 63 frame.setVisible(true); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 }); 69 } 70 71 /** 72 * Create the frame. 73 */ 74 public GamerView() { 75 setResizable(false); 76 setTitle("\u62FC\u56FE\u6E38\u620F(\u6D4B\u8BD5\u7248)"); 77 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 78 setBounds(100, 100, 710, 550); 79 80 JMenuBar menuBar = new JMenuBar(); 81 setJMenuBar(menuBar); 82 83 JMenu m_About = new JMenu("\u5173\u4E8E"); 84 menuBar.add(m_About); 85 86 JMenuItem mI_aboutMe = new JMenuItem("\u56E2\u961F\u4ECB\u7ECD"); 87 mI_aboutMe.addActionListener(new ActionListener() { 88 public void actionPerformed(ActionEvent e) { 89 showAboutActionListener(e); 90 } 91 }); 92 m_About.add(mI_aboutMe); 93 mainPanel = new JPanel(); 94 mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 95 setContentPane(mainPanel); 96 97 JPanel panel_checkGame = new JPanel(); 98 panel_checkGame.setBorder(new TitledBorder(null, "\u6E38\u620F\u9009\u9879\uFF1A", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED)); 99 100 JPanel panel_loadPic = new JPanel(); 101 panel_loadPic.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u539F\u56FE\u52A0\u8F7D\uFF1A", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED)); 102 panel_loadPic.setToolTipText(""); 103 104 panel_beginGame = new JPanel(); 105 panel_beginGame.setBorder(new TitledBorder(null, "\u6E38\u620F\u5F00\u59CB\uFF1A", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED)); 106 GroupLayout gl_mainPanel = new GroupLayout(mainPanel); 107 gl_mainPanel.setHorizontalGroup( 108 gl_mainPanel.createParallelGroup(Alignment.TRAILING) 109 .addGroup(gl_mainPanel.createSequentialGroup() 110 .addContainerGap() 111 .addGroup(gl_mainPanel.createParallelGroup(Alignment.TRAILING) 112 .addComponent(panel_checkGame, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE) 113 .addGroup(Alignment.LEADING, gl_mainPanel.createSequentialGroup() 114 .addComponent(panel_loadPic, GroupLayout.PREFERRED_SIZE, 336, GroupLayout.PREFERRED_SIZE) 115 .addPreferredGap(ComponentPlacement.UNRELATED) 116 .addComponent(panel_beginGame, GroupLayout.PREFERRED_SIZE, 332, GroupLayout.PREFERRED_SIZE))) 117 .addContainerGap()) 118 ); 119 gl_mainPanel.setVerticalGroup( 120 gl_mainPanel.createParallelGroup(Alignment.LEADING) 121 .addGroup(gl_mainPanel.createSequentialGroup() 122 .addContainerGap() 123 .addComponent(panel_checkGame, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE) 124 .addPreferredGap(ComponentPlacement.UNRELATED) 125 .addGroup(gl_mainPanel.createParallelGroup(Alignment.BASELINE) 126 .addComponent(panel_loadPic, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE) 127 .addComponent(panel_beginGame, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)) 128 .addContainerGap()) 129 ); 130 131 jl_loadImage = new JLabel(""); 132 jl_loadImage.setIcon(null); 133 panel_loadPic.add(jl_loadImage); 134 135 136 137 GroupLayout gl_panel_beginGame = new GroupLayout(panel_beginGame); 138 gl_panel_beginGame.setHorizontalGroup( 139 gl_panel_beginGame.createParallelGroup(Alignment.LEADING) 140 .addGap(0, 320, Short.MAX_VALUE) 141 ); 142 gl_panel_beginGame.setVerticalGroup( 143 gl_panel_beginGame.createParallelGroup(Alignment.LEADING) 144 .addGap(0, 322, Short.MAX_VALUE) 145 ); 146 panel_beginGame.setLayout(gl_panel_beginGame); 147 148 JLabel label = new JLabel("\u6E38\u620F\u96BE\u5EA6\uFF1A"); 149 label.setForeground(Color.BLUE); 150 151 rb_simple = new JRadioButton("\u7B80\u5355"); 152 buttonGroup.add(rb_simple); 153 154 rb_difficulty = new JRadioButton("\u56F0\u96BE"); 155 buttonGroup.add(rb_difficulty); 156 157 rb_simple.setSelected(true); 158 159 JLabel label_1 = new JLabel("\u56FE\u7247\u9009\u62E9\uFF1A"); 160 label_1.setForeground(Color.BLUE); 161 162 comboBox_SelectPic = new JComboBox(); 163 comboBox_SelectPic.setModel(new DefaultComboBoxModel(new String[] {"\u98CE\u666F", "\u7F8E\u5973", "\u8C6A\u8F66", "\u6E38\u620F"})); 164 comboBox_SelectPic.setMaximumRowCount(5); 165 166 JLabel label_2 = new JLabel("\u6E38\u620F\u65F6\u95F4\uFF1A"); 167 label_2.setForeground(Color.BLUE); 168 169 textField_time = new JTextField(); 170 textField_time.setHorizontalAlignment(SwingConstants.RIGHT); 171 textField_time.setText("0"); 172 textField_time.setEditable(false); 173 textField_time.setColumns(10); 174 175 JLabel lblNewLabel = new JLabel("\u79D2"); 176 lblNewLabel.setForeground(Color.BLUE); 177 178 bt_GameBegin = new JButton("\u5F00\u59CB\u6E38\u620F"); 179 bt_GameBegin.addActionListener(new ActionListener() { 180 public void actionPerformed(ActionEvent eve) { 181 buttonClickAction(eve); 182 } 183 }); 184 GroupLayout gl_panel_checkGame = new GroupLayout(panel_checkGame); 185 gl_panel_checkGame.setHorizontalGroup( 186 gl_panel_checkGame.createParallelGroup(Alignment.LEADING) 187 .addGroup(gl_panel_checkGame.createSequentialGroup() 188 .addContainerGap() 189 .addComponent(label) 190 .addGap(6) 191 .addGroup(gl_panel_checkGame.createParallelGroup(Alignment.TRAILING) 192 .addComponent(rb_difficulty) 193 .addGroup(gl_panel_checkGame.createSequentialGroup() 194 .addComponent(rb_simple) 195 .addPreferredGap(ComponentPlacement.RELATED))) 196 .addGap(18) 197 .addComponent(label_1) 198 .addPreferredGap(ComponentPlacement.UNRELATED) 199 .addComponent(comboBox_SelectPic, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE) 200 .addGap(32) 201 .addComponent(label_2) 202 .addPreferredGap(ComponentPlacement.UNRELATED) 203 .addComponent(textField_time, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE) 204 .addPreferredGap(ComponentPlacement.RELATED) 205 .addComponent(lblNewLabel) 206 .addGap(52) 207 .addComponent(bt_GameBegin, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE) 208 .addContainerGap(76, Short.MAX_VALUE)) 209 ); 210 gl_panel_checkGame.setVerticalGroup( 211 gl_panel_checkGame.createParallelGroup(Alignment.LEADING) 212 .addGroup(gl_panel_checkGame.createSequentialGroup() 213 .addGroup(gl_panel_checkGame.createParallelGroup(Alignment.LEADING) 214 .addGroup(gl_panel_checkGame.createSequentialGroup() 215 .addComponent(rb_simple) 216 .addPreferredGap(ComponentPlacement.RELATED, 7, Short.MAX_VALUE) 217 .addComponent(rb_difficulty)) 218 .addGroup(gl_panel_checkGame.createSequentialGroup() 219 .addContainerGap() 220 .addGroup(gl_panel_checkGame.createParallelGroup(Alignment.BASELINE) 221 .addComponent(label_1) 222 .addComponent(comboBox_SelectPic, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 223 .addComponent(textField_time, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 224 .addComponent(lblNewLabel) 225 .addComponent(label_2) 226 .addComponent(bt_GameBegin, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))) 227 .addGroup(gl_panel_checkGame.createSequentialGroup() 228 .addGap(22) 229 .addComponent(label))) 230 .addContainerGap()) 231 ); 232 panel_checkGame.setLayout(gl_panel_checkGame); 233 mainPanel.setLayout(gl_mainPanel); 234 } 235 236 private void showAboutActionListener(ActionEvent e) { 237 // TODO Auto-generated method stub 238 JOptionPane.showMessageDialog(null, "QQ:523980553"); 239 } 240 241 private void buttonClickAction(ActionEvent eve) { 242 // TODO Auto-generated method stub 243 //設置按鈕標題 244 if( bt_GameBegin.getText().equals("開始遊戲") ) { 245 beginGame(); 246 timer = new Timer(); 247 timer.scheduleAtFixedRate(new MyTase(), 0, 900); 248 } 249 else { 250 resetGame(); 251 252 } 253 254 } 255 256 public static void beginGame() { 257 bt_GameBegin.setText("重置遊戲"); 258 //獲取ComBox選項 259 int picId = comboBox_SelectPic.getSelectedIndex(); 260 //加載圖片 261 PicloadDao pic = new PicloadDao(); 262 pic.loadPic(picId, jl_loadImage); 263 //獲取難易度 264 if(rb_simple.isSelected()) { 265 gameChoseDao = new GameDao(); 266 gameChoseDao.initButton(panel_beginGame); 267 gameChoseDao.randomLoadPic(picId); 268 269 }else if(rb_difficulty.isSelected()) { 270 271 } 272 } 273 274 public static void resetGame() { 275 bt_GameBegin.setText("開始遊戲"); 276 rb_simple.setSelected(true); 277 comboBox_SelectPic.setSelectedIndex(0); 278 textField_time.setText(""+0); 279 jl_loadImage.setIcon(null); 280 gameChoseDao.resetButton(); 281 time = 0; 282 timer.cancel(); 283 } 284 285 class MyTase extends TimerTask{ 286 @Override 287 public void run() { 288 // TODO Auto-generated method stub 289 time ++; 290 textField_time.setText("" + time); 291 if(time == 60) { 292 JOptionPane.showMessageDialog(null, "挑戰失敗!!!"); 293 resetGame(); 294 } 295 } 296 } 297 }

代碼被分成了幾部分:一部分是界面的初始化,這些都可以通過Windows builder來完成,但是從按下“開始遊戲”的按鈕後,之後的任務都需要手動寫代碼,例如事件的處理,開始遊戲和重置遊戲的方法書寫,計時器的書寫。都是需要獨立完成,技術比較菜,可能代碼不是很完善,請原諒。只是基礎,不在詳細去說。

2.素材的處理。

  a.統一規範的素材命名。

  b.利用PS將圖片盡心切片,可以完成圖片分成均勻的幾份。

需要註意的是:如果你使用的是Win8或者Win10,你需要使用管理員權限打開PS。

3.設計圖片加載區域的圖片加載處理類。

4.設計按鈕組中的按鈕初始化方法。

5.設計按鈕組中的隨機圖片加載方法。

6.設計重置按鈕方法。

7.設計按鈕監聽器。

8.設計判定勝利的條件的方法。

Java小項目之拼圖遊戲