https://www.dropbox.com/s/meeflfiscmi4whe/Mball.jar?dl=0
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Mball extends Frame implements Runnable {
static int X,Y;
int z;
Ball b[];
Mball()
{
X=600;
Y=600;
z=7;
b=new Ball[z];
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args) {
Mball mb=new Mball();
mb.setSize(X,Y);
mb.setVisible(true);
mb.start();
mb.setBackground(Color.green);
}
public void start()
{
Thread t=new Thread(this);
t.start();
}
public int getWidth()
{
return X;
}
public int getHeight()
{
return Y;
}
public void run()
{
for(int i=0;i<z;i++)
{
b[i]=new Ball();
}
while(true)
{
for(int i=0;i<z;i++)
{
b[i].update(this);
repaint();
try{
Thread.sleep(5);
}
catch(Exception e){}
}
}
}
public void paint(Graphics g)
{
for(int i=0;i<z;i++)
{
b[i].paint(g,i+1);
}
}
}
class Ball
{
int x=300;
int y=25;
int dx=20;
int dy=17;
int r=20;
Random rr;
Graphics gg;
Ball()
{
rr=new Random();
x=rr.nextInt(600-2*r+1+2*r);
y=rr.nextInt(600-2*r+1+2*r);
}
public void update(Mball mb)
{
if(x+dx>mb.getWidth()-r-2)
{
x=mb.getWidth()-r-2;
dx=-dx;
}
else if(x+dx<r+2)
{
x=r+2;
dx=-dx;
}
else if(y+dy>mb.getHeight()-r-2)
{
y=mb.getHeight()-r-2;
dy=-dy;
}
else if(y+dy<r+2)
{
y=r+2;
dy=-dy;
}
else
{
x=x+dx;
y=y+dy;
}
}
public void paint(Graphics g,int i)
{
//g.setColor(random(i));
g.setColor(Color.red);
g.fillOval(x-r, y-r,2*r,2*r);
}
}
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Mball extends Frame implements Runnable {
static int X,Y;
int z;
Ball b[];
Mball()
{
X=600;
Y=600;
z=7;
b=new Ball[z];
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args) {
Mball mb=new Mball();
mb.setSize(X,Y);
mb.setVisible(true);
mb.start();
mb.setBackground(Color.green);
}
public void start()
{
Thread t=new Thread(this);
t.start();
}
public int getWidth()
{
return X;
}
public int getHeight()
{
return Y;
}
public void run()
{
for(int i=0;i<z;i++)
{
b[i]=new Ball();
}
while(true)
{
for(int i=0;i<z;i++)
{
b[i].update(this);
repaint();
try{
Thread.sleep(5);
}
catch(Exception e){}
}
}
}
public void paint(Graphics g)
{
for(int i=0;i<z;i++)
{
b[i].paint(g,i+1);
}
}
}
class Ball
{
int x=300;
int y=25;
int dx=20;
int dy=17;
int r=20;
Random rr;
Graphics gg;
Ball()
{
rr=new Random();
x=rr.nextInt(600-2*r+1+2*r);
y=rr.nextInt(600-2*r+1+2*r);
}
public void update(Mball mb)
{
if(x+dx>mb.getWidth()-r-2)
{
x=mb.getWidth()-r-2;
dx=-dx;
}
else if(x+dx<r+2)
{
x=r+2;
dx=-dx;
}
else if(y+dy>mb.getHeight()-r-2)
{
y=mb.getHeight()-r-2;
dy=-dy;
}
else if(y+dy<r+2)
{
y=r+2;
dy=-dy;
}
else
{
x=x+dx;
y=y+dy;
}
}
public void paint(Graphics g,int i)
{
//g.setColor(random(i));
g.setColor(Color.red);
g.fillOval(x-r, y-r,2*r,2*r);
}
}