เอาปายเรย ขอบคุงรุ่นพี่ที่ทำทิ้งไว้
[src]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Movingball_46270354 extends Canvas implements Runnable
{
Random r=new Random();
int x=r.nextInt(500);
int y=r.nextInt(500);
double v=r.nextInt(30)+20;
double rad=r.nextDouble();
double dx=v*Math.cos(rad);
double dy=v*Math.sin(rad);
int w=10;
int h=10;
int a,b,c,d,e,f;
Color color[]={Color.red,Color.green,Color.blue};
Color co;
public void run()
{
Frame f =new Frame();
f.setLayout(new FlowLayout());
f.addWindowListener(new WindowAdapter()
{
public void WindowClosing()
{
System.exit(0);
}
});
this.setSize(500,500);
f.add(this);
f.pack();
f.setVisible(true);
int i=0;
while(true)
{
try
{
x+=dx;
y+=dy;
if ((x>=500)||(x<0))
dx = -dx;
if ((y>=500)||(y<0))
dy = -dy;
//co=color[i%3];
Thread.sleep(50);
i++;
repaint();
}
catch(InterruptedException ex){}
}
}
public void paint(Graphics g)
{
{
//a=r.nextInt(500);
//b =r.nextInt(300);
g.setColor(Color.red);
g.fillOval(x,y,30,30);
}
}
public static void main(String[]args)
{
Thread t=new Thread(new Movingball_46270354());
t.start();
}
}
[/src]