「java小球与小球碰撞」一个小球从高处落下java

博主:adminadmin 2022-11-27 13:05:07 67

今天给各位分享java小球与小球碰撞的知识,其中也会对一个小球从高处落下java进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java里用条件控制小球模拟碰撞,条件2、4出现重复区域而导致矛盾?

你的程序的判断条件有问题,我帮你改完了,并加上了界面关闭程序,你看看吧.

完整的程序如下:

package one;//小球碰撞模拟

import java.awt.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class MyTest4 {

 public static void main(String[] args){

  Frame mb1=new Frame();

  mb1.setSize(500,500);

  MyPanel4 mp4 =new MyPanel4();

  mb1.add(mp4);

  Thread t=new Thread(mp4);

  t.start();

  mb1.setVisible(true);

  mb1.addWindowListener(new WindowAdapter(){

    public void windowClosing(WindowEvent evt){

     System.exit(0);

    }

   });

 }

}

class MyPanel4 extends Panel implements Runnable{

 int x=100;

 int y=0;

 int t1=0;

 int t2=0;

 public void paint(Graphics g){

  super.paint(g);

  g.drawRect(0, 0, 400, 400);

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

 }

 public void run(){

  while(true){

   x+=t1;

   y+=t2;

   if (y==0) {// 条件1                    

    t1=-1;                    

    t2=1;                

   } else if (x==0) {// 条件2                    

    t1=1;                    

    t2=1;                 

   } else if (y==380) {// 条件3                    

    t1=1;                    

    t2=-1;                 

   } else if (x==380) {// 条件4                    

    t1=-1;                    

    t2=-1;              

   }//这句话注释掉以后,会改变很多。

   try{

    Thread.sleep(6);

   }catch(Exception e){}

   repaint();

  }

 }

}

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();

}

}

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小球碰撞窗体边缘来回反弹的代码

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小球与小球碰撞和一个小球从高处落下java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。