java使很多个小球回弹,java 编的小球反弹 但是小球不动
来源:整理 编辑:黑码技术 2024-07-13 02:39:05
本文目录一览
1,java 编的小球反弹 但是小球不动
你不把BALL的类和BounceSimulation 贴出来 让人怎么帮你看?
球不动 估计是因为BounceSimulation 类里线程写的有问题 或者是因为Ball的移动方法有问题。
你只贴出来了无关紧要的类。。。
2,Java小球移动下落碰到边缘向上返回
try Thread.sleep(50); if (y + 20 >= getHeight() || y <= 0) moveY = -moveY; y += moveY; } catch (Exception e) e.printStackTrace(); }给个思路吧,先判断是否碰壁,或者说专门设一个方法使其在没碰壁的情况下一直走,碰壁了就对相应的速度乘以-1,还用原来的方法,貌似叫递归吧。
3,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(i>0)
{
i--;
x=x+sh;
if(x>=RB||x<=LB) sh=-sh; //碰壁后步长变反
y=y+sz;
if(y>=DB||y<=UB) sz=-sz; //碰壁后步长变反
(显示代码)
}
return;
}
总得来说,就是相当于横向和纵向分别处理移动、反弹的问题,碰壁后步长变为相反数
不懂请追问
4,java小球碰撞窗体边缘来回反弹的代码
没办法了 只能注册一个马甲了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 Runnableint 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) else if((x+50)>=getWidth()) if(y<=0) else if((y+50)>=getHeight()) x=x+dx; y=y+dy; repaint(); try catch(InterruptedException e) }}}
5,用JAVA实现多线程编写使得许多小球在界面内循环跳动
y, XSIZE; setTitle("Bounce"); addWindowListener(new WindowAdapter() c.setVisible(false);, new ActionListener() private int y = 0; private int dx = 2; private int dy = 2;}.swing.exit(0); private static final int YSIZE = 10; private int x = 0.getGraphics(); } public void draw() g.dispose(); } public void move() Graphics g = box; } } ).*.width - XSIZE; }}class BounceThreadFrame extends JFrame if (x + XSIZE > } if (y < g;= d; } } catch(InterruptedException e) } private JPanel box; private static final int XSIZE = 10; } private JPanel canvas;}class Ball extends Thread sleep(5); } }); contentPane.add(p, ".exit(0), "Center"); JPanel p = new JPanel(); addButton(p, ".fillOval(x, y; 0) if (y + YSIZE >= d.height) public class BounceThread frame.show(); } public void run() for (int i = 1; i <.height - YSIZE; dy = -dy; } g.fillOval(x, XSIZE, YSIZE); x += dx; y += dy; Dimension d = box.getSize(); if (x < 0) b.start(); } }).add(b); b.addActionListener(a);South"); } public void addButton(Container c; addButton(p; g; dx = -dx; g, "Close" Container contentPane = getContentPane(); canvas = new JPanel(); contentPane.add(canvas.width) ActionListener a) g; dx = -dx.getGraphics().setXORMode(box.getBackground());Start", String title; System, new ActionListener() import java.awt.*;import java.awt.event.*;import javax炯。。。怎么也说的清楚一点吧估计你们老师布置题目时也不可能就这么一句话!!!!
文章TAG:
java 很多 多个 小球 java使很多个小球回弹 编的小球反弹 但是小球不动