资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

多小球碰撞java代码 Java两个小球碰撞的代码

java小球碰撞窗体边缘来回反弹的代码

import java.awt.Color;

创新互联主要从事网站制作、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务武川,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

import java.awt.Graphics;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class RunningBallDemo extends JFrame {

public static void main(String args[]) {

new RunningBallDemo();

}

public RunningBallDemo() {

Ball ballPanel = new Ball(5, 5);

getContentPane().add(ballPanel);

setBackground(Color.BLACK);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

setSize(350, 350);

setVisible(true);

Thread thread1 = new Thread(ballPanel);

thread1.start();

}

}

class Ball extends JPanel implements Runnable {

int rgb = 0;

Color color;

int x, y;

int dx = 5, dy = 5;

Ball(int x, int y) {

this.x = x;

this.y = y;

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

setBackground(Color.BLACK);

g.setColor(color);

g.fillOval(x, y, 50, 50);

}

public void run() {

while (true) {

if (x = 0) {

dx = 5;

updateBallColor();

} else if ((x + 50) = getWidth()) {

dx = -5;

updateBallColor();

}

if (y = 0) {

dy = 5;

updateBallColor();

} else if ((y + 50) = getHeight()) {

dy = -5;

updateBallColor();

}

x = x + dx;

y = y + dy;

repaint();

try {

Thread.sleep(25);

} catch (InterruptedException e) {

;

}

}

}

public void updateBallColor() {

rgb = new Random().nextInt();

color = new Color(rgb);

}

}

JAVA做一个作业,制作一个window框里面有三个小球碰撞,碰到边框或者小球变色加反弹,错误代码求指点

没办法了 只能注册一个马甲了

import java.awt.*;

import java.awt.event.*;

import java.awt.Graphics;

public class yes {

public static void main(String args[])

{

Mywindow win=new Mywindow();

Ball qiu1=new Ball(5,5);

Thread thread1=new Thread(qiu1);

Ball qiu2=new Ball(10,5);

Thread thread2=new Thread(qiu2);

Ball qiu3=new Ball(15,5);

Thread thread3=new Thread(qiu3);

thread1.start();

thread2.start();

thread3.start();

}

}

class Mywindow extends Frame {

Mywindow (){

setSize(350,350);

setVisible(true);

setBackground(Color.BLACK);

validate();

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

}

class Ball extends Mywindow implements Runnable

{

int rgb=0;

Color color;

int x,y;

int dx=5,dy=5;

Ball(int x,int y){

this.x=x;

this.y=y;

}

public void doColor(){

rgb=(int)(Math.random()*0xFFFFFF);

color=new Color(rgb);

}

public void paint(Graphics g){

g.setColor(color);

g.fillOval(x,y,50,50);

}

public void run(){

while(true){

if(x=0) {dx=5;doColor();}

else if((x+50)=getWidth()) {dx=-5; doColor();}

if(y=0) {dy=5;doColor();}

else if((y+50)=getHeight()) {dy=-5; doColor();}

x=x+dx;

y=y+dy;

repaint();

try{Thread.sleep(50);}

catch(InterruptedException e) {;}

}

}

}

JAVA如何实现小球的弹性碰撞

我没用java写过代码,所以我只说算法,代码你自己翻译下

按C的语法来:

void xiaoqiu

{

int UB=10,DB=200,LB=10,RB=200; //定义弹球范围的边界

int sh=1; //定义横向步长

int sz=1; //定义纵向步长(两步长之比决定了反弹的角度)

int x=LB,y=UB; //定义坐标

int i=10000; //循环次数(自己选择跳出手段)

while(i0)

{

i--;

x=x+sh;

if(x=RB||x=LB) sh=-sh; //碰壁后步长变反

y=y+sz;

if(y=DB||y=UB) sz=-sz; //碰壁后步长变反

(显示代码)

}

return;

}

总得来说,就是相当于横向和纵向分别处理移动、反弹的问题,碰壁后步长变为相反数

不懂请追问

Eclipse 写 java小程序。 6个小球碰撞反弹。我知道怎么碰壁反弹。我想要在碰撞过程中小球互相碰撞也反弹。

给小球类定义一个方法:碰撞;然后当周围环境的坐标到球心的距离等于小球的半径时,小球的运动路径算法就应该是轴对称的。先判断之前的运动方向,然后根据运动方向确定新的运动方向。这个其实就是线性方程做小球的运动轨迹而已。


当前标题:多小球碰撞java代码 Java两个小球碰撞的代码
文章来源:http://cdkjz.cn/article/hpiceh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220