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 / TICKER2 / TICKER2.EXE / ticker.java < prev    next >
Encoding:
Java Source  |  1996-04-05  |  6.4 KB  |  243 lines

  1. import java.awt.Graphics;
  2.  
  3. import java.applet.Applet;
  4.  
  5. import java.awt.Font;
  6.  
  7. import java.awt.Color;
  8.  
  9. import java.awt.Event;
  10.  
  11. import java.awt.FontMetrics;
  12.  
  13. import java.awt.Image;
  14.  
  15.  
  16.  
  17. public class ticker extends Applet implements Runnable{
  18.  
  19.  
  20.  
  21. String input_text[] = new String[4];
  22.  
  23. char mesg[][] = new char[4][] ;
  24.  
  25. int xpos=400;
  26.  
  27. int ypos=50;
  28.  
  29. int realLength;
  30.  
  31. int realHeight;
  32.  
  33. int width=400;
  34.  
  35. int height=50;
  36.  
  37. boolean suspended = false;
  38.  
  39. Color color = new Color(0,255,255);
  40.  
  41. Thread killme=null;
  42.  
  43. Image im;
  44.  
  45. Graphics osGraphics;
  46.  
  47. Color foregroundCol;
  48.  
  49. Color DefaultforegroundCol = new Color(0, 0, 0);
  50.  
  51. Color backgroundCol; 
  52.  
  53. Color DefaultbackgroundCol = new Color(192, 192, 192); 
  54.  
  55. Color TextColor[] = new Color[4] ;
  56.  
  57. Font showfont; 
  58.  
  59. int speed = 5 ;
  60.  
  61. int s_time[] = new int[10] ;
  62.  
  63. int seq = 0 ;
  64.  
  65. FontMetrics fmetrics;
  66.  
  67. boolean nervous = false ;
  68.  
  69.  
  70.  
  71.  
  72.  
  73.   private int getColorFromParam(String parmName, Color defaultCol){
  74.  
  75.     Integer IntColor;
  76.  
  77.     String parmValue = this.getParameter(parmName);
  78.  
  79.     if (parmValue != null)
  80.  
  81.     {
  82.  
  83.          Integer intColor = Integer.valueOf(parmValue, 16);
  84.  
  85.         return(intColor.intValue());
  86.  
  87.     }
  88.  
  89.     return (defaultCol.getRGB());
  90.  
  91.   }
  92.  
  93.  
  94.  
  95.   public void init(){
  96.  
  97.     String FontFamily = new String("TimesRoman");
  98.  
  99.     int FontSize = 12;
  100.  
  101.     int FontWeight = Font.PLAIN;
  102.  
  103.     Integer tmp ;
  104.  
  105.     backgroundCol = new Color(this.getColorFromParam("BGCol", DefaultbackgroundCol));
  106.  
  107.     foregroundCol = new Color(this.getColorFromParam("FGCol", DefaultforegroundCol));
  108.  
  109.     this.setBackground(backgroundCol);
  110.  
  111.  
  112.  
  113.     TextColor[0] = new Color(this.getColorFromParam("C1",(new Color(0,0,255))));
  114.  
  115.     TextColor[1] = new Color(this.getColorFromParam("C2",(new Color(0,255,0))));
  116.  
  117.     TextColor[2] = new Color(this.getColorFromParam("C3",(new Color(255,255,0))));
  118.  
  119.     TextColor[3] = new Color(this.getColorFromParam("C4",(new Color(255,0,255))));
  120.  
  121.  
  122.  
  123.  
  124.  
  125.     s_time[0] = 200 ;
  126.  
  127.     s_time[1] = 100 ;
  128.  
  129.     s_time[2] = 75 ;
  130.  
  131.     s_time[3] = 50 ;
  132.  
  133.     s_time[4] = 32 ;
  134.  
  135.     s_time[5] = 16 ;
  136.  
  137.     s_time[6] = 8 ;
  138.  
  139.     s_time[7] = 4;
  140.  
  141.     s_time[8] = 2 ;
  142.  
  143.     s_time[9] = 1 ;
  144.  
  145.  
  146.  
  147.     input_text[0]=getParameter("text1");
  148.  
  149.     input_text[1]=getParameter("text2");
  150.  
  151.     input_text[2]=getParameter("text3");
  152.  
  153.     input_text[3]=getParameter("text4");
  154.  
  155.  
  156.  
  157.     mesg[0] = new char[input_text[0].length()] ;
  158.  
  159.     mesg[1] = new char[input_text[1].length()] ;
  160.  
  161.     mesg[2] = new char[input_text[2].length()] ;
  162.  
  163.     mesg[3] = new char[input_text[3].length()] ;
  164.  
  165.     
  166.  
  167.     input_text[0].getChars(0,input_text[0].length(),mesg[0],0);
  168.  
  169.     input_text[1].getChars(0,input_text[1].length(),mesg[1],0);
  170.  
  171.     input_text[2].getChars(0,input_text[2].length(),mesg[2],0);
  172.  
  173.     input_text[3].getChars(0,input_text[3].length(),mesg[3],0);
  174.  
  175.  
  176.  
  177.     if (this.getParameter("Speed") != null){ 
  178.  
  179.     tmp = new Integer(this.getParameter("Speed"));
  180.  
  181.     speed = tmp.intValue();
  182.  
  183.     if((speed >=0) && (speed <10)){
  184.  
  185.     }else{
  186.  
  187.         speed = 5 ;
  188.  
  189.     }    
  190.  
  191.     }
  192.  
  193.  
  194.  
  195.     if (this.getParameter("Nervous") != null){ 
  196.  
  197.     tmp = new Integer(this.getParameter("Nervous"));
  198.  
  199.     nervous =  (tmp.intValue() == 1);    
  200.  
  201.     }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.     if (this.getParameter("FontFamily") != null)
  210.  
  211.     {
  212.  
  213.         FontFamily = this.getParameter("FontFamily");
  214.  
  215.     }
  216.  
  217.  
  218.  
  219.     if (this.getParameter("FontSize") != null)
  220.  
  221.     {
  222.  
  223.         Integer IntFontSize = new Integer(this.getParameter("FontSize"));
  224.  
  225.         FontSize = IntFontSize.intValue();
  226.  
  227.     }
  228.  
  229.  
  230.  
  231.     if (this.getParameter("FontWeight") != null)
  232.  
  233.     {
  234.  
  235.         String SFontWeight = new String(this.getParameter("FontWeight"));
  236.  
  237.         SFontWeight = SFontWeight.toLowerCase();
  238.  
  239.         if (SFontWeight.equals("bold")){ FontWeight = Font.BOLD; }
  240.  
  241.         if (SFontWeight.equals("italic")){ FontWeight = Font.ITALIC;}
  242.  
  243.         if (SFontWeight.equals("plain")){ FontWeight = Font.PLAIN;}
  244.  
  245.     }
  246.  
  247.     showfont = new Font(FontFamily, FontWeight, FontSize);  
  248.  
  249.  
  250.  
  251.     im=createImage(size().width,size().height);
  252.  
  253.     osGraphics=im.getGraphics();
  254.  
  255.   }
  256.  
  257.  
  258.  
  259.   public void paint(Graphics g){
  260.  
  261.     paintText(osGraphics);
  262.  
  263.     g.drawImage(im,0,0,null);
  264.  
  265.   }
  266.  
  267.  
  268.  
  269.   public void paintText(Graphics g){
  270.  
  271.     int i,x_coord,y_coord;
  272.  
  273.     int char_width;
  274.  
  275.  
  276.  
  277.     g.setColor(backgroundCol);
  278.  
  279.     g.fillRect(0,0,width,height);
  280.  
  281.     g.clipRect(0,0,width,height);
  282.  
  283.     g.setFont(showfont);
  284.  
  285.     g.setColor(TextColor[seq]);
  286.  
  287.     fmetrics=g.getFontMetrics();
  288.  
  289.     char_width = fmetrics.charsWidth(mesg[seq],0,1);
  290.  
  291.     realLength=fmetrics.stringWidth(input_text[seq]);
  292.  
  293.     realHeight=fmetrics.getHeight();
  294.  
  295.     
  296.  
  297.     if(nervous){
  298.  
  299.       for(i=0;i<input_text[seq].length();i++)
  300.  
  301.       {
  302.  
  303.     x_coord = (int) (Math.random()*6+char_width*i+xpos);
  304.  
  305.     y_coord = (int) (Math.random()*6+ypos);
  306.  
  307.     g.drawChars(mesg[seq], i,1,x_coord,y_coord);
  308.  
  309.       }
  310.  
  311.     }else{
  312.  
  313.       g.drawString(input_text[seq],xpos,ypos);
  314.  
  315.     }
  316.  
  317.   }
  318.  
  319.  
  320.  
  321.  public void start(){
  322.  
  323.    if(killme==null){
  324.  
  325.       killme=new Thread(this);
  326.  
  327.        killme.start();
  328.  
  329.    }
  330.  
  331.  }
  332.  
  333.  
  334.  
  335.  public void setcoord(){
  336.  
  337.      switch(seq){
  338.  
  339.     case 0: 
  340.  
  341.            xpos = xpos-2;
  342.  
  343.            ypos = (height+realHeight)/2 ;
  344.  
  345.            if(xpos<-realLength){
  346.  
  347.               ypos = height;
  348.  
  349.             realLength=fmetrics.charsWidth(mesg[1],0,input_text[1].length());
  350.  
  351.             xpos = (width - realLength)/2 ;
  352.  
  353.             seq = 1;
  354.  
  355.            }
  356.  
  357.         break ;
  358.  
  359.     case 1:
  360.  
  361.         ypos = ypos - 1;
  362.  
  363.            if(ypos<-realHeight){
  364.  
  365.               xpos = -fmetrics.stringWidth(input_text[2]);
  366.  
  367.             ypos = (height+realHeight)/2 ;
  368.  
  369.             seq = 2 ;
  370.  
  371.            }
  372.  
  373.         break ;
  374.  
  375.     case 2:
  376.  
  377.         xpos = xpos+2;
  378.  
  379.            if(xpos > realLength){
  380.  
  381.             realLength=fmetrics.charsWidth(mesg[3],0,input_text[3].length());
  382.  
  383.               xpos = (width - realLength)/2 ;
  384.  
  385.             ypos = 0 ;
  386.  
  387.             seq = 3;
  388.  
  389.            }
  390.  
  391.         break ;
  392.  
  393.     case 3 :
  394.  
  395.         ypos = ypos + 1;
  396.  
  397.            if(ypos > (realHeight + height)){
  398.  
  399.               xpos = width;
  400.  
  401.             ypos = (height+realHeight)/2 ;
  402.  
  403.             seq = 0 ;
  404.  
  405.            }
  406.  
  407.         break ;
  408.  
  409.     };
  410.  
  411.  }
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  public void run(){
  420.  
  421.  
  422.  
  423.  while(killme != null){
  424.  
  425.    try {Thread.sleep(s_time[speed]);}
  426.  
  427.     catch(InterruptedException e){}
  428.  
  429.    setcoord();
  430.  
  431.    repaint();
  432.  
  433.    }
  434.  
  435.  }
  436.  
  437.  
  438.  
  439.  public void update(Graphics g){
  440.  
  441.    paint(g);
  442.  
  443.  }
  444.  
  445.  
  446.  
  447.  public boolean handleEvent(Event evt) {
  448.  
  449.    if (evt.id == Event.MOUSE_DOWN) {
  450.  
  451.        if (suspended) {
  452.  
  453.               killme.resume();
  454.  
  455.         } else {
  456.  
  457.               killme.suspend();
  458.  
  459.         }
  460.  
  461.         suspended = !suspended;
  462.  
  463.    }
  464.  
  465.    return true;
  466.  
  467.  }
  468.  
  469.  
  470.  
  471.  public void stop(){
  472.  
  473.    if(killme != null)
  474.  
  475.       killme.stop();
  476.  
  477.      killme=null;
  478.  
  479.  }
  480.  
  481.  
  482.  
  483. }
  484.  
  485.