001package horstmann.ch08_applet;
002import java.applet.Applet;
003import java.awt.Font;
004import java.awt.Graphics;
005import java.awt.Graphics2D;
006import java.awt.font.FontRenderContext;
007import java.awt.geom.Rectangle2D;
008
009import javax.swing.Timer;
010
011@SuppressWarnings("serial")
012public class BannerApplet extends Applet
013{
014        public void init()
015        {
016                message = getParameter("message");
017                String fontname = getParameter("fontname");
018                int fontsize = Integer.parseInt(getParameter("fontsize"));
019                delay = Integer.parseInt(getParameter("delay"));
020                font = new Font(fontname, Font.PLAIN, fontsize);
021                Graphics2D g2 = (Graphics2D) getGraphics();
022                FontRenderContext context = g2.getFontRenderContext();
023                bounds = font.getStringBounds(message, context);
024
025                timer = new Timer(delay, event -> {
026                        start--;
027                        if (start + bounds.getWidth() < 0)
028                                start = getWidth();
029                        repaint();
030                });
031        }
032
033        public void start()
034        {
035                timer.start();
036        }
037
038        public void stop()
039        {
040                timer.stop();
041        }
042
043        public void paint(Graphics g)
044        {
045                g.setFont(font);
046                g.drawString(message, start, (int) -bounds.getY());
047        }
048
049        private Timer timer;
050        private int start;
051        private int delay;
052        private String message;
053        private Font font;
054        private Rectangle2D bounds;
055}