java程序小球大战(Java小球的碰撞与销毁问题)

本文目录
- Java小球的碰撞与销毁问题
- Java高手进,代码设计目的是实现小球沿着对角线来回碰撞‘
- 滚动的小球 java源代码
- 运用java多线程,一次注入两个小数,碰撞墙壁一定次数后停止,也就是java小球碰撞
- java小球碰撞窗体边缘来回反弹的代码
- java 如何在文本框内显示两个小球撞来撞去
- Eclipse 写 java小程序 6个小球碰撞反弹我知道怎么碰壁反弹我想要在碰撞过程中小球互相碰撞也反弹
- JAVA做一个作业,制作一个window框里面有三个小球碰撞,碰到边框或者小球变色加反弹,错误代码求指点
Java小球的碰撞与销毁问题
是否碰撞很好解决,就是判断两球的球心距离d与半径r、R的关系,如果d《r+R则一定会碰撞
距离可以利用坐标来计算
创建一个球类,成员变量有:半径r,球心坐标x、y(如果是三维的话再加上z)
成员函数应该有:运动函数(随即产生一个方向,然后坐标进行修改)
注:该球类应该要implements Runnable,创建一个对象后就开启该线程
大概就这些了
Java高手进,代码设计目的是实现小球沿着对角线来回碰撞‘
if(y《=50)改成 if(y《=80)
package com.yuanluesoft.bidding.demo;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class Test extends Frame implements Runnable{
int x=0,y=30,dx=10,dy=10;
public static void main(String args) {
new Test();
}
public Test() {
setSize(360,390);
setVisible(true);
new Thread(this).start();
}
public void run() {
while(true){
x=x+dx; y=y+dy; repaint();
if(x《=0)
dx=10;
else if((x+10)》=getWidth()) dx=-10;
if(y《=30) dy=10;
else if ((y+10)》=getHeight()) dy=-10;
try{Thread.sleep(250);}
catch(InterruptedException e) {
}
}
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillOval(x,y,10,10);
}
}
滚动的小球 java源代码
要制造那种效果只需要大约 30 行 Java 代码:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class RollingBall extends JPanel {
Ellipse2D.Float ball = new Ellipse2D.Float( -100, 100, 50, 50 );
public void paintComponent( Graphics g ) {
super.paintComponent( g );
Graphics2D g2 = ( Graphics2D ) g;
// Draw the ball
g2.fill( ball );
// Draw the rotating ellipse by skewing the Device Space
double angdeg = // One rotation per ball’s travelling over its perimeter
ball.x++ % ( Math.PI * ball.width ) / ( Math.PI * ball.width ) * 360;
g2.rotate( Math.toRadians( angdeg ), ball.getCenterX( ), ball.getCenterY( ) );
g2.scale( .5, 1 );
g2.translate( ball.getCenterX( ), 0 );
g2.setColor( Color.gray );
g2.fill( ball );
}
public void roll( ) throws Exception {
while( true ) {
repaint( );
Thread.sleep( 8 );
}
}
public static void main( String args ) throws Exception {
JFrame f = new JFrame( );
RollingBall rb = new RollingBall( );
f.setSize( 999, 185 );
f.getContentPane( ).add( rb );
f.setVisible( true );
rb.roll( );
}
}
运用java多线程,一次注入两个小数,碰撞墙壁一定次数后停止,也就是java小球碰撞
在小球这个类中弄一个int flag,然后在小球碰撞改变方向的方法中写flag++;
然后在界面上显示这个flag就行了呀...
连碰撞都会,那显示碰撞次数不就是几下的事么..
java小球碰撞窗体边缘来回反弹的代码
import java.awt.Color;
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 如何在文本框内显示两个小球撞来撞去
指教不敢,
用线程, 加上画图,
然后一个圆圈类,
运动轨迹按自己想的计算好,然后写个碰撞时间,
很简单的, 我第一次做就40分钟就出来了
祝你好运! :)
我找到了,呵呵
给你贴上, 你自己好好研究下吧 :)
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
public class Circle extends Frame implements Runnable
{
int dir_x,dir_y;
int x,y,flag,count=0;
static List《Circle》 circles=new ArrayList《Circle》();
public Circle(int x,int y,int flag)
{
this.x=x;
this.y=y;
this.flag=flag;
}
public void draw(Graphics g)
{
Color c=g.getColor();
if(flag==1)
{
g.setColor(Color.orange);
}
else
{
g.setColor(Color.blue);
}
g.drawOval(this.x, this.y, 20, 20);
g.setColor(c);
direction();
move();
}
public void lunchFrame()
{
this.setSize(500,500);
this.setVisible(true);
}
public void paint(Graphics g)
{
for(int i=0;i《circles.size()-1;i++)
{
for(int j=i+1;j《circles.size();j++)
{
circles.get(i).hitCircle(circles.get(j));
}
}
for(int i=0;i《circles.size();i++)
{
circles.get(i).draw(g);
}
}
public void move()
{
if(dir_x==0)x-=5;
if(dir_x==1)x+=5;
if(dir_y==0)y-=5;
if(dir_y==1)y+=5;
}
public void run()
{
while(true)
{
try
{
Thread.sleep(30);
repaint();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void direction()
{
if(x《50)dir_x=1;
if(x》450)dir_x=0;
if(y《60)dir_y=1;
if(y》450)dir_y=0;
}
public void hitCircle(Circle circle)
{
if( (this.x-circle.x)*(this.x-circle.x)+(this.y-circle.y)*(this.y-circle.y)《=400 )
{
count++;
if(this.dir_x==0)
{
this.dir_x=1;
}
else
{
this.dir_x=0;
}
if(this.dir_y==0)
{
this.dir_y=1;
}
else
{
this.dir_y=0;
}
if(circle.dir_y==0)
{
circle.dir_y=1;
}
else
{
circle.dir_y=0;
}
if(circle.dir_x==0)
{
circle.dir_x=1;
}
else
{
circle.dir_x=0;
}
}
}
public static void main(String args)
{
Circle circle=new Circle(200,200,1);
Thread thread=new Thread(circle);
Circle.circles.add(circle);
Circle.circles.add(new Circle(400,200,2));
Circle.circles.add(new Circle(300,100,1));
Circle.circles.add(new Circle(100,300,2));
Circle.circles.add(new Circle(300,150,1));
Circle.circles.add(new Circle(100,350,2));
Circle.circles.add(new Circle(300,200,1));
Circle.circles.add(new Circle(100,250,2));
Circle.circles.add(new Circle(300,330,1));
Circle.circles.add(new Circle(100,380,2));
circle.lunchFrame();
thread.start();
}
}
Eclipse 写 java小程序 6个小球碰撞反弹我知道怎么碰壁反弹我想要在碰撞过程中小球互相碰撞也反弹
给小球类定义一个方法:碰撞;然后当周围环境的坐标到球心的距离等于小球的半径时,小球的运动路径算法就应该是轴对称的。先判断之前的运动方向,然后根据运动方向确定新的运动方向。这个其实就是线性方程做小球的运动轨迹而已。
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) {;}
}
}
}

