[Java] 弹球游戏 →→→→→进入此内容的聊天室

来自 , 2020-09-20, 写在 Java, 查看 131 次.
URL http://www.code666.cn/view/87ec2f45
  1. package org.crazyit.ball;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.Timer;
  6. import java.awt.Dimension;
  7. import java.awt.Image;
  8. import java.awt.Graphics;
  9. import java.awt.Color;
  10. import java.awt.event.KeyAdapter;
  11. import java.awt.event.KeyListener;
  12. import java.awt.event.KeyEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.ActionEvent;
  15. import java.io.IOException;
  16.  
  17. /*
  18.  * 游戏界面
  19.  */
  20. public class BallFrame extends JFrame {
  21.         // 定义JPanel的宽度
  22.         private final int BALLPANEL_WIDTH = 307;
  23.         // 定义JPanel的高度
  24.         private final int BALLPANEL_HEIGHT = 400;
  25.         // 定义画板
  26.         private BallPanel ballPanel = null;
  27.         // 定义档板
  28.         // private Image stick = null;
  29.         // 设置档板x坐标
  30.         private int stickX = -1;
  31.         // 创建一个BallService实例
  32.         private BallService service = null;
  33.         // 定义一个timer
  34.         Timer timer = null;
  35.  
  36.         /**
  37.          * 默认构造器
  38.          */
  39.         public BallFrame() throws IOException {
  40.                 super();
  41.                 // 初始化
  42.                 initialize();
  43.         }
  44.  
  45.         /*
  46.          * 初始化界面
  47.         */
  48.         public void initialize() throws IOException {
  49.                 // 设置窗口的标题
  50.                 this.setTitle("弹球");
  51.                 // 设置为不可改变大小
  52.                 this.setResizable(false);
  53.                 // 设置背景为黑色
  54.                 this.setBackground(Color.BLACK);
  55.                 // 获取画板
  56.                 ballPanel = getBallPanel();
  57.                 // 创建BallService实例
  58.                 service = new BallService(this, BALLPANEL_WIDTH, BALLPANEL_HEIGHT);
  59.  
  60.                 // 定义每0.1秒执行一次监听器
  61.                 ActionListener task = new ActionListener() {
  62.                         public void actionPerformed(ActionEvent e) {
  63.                                 // 开始改变位置
  64.                                 service.run();
  65.                                 // 刷新画板
  66.                                 ballPanel.repaint();
  67.                         }
  68.                 };
  69.                 // 如果timer不为空
  70.                 if (timer != null) {
  71.                         // 重新开始timer
  72.                         timer.restart();
  73.                 } else {
  74.                         // 新建一个timer
  75.                         timer = new Timer(100, task);
  76.                         // 开始timer
  77.                         timer.start();
  78.                 }
  79.  
  80.                 this.add(ballPanel);
  81.                 // 增加事件监听器
  82.                 KeyListener[] klarr = this.getKeyListeners();
  83.                 if (klarr.length == 0) {
  84.                         // 定义键盘监听适配器
  85.                         KeyListener keyAdapter = new KeyAdapter() {
  86.                                 public void keyPressed(KeyEvent ke) {
  87.                                         // 改变档板的坐标
  88.                                         service.setStickPos(ke);
  89.                                 }
  90.                         };
  91.                         this.addKeyListener(keyAdapter);
  92.                 }
  93.         }
  94.  
  95.         public BallPanel getBallPanel() {
  96.  
  97.                 if (ballPanel == null) {
  98.                         // 新建一个画板
  99.                         ballPanel = new BallPanel();
  100.                         // 设置画板的大小
  101.                         ballPanel.setPreferredSize(new Dimension(BALLPANEL_WIDTH,
  102.                                         BALLPANEL_HEIGHT));
  103.                 }
  104.                 return ballPanel;
  105.         }
  106.  
  107.         // 定义一个JPanel内部类来完成画图功能
  108.         public class BallPanel extends JPanel {
  109.                 /**
  110.                  * 重写void paint( Graphics g )方法
  111.                  *
  112.                  * @param g
  113.                  *            Graphics
  114.                  * @return void
  115.                  */
  116.                 public void paint(Graphics g) {
  117.                         // 画图
  118.                         service.draw(g);
  119.                 }
  120.         }
  121.  
  122. }

回复 "弹球游戏"

这儿你可以回复上面这条便签

captcha