import?java.awt.Color;
創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)武川,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):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);
}
}
沒辦法了 只能注冊一個馬甲了
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寫過代碼,所以我只說算法,代碼你自己翻譯下
按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; //循環(huán)次數(shù)(自己選擇跳出手段)
while(i0)
{
i--;
x=x+sh;
if(x=RB||x=LB) sh=-sh; //碰壁后步長變反
y=y+sz;
if(y=DB||y=UB) sz=-sz; //碰壁后步長變反
(顯示代碼)
}
return;
}
總得來說,就是相當于橫向和縱向分別處理移動、反彈的問題,碰壁后步長變?yōu)橄喾磾?shù)
不懂請追問
給小球類定義一個方法:碰撞;然后當周圍環(huán)境的坐標到球心的距離等于小球的半徑時,小球的運動路徑算法就應(yīng)該是軸對稱的。先判斷之前的運動方向,然后根據(jù)運動方向確定新的運動方向。這個其實就是線性方程做小球的運動軌跡而已。