// StringFreeRun.java // Draw String Image and Free Running // by nagatani@eken.phys.nagoya-u.ac.jp import java.applet.Applet; import java.awt.*; public class StringFreeRun implements SGObject { int xWindow, yWindow; public Image imageString; int xImage, yImage; double xV, yV, xP, yP; boolean bFirst; StringFreeRun (Image imageString, Applet applet) { this.imageString = imageString; this.xWindow = applet.size().width; this.yWindow = applet.size().height; int xImage = 50, yImage = 50; // Default Image Size for (int n = 0 ; n < 10 ; n++) { int x = imageString.getWidth (applet), y = imageString.getHeight (applet); if (x > 0 && y > 0 ) { xImage = x; yImage = y; break; } try { Thread.sleep (10); // 10 ms Delay Time } catch (InterruptedException e) {} } this.xImage = xImage; this.yImage = yImage; setNewParameters (); bFirst = true; } public void paint (Graphics g, Applet applet) { // Sleeping at the first call if (bFirst) return; g.drawImage (imageString, (int) xP, (int) yP, applet); } public void move () { // Sleeping at the first call if (bFirst && Math.random () > 0.05) return; bFirst = false; xP += xV; yP += yV; if (yP < 0 || xP > xWindow || yP > yWindow) setNewParameters (); } private void setNewParameters () { xP = -1.0 * xImage; yP = 1.0 * (yWindow - yImage) * Math.random (); xV = 2.0 + 10.0 * Math.random (); yV = -0.5 + 1.0 * Math.random (); } }