JAVA小球冒泡问题 怎么向各个方向冒泡以及向下冒泡小球 求大神修改代码啦
packagecom.tarena.game.day06;importjava.awt.Color;importjava.awt.Graphics;importjava....
package com.tarena.game.day06;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test01 {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setTitle("小球冒泡");
jf.setBounds(0, 0, 430, 640);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BubblePanel bp = new BubblePanel();
jf.add(bp);
jf.setResizable(false);
jf.addMouseListener(bp);
jf.addMouseMotionListener(bp);
Thread t = new Thread(bp);
t.start();
jf.setVisible(true);
}
}
class BubblePanel extends JPanel implements MouseListener, MouseMotionListener ,Runnable{
boolean isFire = false;
int x = 180;
int y = 300;
int r = 25;
LinkedList<Bubble> ballList = new LinkedList<Bubble>();
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillOval(x, y, 2*r, 2*r);
if (isFire) {
g.drawString("冒泡", 10, 30);
}else {
g.drawString("停止冒泡", 10, 30);
}
//循环取出所有的球 并绘制出
for (int i = 0; i < ballList.size(); i++) {
ballList.get(i).drawBall(g);
}
}
@Override//按下拖动
public void mouseDragged(MouseEvent e) {
isFire = true;
//飞机跟着鼠标移动 更改飞机的 x y 值
x = e.getX()-r;
y = e.getY()-r-10;
repaint();
}
//循环产生子弹 加入 集合中 并不绘制 每次间隔300毫秒产生一个球
if (isFire&&count%15==0) {
Bubble ball = new Bubble();
ball.setSpeed(3);
ball.setPath(Bubble.UP);
ball.setR(15);
ball.setColor(Color.RED);
ball.setY(y);
ball.setX(x+r-ball.getR());
ballList.add(ball);
}
count++;
//循环上移所有的子弹 同时超出屏幕顶端的子弹 回收 并释放内存
for (int i = 0; i < ballList.size(); i++) {
Bubble ball = ballList.get(i);
//竖直移动的子弹
if (ball.getPath()==Bubble.UP) {
if (ball.getY()<-2*ball.getR()) {//球已经超出了上方的屏幕顶端 移除该球
ballList.remove(ball);
i--;//因为求已经移除了 球列表里少了一个元素 所以循环次数减1
}else {//球往上移动
ball.move();
}
}else if(ball.getPath()==Bubble.LEFT_UP){
}else if(ball.getPath()==Bubble.RIGHT_UP){
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
} 展开
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test01 {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setTitle("小球冒泡");
jf.setBounds(0, 0, 430, 640);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BubblePanel bp = new BubblePanel();
jf.add(bp);
jf.setResizable(false);
jf.addMouseListener(bp);
jf.addMouseMotionListener(bp);
Thread t = new Thread(bp);
t.start();
jf.setVisible(true);
}
}
class BubblePanel extends JPanel implements MouseListener, MouseMotionListener ,Runnable{
boolean isFire = false;
int x = 180;
int y = 300;
int r = 25;
LinkedList<Bubble> ballList = new LinkedList<Bubble>();
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillOval(x, y, 2*r, 2*r);
if (isFire) {
g.drawString("冒泡", 10, 30);
}else {
g.drawString("停止冒泡", 10, 30);
}
//循环取出所有的球 并绘制出
for (int i = 0; i < ballList.size(); i++) {
ballList.get(i).drawBall(g);
}
}
@Override//按下拖动
public void mouseDragged(MouseEvent e) {
isFire = true;
//飞机跟着鼠标移动 更改飞机的 x y 值
x = e.getX()-r;
y = e.getY()-r-10;
repaint();
}
//循环产生子弹 加入 集合中 并不绘制 每次间隔300毫秒产生一个球
if (isFire&&count%15==0) {
Bubble ball = new Bubble();
ball.setSpeed(3);
ball.setPath(Bubble.UP);
ball.setR(15);
ball.setColor(Color.RED);
ball.setY(y);
ball.setX(x+r-ball.getR());
ballList.add(ball);
}
count++;
//循环上移所有的子弹 同时超出屏幕顶端的子弹 回收 并释放内存
for (int i = 0; i < ballList.size(); i++) {
Bubble ball = ballList.get(i);
//竖直移动的子弹
if (ball.getPath()==Bubble.UP) {
if (ball.getY()<-2*ball.getR()) {//球已经超出了上方的屏幕顶端 移除该球
ballList.remove(ball);
i--;//因为求已经移除了 球列表里少了一个元素 所以循环次数减1
}else {//球往上移动
ball.move();
}
}else if(ball.getPath()==Bubble.LEFT_UP){
}else if(ball.getPath()==Bubble.RIGHT_UP){
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
} 展开
1个回答
展开全部
js版本的, 思想是一样的
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset='utf-8'/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="https://code.createjs.com/createjs-2015.05.21.combined.js"></script>
<title>bubble</title>
<style>
#canvas-wrapper {
height: 600px;
box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div id="canvas-wrapper">
<canvas id="canvas"></canvas>
</div>
</body>
<script>
$(function () {
$('#canvas-wrapper').each(function () {
var width = $(this).css('width');
var height = $(this).css('height');
$(this).find('canvas').attr({'width': width, 'height': height});
});
var stage = new createjs.Stage($('#canvas').get(0));
stage.xp = $('#canvas').get(0).width;
stage.yp = $('#canvas').get(0).height;
var rect = new createjs.Shape();
rect.graphics.beginFill('#fff').drawRect(0, 0, stage.xp, stage.yp).endFill();
stage.addChild(rect);
var container = new createjs.Container();
stage.addChild(container);
stage.update();
var Bubble = function (pos) {
this.bubble = new createjs.Shape();
this.bubble.wrapperObject = this;
container.addChild(this.bubble);
this.obj = {x: pos.x, y: pos.y, radius: 1};
var _this = this;
var tween = new createjs.Tween.get(this.obj).to({
x: pos.x + (0.5 - Math.random()) * 300,
y: pos.y + (0.5 - Math.random()) * 300,
radius: 10 + 10 * Math.random()
}, 1000).call(function (e) {
container.removeChild(_this.bubble);
});
}
Bubble.prototype.update = function () {
this.bubble.graphics.clear();
this.bubble.graphics.setStrokeStyle(1).beginStroke('#888').drawCircle(0, 0, this.obj.radius).endStroke();
this.bubble.set({x: this.obj.x, y: this.obj.y});
}
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener("tick", function () {
container.children.forEach(function (d, i) {
d.wrapperObject.update();
})
stage.update();
});
stage.on('pressmove', function (e) {
new Bubble({x: e.stageX, y: e.stageY});
})
})
</script>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询