- 浏览: 23287 次
- 性别:
- 来自: 浙江
最新评论
最近java学到线程,于是做了一个线程弹球的小游戏,不过还没完善
这里是提纲
1.线程弹球游戏实现
1.实现界面需要使用哪些API类
JFrame
JPanel
JButton
FlowLayout
Graphics2D
Thread
Color
ActionListener
ActionEvent
MouseListener
MouseEvent
BorderLayout
2.实现界面步骤
3.给按钮添加监听器,传递centerPanel
4.定义小球类
5.点击添加按钮,创建小球,让小球运动起来
2.练习
1.绘制立体球
实现的代码是这样
for (int i = 0; i != 100; i++) {
g.setColor(new Color(i, i, i));
g.fillOval(x - r + i / 2, y - r + i / 2, r * 2 - i, r * 2 - i);
}
2.球体碰撞
在BallListener类中定义三个数组队列分别保存小球的坐标,x,y,半径R
,作为ball类构造函数的形参传过去,在ball的peng函数中计算是否碰撞,只要计算两个小球圆心的距离之和小于等于两个小球的半径之和就可以了。每个小球每次都要和其他的所有小球进行比较。
3.如何移除
这个还没想到,之后补上
4.如何暂停
在Ball类的构造函数中传一个布尔型的isrun参数,值为true,如果点暂停,isrun变成false.在run函数中进行判断,若isrun为true则线程继续,否则线程暂停。
下面为具体代码,仍有很多BUG,之后完善了再改
这里是提纲
1.线程弹球游戏实现
1.实现界面需要使用哪些API类
JFrame
JPanel
JButton
FlowLayout
Graphics2D
Thread
Color
ActionListener
ActionEvent
MouseListener
MouseEvent
BorderLayout
2.实现界面步骤
3.给按钮添加监听器,传递centerPanel
4.定义小球类
5.点击添加按钮,创建小球,让小球运动起来
2.练习
1.绘制立体球
实现的代码是这样
for (int i = 0; i != 100; i++) {
g.setColor(new Color(i, i, i));
g.fillOval(x - r + i / 2, y - r + i / 2, r * 2 - i, r * 2 - i);
}
2.球体碰撞
在BallListener类中定义三个数组队列分别保存小球的坐标,x,y,半径R
,作为ball类构造函数的形参传过去,在ball的peng函数中计算是否碰撞,只要计算两个小球圆心的距离之和小于等于两个小球的半径之和就可以了。每个小球每次都要和其他的所有小球进行比较。
3.如何移除
这个还没想到,之后补上
4.如何暂停
在Ball类的构造函数中传一个布尔型的isrun参数,值为true,如果点暂停,isrun变成false.在run函数中进行判断,若isrun为true则线程继续,否则线程暂停。
下面为具体代码,仍有很多BUG,之后完善了再改
package MMM0528; import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.util.ArrayList; import javax.swing.JPanel; public class ball extends Thread { private int x; private int y; private int r; private Color color; private int xspeed; private int yspeed; private JPanel centerpanel; private Graphics2D g; public static boolean isrun; public boolean isremove ; private ArrayList arrayx; private ArrayList arrayy; private ArrayList arrayr; private int i; public ball(int x, int y, int r, int xspeed, int yspeed, JPanel centerpanel, boolean isrun,int i,ArrayList arrayx,ArrayList arrayy,ArrayList arrayr) { this.x = x; this.y = y; this.r = r; this.xspeed = xspeed; this.yspeed = yspeed; this.centerpanel = centerpanel; this.g = (Graphics2D) centerpanel.getGraphics(); this.isrun = isrun; this.i=i; this.arrayx = arrayx; this.arrayy = arrayy; this.arrayr = arrayr; // 设置取表画笔的锯齿状 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } public void run() { while (true) { if (balllistener.getisrun() == true) { this.peng(); this.draw(g); this.move(); this.peng(); try { Thread.sleep(30); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } public void draw(Graphics2D g) { // 擦除上一次绘制的小球 // g.setColor(Color.white); // g.fillOval(x - r - 10, y - r - yspeed + xspeed, r * 2 + 10, r * 2 + 10); // 绘制新的小球 for (int i = 0; i != 100; i++) { g.setColor(new Color(i, i, i)); g.fillOval(x - r + i / 2, y - r + i / 2, r * 2 - i, r * 2 - i); } } public void remove(Graphics2D g) { g.setColor(Color.white); g.fillOval(x - r - 1, y - r - yspeed - 1, r * 2 + 2, r * 2 + 3); g.fillOval(x - r, y - r, r * 2, r * 2); } public void move() { x += xspeed; y += yspeed; arrayx.set(i,x); arrayy.set(i,y); if (y >= (centerpanel.getHeight() - r)) { yspeed = -yspeed; } else if (y <= r) { yspeed = -yspeed; } else if (x <= r) { xspeed = -xspeed; } else if (x >= (centerpanel.getWidth() - r)) { xspeed = -xspeed; } } public void peng() { for (int j = 0; j < arrayx.size(); j++) { if(j==i){ j++; continue; } double dis; int xx, yy; xx = (int) arrayx.get(j); yy = (int) arrayy.get(j); dis = Math.sqrt(Math.abs((xx - x)) * Math.abs((xx - x)) + Math.abs((yy - y)) * Math.abs((yy - y))); int R = (int) arrayr.get(j); R += r; int dist = (int) dis; if (dist == R) { System.out.println(xx); // if (x >= xx && y >= yy) { // xspeed = -xspeed; // yspeed = -yspeed; // }else if (x >= xx && y < yy) { // xspeed = -xspeed; // }else if (x < xx && y >= yy) { // yspeed = -yspeed; // }else if (x < xx && y < yy) { xspeed = -xspeed; yspeed = -yspeed; // } } } } // public void collide(){ // // } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getR() { return r; } public void setR(int r) { this.r = r; } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } public int getXspeed() { return xspeed; } public void setXspeed(int xspeed) { this.xspeed = xspeed; } public int getYspeed() { return yspeed; } public void setYspeed(int yspeed) { this.yspeed = yspeed; } public static void setisrun() { isrun = true; } }
package MMM0528; import java.awt.Color; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random; import javax.swing.JPanel; public class balllistener implements ActionListener { static boolean isrun=true; private static int i=0; private JPanel centerPanel; private Graphics2D g; private ArrayList arrayx =new ArrayList(); private ArrayList arrayy =new ArrayList(); private ArrayList arrayr =new ArrayList(); public balllistener(JPanel centerPanel) { this.centerPanel = centerPanel; g = (Graphics2D)centerPanel.getGraphics();//强制转型 // g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // RenderingHints.VALUE_ANTIALIAS_ON); } public void actionPerformed(ActionEvent e) { //判断点击的是否添加按钮 if(e.getActionCommand().equals("添加")){ // System.out.println("添加"); Random rand = new Random(); int x = rand.nextInt(centerPanel.getWidth()); int y = 50; int r = 40-rand.nextInt(20); int xspeed = rand.nextInt(10)-5; int yspeed = rand.nextInt(10)-5; //创建小球对象 arrayx.add(x); arrayy.add(y); arrayr.add(r); ball balll = new ball(x,y,r,xspeed,yspeed,centerPanel,true,i, arrayx, arrayy, arrayr); i++; balll.start();//启动线程 }else if(e.getActionCommand().equals("移除")){ // System.out.println("移除"); }else if(e.getActionCommand().equals("暂停")){ isrun=false; } } public static boolean getisrun(){ return isrun; } public void setisrun(){ isrun=true; } public static int getI(){ return i; } }
package MMM0528; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class TanqiuUI extends JFrame { public static void main(String[] args) { TanqiuUI tq = new TanqiuUI(); tq.intUI(); } public void intUI() { this.setTitle("弹球游戏"); this.setSize(500, 500); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3); this.setResizable(false); BorderLayout bl = new BorderLayout(); this.setLayout(bl); /* * 北边的按钮 */ JPanel panelnorth = new JPanel(); JButton butadd = new JButton("添加"); JButton butremove = new JButton("移除"); JButton butpause = new JButton("暂停"); panelnorth.add(butadd); panelnorth.add(butremove); panelnorth.add(butpause); this.add(panelnorth, BorderLayout.NORTH); /* * 添加中间的画板 */ JPanel panelcent = new JPanel(); panelcent.setBackground(Color.WHITE); this.add(panelcent, BorderLayout.CENTER); /* * 创建监听类对象 */ balllistener bl1 = new balllistener(panelcent); /* * 调用事件监听方法 */ butadd.addActionListener(bl1); butremove.addActionListener(bl1); butpause.addActionListener(bl1); this.setVisible(true); } }
发表评论
-
配置免安装版Mysql
2016-03-08 23:46 380今天在下载MySQL的时候遇到一些麻烦,可能也是挺普遍的现象, ... -
swap算法
2015-12-13 16:30 960在这里介绍几种交换算法 一、 最常用的一种 int x=1 ... -
c++链表实现2
2015-11-25 20:21 1134template <class Elem> cl ... -
acm实用小技巧
2015-11-23 21:46 2141在acm题目中经常会有十分麻烦的问题,有时候这些问题可以用一些 ... -
c++ 用数组实现栈类
2015-06-15 12:27 1378#include<iostream> #incl ... -
c++ 用类模版实现链表(c++语言程序设计第四版示例代码)
2015-06-12 22:28 873#include<iostream> #incl ... -
c++ 实现五种基础的排序算法
2015-06-11 22:29 1224#include<iostream> using ... -
java 实现自定义链表
2015-06-06 21:33 15081.链表结构 链表是链式的结构 2.链表的组成 ... -
小白谈谈云计算--基于Google三大论文
2015-06-06 21:18 2620之前在没有接触到云计算之前,只是对云计算有一点点模糊的 ... -
c++ 用类模版实现数组类
2015-06-05 01:18 3861最近c++学到数组类,写了代码将他实现,基本具有vector类 ... -
c++运算符重载
2015-05-30 01:06 315一、加+,减-,乘*,除/ 的运算符重载 Rational ... -
java 链表
2015-05-25 20:52 01.链式结构是什么样的? 2.链表的组成 链表是由 ... -
算法竞赛入门经典(第二版)第2章习题
2015-05-25 20:43 52242.4.1 输出技巧 #include <stdio.h ... -
java异常处理机制
2015-05-21 21:29 428java异常关键字有以下几个,分别为 try catch fi ... -
java 关键字
2015-05-14 22:49 540关键字是事先定义的,有特别意义的标识符,有时又叫保留字。对于 ...
相关推荐
JAVA写得小弹球,多线程。随机出现,随机选择颜色,遇到墙壁反弹。
Java做的弹球图形界面小游戏使用awt.*可以创建多个小球.
java
java 弹球游戏 多线程 注意这是eclipe 下用了swingdesigner
每个小球一个独立的线程,按add按钮,增加一个小球
用户能通过GUI组件指定生成小球的...鼠标在界面中显示为方块状,玩家需来回移动鼠标以避开运动的小球及屏幕四周,如果鼠标碰到任一小球或者窗口四周,则游戏结束。 程序需提供计时功能,并最终显示玩家能坚持多少秒。
游戏分为三个等级——简单,中等,困难。 其中,简单对应一个球,速度设置为30,即对应线程暂停30/1000秒; 中等对应两个球,速度设置为40,即对应线程暂停40/...若玩家胜利,游戏结束后设有小窗口,收录玩家心得。
设计一个Java弹球小游戏的思路如下: 创建游戏窗口:使用Java图形库(如Swing或JavaFX)创建一个窗口,作为游戏的可视化界面。 绘制游戏界面:在游戏窗口中绘制游戏所需的各个元素,包括弹球、挡板、得分等。 ...
图片浏览器,桌面弹球,俄罗斯方块,仿windows画图,单机连连看,简单java ide工具,图书进销存系统,事物跟踪系统,多线程下载工具,邮件客户端,mysql管理器,自己开发的ioc容器,仿qq游戏大厅。
英文打字小游戏,TypingGame.java; applet间通信; 汉诺塔游戏, 16 j2se5.0 新特性 自动装箱和拆箱; 新的for循环; 枚举类型; 静态导入; 格式化输出; 使用ProcessBuilder执行本地命令; 泛型编程; 监控和管理虚拟机;新...
ball: 第4章 桌面弹球 tetris: 第5章 俄罗斯方块 image: 第6章 仿Windows画图 linkgame: 第7章 单机连连看 editor: 第8章 简单Java IDE工具 book: 第9章 图书进存销系统 transaction: 第10章 ...
自己编写的Java游戏, 小球在电脑上运行还可以,安在手机上明显不行。 程序各个线程处理不好, 那位大哥有兴趣修改, 大家一起交流。