import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ThrowAway extends Applet implements Runnable,MouseMotionListener{
int x,y,dx,dy,p,w,h;
public void init(){
w=getWidth();
h=getHeight();
new Thread(this).start();
addMouseMotionListener(this);
}
public void render(Graphics g){
x+=dx;
y+=dy;
if(x<10||x>w-10){
if(y>p&&y<p+30)
dx=-dx;
if(x<=0||x>w){
dx=dy=1;
x=y=w/2;
}
}
if(y<0||y>h) dy=-dy;
g.setColor(Color.black);
g.fillRect(0, 0, w, h);
g.setColor(Color.white);
g.fillRect(0, p, 10, 30);
g.fillRect(w-10, p, 120, 30);
g.fillRect(x-1, y-1, 3, 3);
}
public void run() {
Image buf=createImage(w,h);
Graphics g=buf.getGraphics();
while(true){
render(g);
getGraphics().drawImage(buf,0,0,this);
try{Thread.sleep(5);}catch(Exception ex){};
}
}
public void mouseMoved(MouseEvent e) {
p=e.getY()-15;
}
public void mouseDragged(MouseEvent e){}
}
Name:
Anonymous2009-03-20 17:00
Here it is the white space free version(can't do this in python): import java.awt.*;import java.awt.event.*;import java.applet.*;public class a extends Applet implements Runnable,MouseMotionListener{int x,y,t,u,p,w,h;public void init(){w=getWidth();h=getHeight();new Thread(this).start();addMouseMotionListener(this);}public void render(Graphics g){x+=t;y+=u;if(x<10||x>w-10){if(y>p&&y<p+30)t=-t;if(x<=0||x>w){t=u=1;x=y=w/2;}}if(y<0||y>h)u=-u;g.setColor(Color.black);g.fillRect(0,0,w,h);g.setColor(Color.white);g.fillRect(0, p, 10, 30);g.fillRect(w-10, p, 120, 30);g.fillRect(x,y,3,3);}public void run(){Image buf=createImage(w,h);Graphics g=buf.getGraphics();while(true){render(g);getGraphics().drawImage(buf,0,0,this);try{Thread.sleep(5);}catch(Exception x){};}}public void mouseMoved(MouseEvent e){p=e.getY()-15;}public void mouseDragged(MouseEvent e){}}