
正文
小球碰撞java代码 c++小球碰撞
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA编程小球碰撞怎么输出碰撞次数
在小球这个类中弄一个int flag,然后在小球碰撞改变方向的方法中写flag++;
然后在界面上显示这个flag就行了呀...
连碰撞都会,那显示碰撞次数不就是几下的事么..
相关问答
Q1: 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);
}
}
Q2: JAVA用repaint方法在窗格内实现小球的来回碰撞,怎么在窗格边缘改变小球的运动方向呢?
public class DrawBall extends JFrame {
int x, y, width, height;
Color c;
int incX = 10;//X方向增量
int incY = 10;//Y方向增量
public DrawBall() {
super("寂寞高手不寂寞");
setSize(800, 600);
setVisible(true);
x = 0;
y = 0;
width = height = 50;
c = new Color(255, 0, 0);
}
public static void main(String[] args) {
DrawBall a = new DrawBall();
}
public void paint(Graphics g) {
Container pane = getContentPane();
Graphics pg = pane.getGraphics();
pg.setColor(Color.WHITE);
pg.fillRect(0, 0, pane.getWidth(), pane.getHeight());
//从这里开始改变小球的运动方向
if (x+width pane.getWidth() || x 0) {//X边界判断
incX *= -1; //增量方向反转
}
if (y+height pane.getHeight() || y 0) {//Y边界判断
incY *= -1;//增量方向反转
}
x = x + incX;
y = y + incY;
pg.setColor(c);
pg.fillOval(x, y, width, height);
try {
Thread.sleep(100);
} catch (InterruptedException E) {
};
repaint();
}
}
Q3: 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) {;}
}
}
}
Q4: java 小球碰撞 条件是球心距离 导致"黏住"
2个球相撞,2个球心在一定距离内就可以认为相撞。
两球相撞反弹运动 if((x1-x20)(y1-y20)){ if((x1-x2=r)(y1-y2=r)){ dr1=3; dr2=1;} } if((x1-x20)(y1-y20)){ if((x1-x2=r)(y2-y1=r)){ dr1=0; dr2=2;} } if((x2-x10)
关于小球碰撞java代码和c++小球碰撞的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








