import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.applet.Applet; public class Liikumine2 extends Applet implements ActionListener{ double x=150; double y=80; double d=15; double rx=50, ry=80, rlaius=200, rkorgus=180; double v=2.5; double rv=1; static final long serialVersionUID=1; BufferedImage puhver=new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB); Graphics2D puhverg=(Graphics2D)puhver.getGraphics(); Timer t=new Timer(500, this); public Liikumine2(){ t.start(); } public void paint(Graphics g){ super.paint(g); puhverg.setColor(Color.yellow); puhverg.fillRect(0, 0, 300, 300); puhverg.setColor(Color.black); puhverg.drawRect((int)x, (int)y, (int)d, (int)d); puhverg.drawRect((int)rx, (int)ry, (int)rlaius, (int)rkorgus); g.drawImage(puhver, 0, 0, this); } public void update(Graphics g){paint(g);} public void liigu(){ x=x+v; rx=rx+rv; if(x+d>rx+rlaius){v=-Math.abs(v);} if(xgetWidth()){rv=-Math.abs(rv);} if(rx<1){rv=Math.abs(rv);} repaint(); } public void actionPerformed(ActionEvent e){ if(e.getSource()==t){ liigu(); } } public static void main(String[] arg){ Frame f=new Frame("Liikumine"); f.add(new Liikumine2()); f.setSize(300, 300); f.setVisible(true); } }