public class CDRotation extends Applet
Листинг 1
. Файл CDRotation.java
import java.applet.*; import java.awt.*;
public class CDRotation extends Applet implements Runnable { Thread m_CDRotation = null; private Graphics m_Graphics; private Image m_Images[]; private int m_nCurrImage; private int m_nImgWidth = 0; private int m_nImgHeight = 0; private boolean m_fAllLoaded = false; private final int NUM_IMAGES = 11;
public String getAppletInfo() { return "Name: CDRotation"; }
private void displayImage(Graphics g) { if (!m_fAllLoaded) return;
g.drawImage(m_Images[m_nCurrImage], (size().width - m_nImgWidth) / 2, (size().height - m_nImgHeight) / 2, null);
g.drawString( (new Integer(m_nCurrImage)).toString(), (size().width - m_nImgWidth) /2, ((size().height - m_nImgHeight)/2)+ 10);
}
public void paint(Graphics g) { Dimension dimAppWndDimension = size();
g.setColor(Color.white);
g.fillRect(0, 0, dimAppWndDimension.width - 1, dimAppWndDimension.height - 1);
g.setColor(Color.black);
g.drawRect(0, 0, dimAppWndDimension.width - 1, dimAppWndDimension.height - 1);
if (m_fAllLoaded) { displayImage(g);
}
else g.drawString("Please, wait...", 10, dimAppWndDimension.height / 2);
}
public void start() { if (m_CDRotation == null) { m_CDRotation = new Thread(this);
m_CDRotation.start();
} }
public void stop() { if (m_CDRotation != null) { m_CDRotation.stop();
m_CDRotation = null; } }
public void run() { m_nCurrImage = 0;
if (!m_fAllLoaded) { repaint();
m_Graphics = getGraphics();
m_Images = new Image[NUM_IMAGES];
MediaTracker tracker = new MediaTracker(this);
String strImage;
for (int i = 0; i < NUM_IMAGES; i++) { strImage = "images/cdimg0" + ((i < 10) ? "0" : "") + i + ".gif";
m_Images[i] = getImage( getDocumentBase(), strImage);
tracker.addImage(m_Images[i], 0);
}
try { tracker.waitForAll();
m_fAllLoaded = !tracker.isErrorAny();
} catch (InterruptedException e) { }
if (!m_fAllLoaded) { stop();
m_Graphics.drawString( "Load error", 10, size().height / 2);
return; }
m_nImgWidth = m_Images[0].getWidth(this);
m_nImgHeight = m_Images[0].getHeight(this);
}
repaint();
while (true) { try { displayImage(m_Graphics);
m_nCurrImage++;
if(m_nCurrImage == NUM_IMAGES) m_nCurrImage = 0;
Thread.sleep(30);
}
catch (InterruptedException e) { stop();
} } } }
Листинг 2 содержит исходный текст документа HTML, созданного для аплета CDRotation.