home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / JUMPTEXT / JUMPTEXT.EXE / Jumptext.java < prev   
Encoding:
Java Source  |  1996-06-06  |  3.6 KB  |  145 lines

  1. /*
  2.  
  3.  
  4.     Jump text
  5.  
  6.  
  7. */
  8.  
  9.  
  10.  
  11.  
  12.  
  13. import java.awt.*;
  14.  
  15.  
  16. import java.util.*;
  17.  
  18.  
  19. import java.lang.*;
  20.  
  21.  
  22.  
  23.  
  24.  
  25. /*
  26.  
  27.  
  28. */
  29.  
  30.  
  31. public class Jumptext extends java.applet.Applet implements Runnable {
  32.  
  33.  
  34.     Thread    blinker;
  35.  
  36.  
  37.     String    message1, message2;
  38.  
  39.  
  40.     Font     font1, font2;
  41.  
  42.  
  43.     int     speed,
  44.  
  45.  
  46.         lastX1, lastY1, directX1, directY1,
  47.  
  48.  
  49.         lastX2, lastY2, directX2, directY2;
  50.  
  51.  
  52.  
  53.  
  54.  
  55.     public void init() {
  56.  
  57.  
  58.         String att;
  59.  
  60.  
  61.         Dimension d = size();
  62.  
  63.  
  64.  
  65.  
  66.  
  67.         att = getParameter("speed");
  68.  
  69.  
  70.         speed = (att == null) ? 50 : Integer.valueOf(att).intValue();
  71.  
  72.  
  73.  
  74.  
  75.  
  76.         font1 = new java.awt.Font("TimesRoman", Font.ITALIC, 24);
  77.  
  78.  
  79.         att = getParameter("message1");
  80.  
  81.  
  82.         message1 = (att == null) ? "CATNET Internet Service" : att;
  83.  
  84.  
  85.         lastX1 = (int)(Math.random() * (d.width - 1));
  86.  
  87.  
  88.         lastY1 = (int)((d.height - font1.getSize() - 1) * Math.random());
  89.  
  90.  
  91.         directX1 = 3;
  92.  
  93.  
  94.         directY1 = 3;
  95.  
  96.  
  97.  
  98.  
  99.  
  100.         font2 = new java.awt.Font("TimesRoman", Font.PLAIN, 20);
  101.  
  102.  
  103.         att = getParameter("message2");
  104.  
  105.  
  106.         message2 = (att == null) ? "System Intelligent" : att;
  107.  
  108.  
  109.         lastX2 = (int)(Math.random() * (d.width - 1));
  110.  
  111.  
  112.         lastY2 = (int)((d.height - font2.getSize() - 1) * Math.random());
  113.  
  114.  
  115.         directX2 = -3;
  116.  
  117.  
  118.         directY2 = -3;
  119.  
  120.  
  121.     }
  122.  
  123.  
  124.     
  125.  
  126.  
  127.     public void start() {
  128.  
  129.  
  130.         /* Set background color */
  131.  
  132.  
  133.         this.setBackground(Color.black);
  134.  
  135.  
  136.  
  137.  
  138.  
  139.         /* Start thread */
  140.  
  141.  
  142.         if (blinker == null) {
  143.  
  144.  
  145.             blinker = new Thread(this, "Blink");
  146.  
  147.  
  148.             blinker.start();
  149.  
  150.  
  151.         }
  152.  
  153.  
  154.     }
  155.  
  156.  
  157.     public void paint(Graphics g) {
  158.  
  159.  
  160.         int    x,
  161.  
  162.  
  163.             y,
  164.  
  165.  
  166.             space;
  167.  
  168.  
  169.         Dimension d = size();
  170.  
  171.  
  172.         StringTokenizer t;
  173.  
  174.  
  175.         FontMetrics fm;
  176.  
  177.  
  178.  
  179.  
  180.  
  181.         g.setColor(Color.black);
  182.  
  183.  
  184.         g.setFont(font1);
  185.  
  186.  
  187.         fm = g.getFontMetrics();
  188.  
  189.  
  190.         space = fm.stringWidth(" ");
  191.  
  192.  
  193.         x = lastX1;
  194.  
  195.  
  196.         y = lastY1;
  197.  
  198.  
  199.         for (t = new StringTokenizer(message1) ; t.hasMoreTokens() ; ) {
  200.  
  201.  
  202.             String word = t.nextToken();
  203.  
  204.  
  205.             int w = fm.stringWidth(word) + space;
  206.  
  207.  
  208.             if (x > d.width) {
  209.  
  210.  
  211.                 x = x - d.width;
  212.  
  213.  
  214.             }
  215.  
  216.  
  217.             g.setColor(new java.awt.Color((int)(Math.random() * 256),
  218.  
  219.  
  220.                 (int)(Math.random() * 256), (int)(Math.random() * 256)));
  221.  
  222.  
  223.             g.drawString(word, x, y);
  224.  
  225.  
  226.             x += w;
  227.  
  228.  
  229.         }
  230.  
  231.  
  232.  
  233.  
  234.  
  235.         if (Math.random() > 0.99) {
  236.  
  237.  
  238.             directX1 = -directX1;
  239.  
  240.  
  241.         }
  242.  
  243.  
  244.         lastX1 += directX1;
  245.  
  246.  
  247.         if (lastX1 >= d.width) {
  248.  
  249.  
  250.             lastX1 = 0;
  251.  
  252.  
  253.         } else if (lastX1 < 0) {
  254.  
  255.  
  256.             lastX1 = d.width - 1;
  257.  
  258.  
  259.         }
  260.  
  261.  
  262.         lastY1 += directY1;
  263.  
  264.  
  265.         if (lastY1 >= d.height) {
  266.  
  267.  
  268.             directY1 = -3;
  269.  
  270.  
  271.         } else if (lastY1 < font1.getSize()) {
  272.  
  273.  
  274.             directY1 = 3;
  275.  
  276.  
  277.         }
  278.  
  279.  
  280.  
  281.  
  282.  
  283.         g.setColor(Color.black);
  284.  
  285.  
  286.         g.setFont(font2);
  287.  
  288.  
  289.         fm = g.getFontMetrics();
  290.  
  291.  
  292.         space = fm.stringWidth(" ");
  293.  
  294.  
  295.         x = lastX2;
  296.  
  297.  
  298.         y = lastY2;
  299.  
  300.  
  301.         for (t = new StringTokenizer(message2) ; t.hasMoreTokens() ; ) {
  302.  
  303.  
  304.             String word = t.nextToken();
  305.  
  306.  
  307.             int w = fm.stringWidth(word) + space;
  308.  
  309.  
  310.             if (x > d.width) {
  311.  
  312.  
  313.                 x = x - d.width;
  314.  
  315.  
  316.             }
  317.  
  318.  
  319.             g.setColor(new java.awt.Color((int)(Math.random() * 256),
  320.  
  321.  
  322.                 (int)(Math.random() * 256), (int)(Math.random() * 256)));
  323.  
  324.  
  325.             g.drawString(word, x, y);
  326.  
  327.  
  328.             x += w;
  329.  
  330.  
  331.         }
  332.  
  333.  
  334.  
  335.  
  336.  
  337.         if (Math.random() > 0.99) {
  338.  
  339.  
  340.             directX2 = -directX2;
  341.  
  342.  
  343.         }
  344.  
  345.  
  346.         lastX2 += directX2;
  347.  
  348.  
  349.         if (lastX2 >= d.width) {
  350.  
  351.  
  352.             lastX2 = 0;
  353.  
  354.  
  355.         } else if (lastX2 < 0) {
  356.  
  357.  
  358.             lastX2 = d.width - 1;
  359.  
  360.  
  361.         }
  362.  
  363.  
  364.         lastY2 += directY2;
  365.  
  366.  
  367.         if (lastY2 >= d.height) {
  368.  
  369.  
  370.             directY2 = -3;
  371.  
  372.  
  373.         } else if (lastY2 < font1.getSize()) {
  374.  
  375.  
  376.             directY2 = 3;
  377.  
  378.  
  379.         }
  380.  
  381.  
  382.     }
  383.  
  384.  
  385.  
  386.  
  387.  
  388.     public void stop() {
  389.  
  390.  
  391.         blinker = null;
  392.  
  393.  
  394.         blinker.stop();
  395.  
  396.  
  397.     }
  398.  
  399.  
  400.  
  401.  
  402.  
  403.     public void run() {
  404.  
  405.  
  406.         while (blinker != null) {
  407.  
  408.  
  409.             repaint();
  410.  
  411.  
  412.             try {
  413.  
  414.  
  415.                 blinker.sleep(speed);
  416.  
  417.  
  418.             } catch (InterruptedException e)
  419.  
  420.  
  421.             {}
  422.  
  423.  
  424.         }
  425.  
  426.  
  427.     }
  428.  
  429.  
  430. }
  431.  
  432.  
  433.