// StringRoll.java // Draw Rolling String // by nagatani@eken.phys.nagoya-u.ac.jp import java.applet.Applet; import java.awt.*; public class StringRoll implements SGObject { int xWindow, yWindow; Image imageString; int xImage, yImage; int xPosInit, yPosInit; double deltaY; int xTotalShift; int yOffset; double t; int nMode; StringRoll (Image imageString, int heightString, int yOffset, Applet applet) { this.imageString = imageString; this.yOffset = yOffset; this.xWindow = applet.size().width; this.yWindow = applet.size().height; int xImage = 1026, yImage = 417; // 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 (100); // 100 ms Delay Time } catch (InterruptedException e) {} } this.xImage = xImage; this.yImage = yImage; nMode = 2; t = 0.0; deltaY = (double) (yImage - heightString) / xImage; xPosInit = xWindow; yPosInit = yWindow/2 + (int) (xWindow*deltaY/2) - heightString/2; xTotalShift = xImage + xWindow; } public void paint (Graphics g, Applet applet) { if (nMode == 1) { // Displaying Mode int xPos, yPos; xPos = (int) (xPosInit - t*xTotalShift); yPos = (int) (yPosInit - t*xTotalShift * deltaY); g.drawImage (imageString, xPos, yWindow - (yPos + yImage + yOffset), applet); } } public void move () { if (nMode == 0) { // Reset Mode t = 0.0; nMode ++; return; } if (nMode == 1) { // Displaying Mode t += 0.01; if (t >= 1.0) { t = 0.0; nMode ++; } return; } if (nMode == 2) { // Sleeping Mode t += 0.01; if (t >= 0.2) { t = 0.0; nMode = 0; } return; } } }