弹力弹珠java_弹球游戏 - java代码库 - 云代码
发布日期:2021-06-24 17:21:53 浏览次数:2 分类:技术文章

本文共 2207 字,大约阅读时间需要 7 分钟。

[java]代码库package org.crazyit.ball;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Timer;

import java.awt.Dimension;

import java.awt.Image;

import java.awt.Graphics;

import java.awt.Color;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyListener;

import java.awt.event.KeyEvent;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.io.IOException;

/*

* 游戏界面

*/

public class BallFrame extends JFrame {

// 定义JPanel的宽度

private final int BALLPANEL_WIDTH = 307;

// 定义JPanel的高度

private final int BALLPANEL_HEIGHT = 400;

// 定义画板

private BallPanel ballPanel = null;

// 定义档板

// private Image stick = null;

// 设置档板x坐标

private int stickX = -1;

// 创建一个BallService实例

private BallService service = null;

// 定义一个timer

Timer timer = null;

/**

* 默认构造器

*/

public BallFrame() throws IOException {

super();

// 初始化

initialize();

}

/*

* 初始化界面

*/

public void initialize() throws IOException {

// 设置窗口的标题

this.setTitle("弹球");

// 设置为不可改变大小

this.setResizable(false);

// 设置背景为黑色

this.setBackground(Color.BLACK);

// 获取画板

ballPanel = getBallPanel();

// 创建BallService实例

service = new BallService(this, BALLPANEL_WIDTH, BALLPANEL_HEIGHT);

// 定义每0.1秒执行一次监听器

ActionListener task = new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 开始改变位置

service.run();

// 刷新画板

ballPanel.repaint();

}

};

// 如果timer不为空

if (timer != null) {

// 重新开始timer

timer.restart();

} else {

// 新建一个timer

timer = new Timer(100, task);

// 开始timer

timer.start();

}

this.add(ballPanel);

// 增加事件监听器

KeyListener[] klarr = this.getKeyListeners();

if (klarr.length == 0) {

// 定义键盘监听适配器

KeyListener keyAdapter = new KeyAdapter() {

public void keyPressed(KeyEvent ke) {

// 改变档板的坐标

service.setStickPos(ke);

}

};

this.addKeyListener(keyAdapter);

}

}

public BallPanel getBallPanel() {

if (ballPanel == null) {

// 新建一个画板

ballPanel = new BallPanel();

// 设置画板的大小

ballPanel.setPreferredSize(new Dimension(BALLPANEL_WIDTH,

BALLPANEL_HEIGHT));

}

return ballPanel;

}

// 定义一个JPanel内部类来完成画图功能

public class BallPanel extends JPanel {

/**

* 重写void paint( Graphics g )方法

*

* @param g

* Graphics

* @return void

*/

public void paint(Graphics g) {

// 画图

service.draw(g);

}

}

}

[源代码打包下载]

ball.zip(50积分)[19 次下载]

转载地址:https://blog.csdn.net/weixin_34081553/article/details/112938184 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:qbytearray类型_QByteArray类
下一篇:python powershell库_PowerShell封装了Python for .NET实现从PowerShell调用Python

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月06日 13时39分44秒