更多文章:
字符串“\t\t\t”的长度?编写算法,将字符串t插入到字符串s中第i个字符位置
2026年2月1日 00:45
全栈java工程师培训(北大青鸟java培训:Java全栈工程师需要掌握哪些知识)
2025年6月12日 22:30
wrap在服装上是什么意思(英语服装设计专业术语大全,英语服装设计专业术语大全)
2025年10月2日 02:00
pytorch lightning(PyTorch Lightning 中的批量测试及其存在的问题)
2025年12月1日 20:15
hoisting什么意思(musical fountain是什么意思)
2026年3月7日 04:00
java与mysql建立连接(java怎么连mysql数据库)
2025年7月22日 15:15
rank函数递减次序排名(如何利用rank函数,降序“成绩排名”)
2026年3月4日 05:45
满二叉树和完全二叉树到底有什么区别,他们定义不是差不多?请解释“满二叉树一定是完全二叉树,而完全二叉树不一定是满二叉树”谢谢了!
2025年8月4日 22:00
java编写网络代码(如何用70行Java代码实现深度神经网络算法)
2026年7月1日 19:15
进程管理有哪些特性(操作系统一般具有哪些特征,这些特征在进程管理中是如何体现的)
2026年3月24日 23:30
db2数据库连接数突增(db2数据库导入时自动增长的ID列如何填写)
2026年4月27日 00:00
python的安装选项怎么勾选(python怎么安装 python安装教程)
2025年10月9日 10:15











