GUI編程
05 GUI總結
在總結之前,先給出之前的貪吃蛇小游戲全代碼。
- 游戲的主啟動類:
package com.duo.snake;import javax.swing.*;//游戲的主啟動類
public class StartGame {public static void main(String[] args) {JFrame frame = new JFrame();frame.add(new GamePanel()); //此行代碼的位置會影響后續按下空格時游戲的反應(Tab鍵問題)!!!frame.setVisible(true);frame.setResizable(false);frame.setTitle("貪吃蛇-2023");frame.setBounds(5, 10, 915, 740);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
- GamePanel類:
package com.duo.snake;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;//游戲的面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {//定義蛇的數據結構int length; //小蛇總長int[] snakeX = new int[600]; //蛇的X坐標 25*25int[] snakeY = new int[500]; //蛇的Y坐標 25*25String direction; //蛇頭方向boolean isStart = false; //默認未開始游戲boolean isFail = false; //游戲失敗狀態Timer timer = new Timer(100, this); //定時器,100ms執行一次//食物的坐標int foodX;int foodY;Random random = new Random();//積分面板數據結構int score;//構造器public GamePanel() {init();this.setFocusable(true); //獲取焦點事件this.addKeyListener(this); //獲取鍵盤監聽事件timer.start(); //游戲一開始,定時器就啟動}//初始化方法public void init() {length = 3;snakeX[0] = 100; snakeY[0] = 100; //腦袋的坐標snakeX[1] = 75; snakeY[1] = 100; //第一節身體的坐標snakeX[2] = 50; snakeY[2] = 100; //第二節身體的坐標direction = "R"; //蛇頭初始向右//將食物隨機分布于游戲界面foodX = 25 + 25 * random.nextInt(34);foodY = 75 + 25 * random.nextInt(24);score = 0; //定義初始得分}//繪制靜態面板@Overrideprotected void paintComponent(Graphics g) {super.paintComponent(g); //起到清屏的作用Data.header.paintIcon(this, g, 25, 11); //將廣告欄畫入面板g.fillRect(25, 75, 850, 600); //默認的游戲界面(黑色區域)this.setBackground(Color.white);//畫出積分面板g.setColor(Color.white);g.setFont(new Font("宋體", Font.BOLD, 18));g.drawString("長度 " + length, 750, 30);g.drawString("得分 " + score, 750, 55);//畫出食物Data.food.paintIcon(this, g, foodX, foodY);//將小蛇(頭)畫入面板中的游戲區域,且蛇頭部分需要判斷方向switch (direction) {case "R":Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);break;case "L":Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);break;case "U":Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);break;case "D":Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);break;}//畫小蛇的其余身體部分for (int i = 1; i < length; i++) {Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);}//繪制當前游戲狀態if (!isStart) {g.setColor(Color.white);g.setFont(new Font("宋體", Font.BOLD, 40));g.drawString("按下空格開始游戲", 270, 300);}if (isFail) {g.setColor(Color.red);g.setFont(new Font("宋體", Font.BOLD, 40));g.drawString("游戲失敗,按下空格重新開始", 220, 300);}}//鍵盤監聽事件@Overridepublic void keyPressed(KeyEvent e) {int keyCode = e.getKeyCode(); //獲取鍵盤按鍵if (keyCode == KeyEvent.VK_SPACE) {if (isFail) { //重新開始isFail = false;init();} else { //正常開始isStart = !isStart;repaint();}}//鍵盤監聽到按鍵后畫上對應方向的蛇頭if (keyCode == KeyEvent.VK_UP && !direction.equals("D")) {direction = "U";} else if (keyCode == KeyEvent.VK_DOWN && !direction.equals("U")) {direction = "D";} else if (keyCode == KeyEvent.VK_LEFT && !direction.equals("R")) {direction = "L";} else if (keyCode == KeyEvent.VK_RIGHT && !direction.equals("L")) {direction = "R";}}//事件監聽需要定時器來實現,即通過固定的時間進行刷新(例如1s=10次)@Overridepublic void actionPerformed(ActionEvent e) {if (isStart && !isFail) {//小蛇身體右移for (int i = length - 1; i > 0; i--) { //后一節身體移動至前一節 snakeX[1] = snakeX[0]snakeX[i] = snakeX[i - 1];snakeY[i] = snakeY[i - 1];}//判斷蛇頭是否吃到食物if (snakeX[0] == foodX && snakeY[0] == foodY) {length++;foodX = 25 + 25 * random.nextInt(34);foodY = 75 + 25 * random.nextInt(24);score += 10;}//判斷蛇頭走向switch (direction) {case "R":snakeX[0] += 25;//邊界判斷if (snakeX[0] > 850) {snakeX[0] = 25;}break;case "L":snakeX[0] -= 25;//邊界判斷if (snakeX[0] < 25) {snakeX[0] = 850;}break;case "U":snakeY[0] -= 25;//邊界判斷if (snakeY[0] < 75) {snakeY[0] = 650;}break;case "D":snakeY[0] += 25;//邊界判斷if (snakeY[0] > 650) {snakeY[0] = 75;}break;}//游戲失敗判定for (int i = 1; i < length; i++) {if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {isFail = true;break;}}repaint();}timer.start();}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void keyTyped(KeyEvent e) {}
}
游戲的數據中心類:
package com.duo.snake;import javax.swing.*;
import java.net.URL;//數據中心
public class Data {//相對路徑 tx.jpg//絕對路徑 /:相當于當前的項目public static URL headerURL = Data.class.getResource("static/header.png");public static URL upURL = Data.class.getResource("static/up.png");public static URL downURL = Data.class.getResource("static/down.png");public static URL leftURL = Data.class.getResource("static/left.png");public static URL rightURL = Data.class.getResource("static/right.png");public static URL bodyURL = Data.class.getResource("static/body.png");public static URL foodURL = Data.class.getResource("static/food.png");public static ImageIcon header = new ImageIcon(headerURL);public static ImageIcon up = new ImageIcon(upURL);public static ImageIcon down = new ImageIcon(downURL);public static ImageIcon left = new ImageIcon(leftURL);public static ImageIcon right = new ImageIcon(rightURL);public static ImageIcon body = new ImageIcon(bodyURL);public static ImageIcon food = new ImageIcon(foodURL);}
游戲運行結果已于前節展示,可實現小蛇移動、吃到食物長大并且分數增加、失敗判定等操作。下面將對此階段GUI編程作一簡單總結。
GUI編程:
- AWT
- 組件
- 容器
- 面板
- 事件監聽
- 文本框監聽
- 畫圖類paint(g)
- 鼠標/窗口/鍵盤監聽器
- Swing
- 容器
- 面板(可帶有滾動條)
- label
- 普通標簽
- 圖片標簽
- 圖像標簽
- 按鈕
- 普通按鈕
- 圖片按鈕
- 單選框按鈕
- 多選框按鈕
- 列表
- 下拉框
- 列表框
- 文本框
- 普通文本框
- 密碼框
- 文本域
- 貪吃蛇小游戲