Немного Java - и страница ожила



Листинг 3


import java.applet.*; import java.awt.*;

public class SimpleScroll extends Applet implements Runnable { private Thread m_SimpleScroll = null; private String m_Text = "Scrolling string"; private final String PARAM_Text = "Text"; int m_StringSize; int m_CurrentXPosition; public String getAppletInfo() { return "Name: SimpleScroll\r\n" + "Author: Alexandr Frolov\r\n" + "E-mail: frolov@glas.apc.org\r\n" + "Web: http://www.glasnet.ru/~frolov," + " http://www.dials.ccas.ru/frolov"; }

public String[][] getParameterInfo() { String[][] info = { { PARAM_Text, "String", "Scrolling string" }, }; return info; }

public void init() { String param; param = getParameter(PARAM_Text); if (param != null) m_Text = param; FontMetrics fm = getFontMetrics(getFont()); m_StringSize = fm.stringWidth(m_Text); m_CurrentXPosition = size().width; setBackground(Color.yellow); } public void paint(Graphics g) { g.drawString(m_Text, m_CurrentXPosition, 20); m_CurrentXPosition-; if(m_CurrentXPosition < -m_StringSize) m_CurrentXPosition = size().width; }

public void start() { if (m_SimpleScroll == null) { m_SimpleScroll = new Thread(this); m_SimpleScroll.start(); } }

public void stop() { if (m_SimpleScroll != null) { m_SimpleScroll.stop(); m_SimpleScroll = null; } }

public void run() { while (true) { try { repaint(); Thread.sleep(50); } catch (InterruptedException e) { stop(); } } } }




Содержание  Назад  Вперед