import java.applet.Applet; import java.awt.*; public class Train extends Applet { Shape train=null; // For double buffering private Image buffer; private Graphics buffer_gr; public void init() { Shape wheels=new Compound(new Circle(10,50,5), new Circle(35,50,5)); Shape bottom=new Compound(new Compound(wheels, new Line(15,50,30,50)), new Compound(new Line(0,50,5,50), new Line(40,50,45,50))); Shape locobt=new Compound(new Compound(bottom, new Line(0,35,45,35)), new Compound(new Line(0,35,0,50), new Line(45,35,45,50))); Shape loco=new Compound(locobt, new Compound(new Rectangle(5,20,10,15), new Rectangle(25,15,20,20))); Shape car1=new Compound(new Compound(bottom.copy(), new Compound(new Line(0,15,45,15), new Line(-5,45,0,45))), new Compound(new Line(0,15,0,50), new Line(45,15,45,50))); car1.move(50,0); Shape car2=car1.copy(); car2.move(50,0); train=new Compound(loco,new Compound(car1,car2)); train.move(105,-10); } public void paint(Graphics gr) { if (buffer == null) { buffer = createImage(getSize().width, getSize().height); buffer_gr = buffer.getGraphics(); } buffer_gr.setColor(Color.gray); buffer_gr.fillRect(0, 0, getSize().width-1, getSize().height-1); if(train!=null) for (int i = 0; i<30; i=i+1) { buffer_gr.setColor(Color.gray); train.draw(buffer_gr); train.move(-1,0); buffer_gr.setColor(Color.black); train.draw(buffer_gr); gr.drawImage(buffer, 0, 0, this); sovSødt(75); } } private void sovSødt(int n) { try { Thread.sleep(n); } catch (InterruptedException e) {} } }