import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; public class sng extends Applet { Timer timer; int x = 20; int y =20; int speed = 0; public sng(){ timer = new Timer(); timer.schedule(new sngTask(),0,1*10); } class sngTask extends TimerTask { public void run(){ repaint(); if(y<50){ y+=3; } if(speed>0){ speed-=1; } if(speed<0){ speed+=1; } if(speed>10){ speed=10; } if(speed<-10){ speed=-10; } x = x+speed; } } public boolean keyDown(Event evt,int key) { switch (key) { case Event.LEFT: speed -= 3; break; case Event.RIGHT: speed += 3; break; } return true; } public void paint(Graphics g){ update(g); } public void update(Graphics g){ Graphics2D rd =(Graphics2D)g; Graphics2D rc =(Graphics2D)g; rd.setBackground(Color.green); rd.clearRect(0,0,400,300); rd.fillOval(x,y,30,25); rc.setColor(Color.blue); rd.fillRect(x,y+7,50,20); rd.setColor(Color.red); rd.fillOval(x+35,y+10,20,20); rd.fillOval(x-3,y+10,20,20); } }