home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1996 December / PCO1296.ISO / filesbbs / frei / fade.arj / FADE.EXE / Fade.java < prev    next >
Encoding:
Java Source  |  1996-04-06  |  9.2 KB  |  362 lines

  1. /***********************************************************
  2.  
  3.  *
  4.  
  5.  * Fade v1.05 by Giuseppe Gennaro, 1996
  6.  
  7.  *
  8.  
  9.  *    Feel free to modify or distribute this code as you 
  10.  
  11.  *    wish, just mention the author somewhere on the page
  12.  
  13.  *      you use it. It is one of my many learning attempts at
  14.  
  15.  *    the java language.  Here are the parameters for the
  16.  
  17.  *     applet:
  18.  
  19.  *
  20.  
  21.  *    <applet code="Fade.class" width="300" height="100">
  22.  
  23.  *    <param name="bgcolor" value="ffffff">
  24.  
  25.  *    <param name="txtcolor" value="000000">
  26.  
  27.  *    <param name="changefactor" value="dx">
  28.  
  29.  *    <param name="text1" value="First text">
  30.  
  31.  *    <param name="url1" value="http://www.xxx.xxx">
  32.  
  33.  *    <param name="font1" value="FontName,FontStyle,FontSize">
  34.  
  35.  *    <param name="text2" value="Second text">
  36.  
  37.  *    <param name="url2" value="http://www.xxx.xxx">
  38.  
  39.  *    <param name="fontname2" value="FontName,FontStyle,FontSize">
  40.  
  41.  *     ...
  42.  
  43.  *    </applet>
  44.  
  45.  *      
  46.  
  47.  *    You should be able to change the width, height, background 
  48.  
  49.  *     color, text color, etc...  Also, the text with the extension
  50.  
  51.  *    number following it is affected by all other parameters with
  52.  
  53.  *     that same extension number. (Just stating the obvious.) 
  54.  
  55.  */
  56.  
  57.  
  58.  
  59. import java.awt.*;
  60.  
  61. import java.lang.*;
  62.  
  63. import java.net.*;
  64.  
  65. import java.applet.*;
  66.  
  67. import java.io.*;
  68.  
  69.  
  70.  
  71. //////////////////////////////////////
  72.  
  73. // OTHER CLASSES
  74.  
  75. //////////////////////////////////////
  76.  
  77.  
  78.  
  79. class Thoughts {
  80.  
  81.     static int MAX = 10;
  82.  
  83.     String theThoughts[] = new String[MAX];
  84.  
  85.     URL theUrls[] = new URL[MAX];
  86.  
  87.     Font theFonts[] = new Font[MAX];
  88.  
  89.     int R, G, B;
  90.  
  91.     int dr, dg, db;
  92.  
  93.     int rinit, ginit, binit;
  94.  
  95.     int rfinal, gfinal, bfinal;
  96.  
  97.     Color bgColor;
  98.  
  99.     boolean maxxed = false;
  100.  
  101.     boolean darker = false;
  102.  
  103.     int curr, count;
  104.  
  105.  
  106.  
  107.     Thoughts() {
  108.  
  109.         R = G = B = 0;
  110.  
  111.         dr = dg = db = 1;
  112.  
  113.         rinit = ginit = binit = 0;
  114.  
  115.         rfinal = gfinal = bfinal = 255;
  116.  
  117.         bgColor = new Color(R,G,B);
  118.  
  119.         curr = -1;
  120.  
  121.         count = 0;
  122.  
  123.     }
  124.  
  125.  
  126.  
  127.     public void SetBackground(int i, int j, int k) {
  128.  
  129.         R = rinit = i;
  130.  
  131.         G = ginit = j;
  132.  
  133.         B = binit = k;
  134.  
  135.         bgColor = new Color(R,G,B);
  136.  
  137.     }
  138.  
  139.  
  140.  
  141.     public Color GetBackground() {
  142.  
  143.         return bgColor;
  144.  
  145.     }
  146.  
  147.  
  148.  
  149.     public void SetTextColor(int i, int j, int k)
  150.  
  151.     {    rfinal = i;
  152.  
  153.         gfinal = j;
  154.  
  155.         bfinal = k;
  156.  
  157.     }
  158.  
  159.  
  160.  
  161.     public void SetChangeFactor(int i) {
  162.  
  163.         if(rfinal > rinit)
  164.  
  165.             dr = i;
  166.  
  167.         else if(rfinal == rinit)
  168.  
  169.             dr = 0;
  170.  
  171.         else
  172.  
  173.             dr = -i;
  174.  
  175.         if(gfinal > ginit)
  176.  
  177.             dg = i;
  178.  
  179.         else if(gfinal == ginit)
  180.  
  181.             dg = 0;
  182.  
  183.         else
  184.  
  185.             dg = -i;
  186.  
  187.         if(bfinal > binit)
  188.  
  189.             db = i;
  190.  
  191.         else if(bfinal == binit)
  192.  
  193.             db = 0;
  194.  
  195.         else
  196.  
  197.             db = -i;
  198.  
  199.     }
  200.  
  201.  
  202.  
  203.     public void AddThought(String idea, String url, String fontname, int fontstyle, int fontsize) {
  204.  
  205.         if(curr < MAX)
  206.  
  207.         {    curr++;
  208.  
  209.             count++;
  210.  
  211.             theThoughts[curr] = idea;
  212.  
  213.             try{
  214.  
  215.                 theUrls[curr] = new URL(url);
  216.  
  217.             }catch(MalformedURLException e) {}
  218.  
  219.             theFonts[curr] = new Font(fontname, fontstyle, fontsize);
  220.  
  221.         }
  222.  
  223.     }
  224.  
  225.  
  226.  
  227.     public void Reset() {
  228.  
  229.         curr = 0;
  230.  
  231.     }
  232.  
  233.  
  234.  
  235.     public void Next() {
  236.  
  237.         curr++;
  238.  
  239.         if(curr >= count) Reset();
  240.  
  241.     }
  242.  
  243.  
  244.  
  245.     public void DrawThoughts(Fade that, Graphics g) {
  246.  
  247.         FontMetrics fm = that.getFontMetrics(theFonts[curr]);
  248.  
  249.         Color temp = new Color(R,G,B);
  250.  
  251.         g.setColor(temp);
  252.  
  253.         g.setFont(theFonts[curr]);
  254.  
  255.         g.drawString(theThoughts[curr],
  256.  
  257.                 (that.size().width - fm.stringWidth(theThoughts[curr]))/2,
  258.  
  259.                 (that.size().height + fm.getAscent())/2);
  260.  
  261.     }
  262.  
  263.     
  264.  
  265.     // (that.size().height + fm.getHeight())/2
  266.  
  267.  
  268.  
  269.     public URL GetCurrentURL() {
  270.  
  271.         return theUrls[curr];
  272.  
  273.     }
  274.  
  275.  
  276.  
  277.     public int ChangeColors() {
  278.  
  279.         int pause = 1;
  280.  
  281.  
  282.  
  283.         if(!maxxed)
  284.  
  285.         {  // Adjust colors to fade in...
  286.  
  287.             R += dr;
  288.  
  289.             G += dg;
  290.  
  291.             B += db;
  292.  
  293.             if(!((dr > 0 && R < rfinal) || (dr < 0 && R > rfinal)))
  294.  
  295.                 R = rfinal;
  296.  
  297.             if(!((dg > 0 && G < gfinal) || (dg < 0 && G > gfinal)))
  298.  
  299.                 G = gfinal;
  300.  
  301.             if(!((db > 0 && B < bfinal) || (db < 0 && B > bfinal)))
  302.  
  303.                 B = bfinal;
  304.  
  305.             if(R == rfinal && G == gfinal && B == bfinal)
  306.  
  307.             {    maxxed = true;
  308.  
  309.                 pause = 10;
  310.  
  311.             }
  312.  
  313.         }
  314.  
  315.         else
  316.  
  317.         {  // Adjust colors to fade out...
  318.  
  319.             R -= dr;
  320.  
  321.             G -= dg;
  322.  
  323.             B -= db;
  324.  
  325.             if(!((dr > 0 && R > rinit) || (dr < 0 && R < rinit)))
  326.  
  327.                 R = rinit;
  328.  
  329.             if(!((dg > 0 && G > ginit) || (dg < 0 && G < ginit)))
  330.  
  331.                 G = ginit;
  332.  
  333.             if(!((db > 0 && B > binit) || (db < 0 && B < binit)))
  334.  
  335.                 B = binit;
  336.  
  337.             if(R == rinit && G == ginit && B == binit)
  338.  
  339.             {    maxxed = false;
  340.  
  341.                 pause = 10;
  342.  
  343.                 Next();
  344.  
  345.             }
  346.  
  347.         }
  348.  
  349.         return pause;
  350.  
  351.     }
  352.  
  353.  
  354.  
  355. }
  356.  
  357.  
  358.  
  359. //------------------------------------
  360.  
  361.  
  362.  
  363. public class Fade extends java.applet.Applet implements Runnable {
  364.  
  365.     Thoughts thoughts = new Thoughts();
  366.  
  367.     Thread runner = null;
  368.  
  369.  
  370.  
  371.     public void init() {
  372.  
  373.         // Set the background...
  374.  
  375.         String bgRGB = getParameter("bgcolor");
  376.  
  377.         if(bgRGB == null || bgRGB.length() != 6)
  378.  
  379.         {    thoughts.SetBackground(0,0,0);
  380.  
  381.         }
  382.  
  383.         else
  384.  
  385.         {    thoughts.SetBackground(HexToInt(bgRGB.substring(0,2)),
  386.  
  387.                         HexToInt(bgRGB.substring(2,4)),
  388.  
  389.                         HexToInt(bgRGB.substring(4)));
  390.  
  391.         }
  392.  
  393.         setBackground(thoughts.GetBackground());
  394.  
  395.  
  396.  
  397.         // Set the text color...
  398.  
  399.         String txtRGB = getParameter("txtcolor");
  400.  
  401.         if(txtRGB == null || txtRGB.length() != 6)
  402.  
  403.         {    thoughts.SetTextColor(255,255,255);
  404.  
  405.         }
  406.  
  407.         else
  408.  
  409.         {
  410.  
  411.             thoughts.SetTextColor(HexToInt(txtRGB.substring(0,2)),
  412.  
  413.                         HexToInt(txtRGB.substring(2,4)),
  414.  
  415.                         HexToInt(txtRGB.substring(4)));
  416.  
  417.         }
  418.  
  419.  
  420.  
  421.         // Set the delta for the changing color...
  422.  
  423.         String changeFactor = getParameter("changefactor");
  424.  
  425.         if(changeFactor == null)
  426.  
  427.             thoughts.SetChangeFactor(1);
  428.  
  429.         else
  430.  
  431.             thoughts.SetChangeFactor(Integer.valueOf(changeFactor).intValue());
  432.  
  433.  
  434.  
  435.         // Obtaining the data for the thoughts...
  436.  
  437.         GetThoughts(this);
  438.  
  439.  
  440.  
  441.         thoughts.Reset();
  442.  
  443.         resize(size().width, size().height);
  444.  
  445.     }
  446.  
  447.  
  448.  
  449.     public void start() {
  450.  
  451.         if(runner == null)
  452.  
  453.         {    runner = new Thread(this);
  454.  
  455.             runner.setPriority(runner.MIN_PRIORITY);
  456.  
  457.             runner.start();
  458.  
  459.         }
  460.  
  461.     }
  462.  
  463.  
  464.  
  465.     public void stop() {
  466.  
  467.         runner = null;
  468.  
  469.     }
  470.  
  471.  
  472.  
  473.     public void paint(Graphics g) {
  474.  
  475.     }
  476.  
  477.  
  478.  
  479.     public void update(Graphics g) {
  480.  
  481.         thoughts.DrawThoughts(this, g);
  482.  
  483.     }
  484.  
  485.  
  486.  
  487.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  488.  
  489.         super.getAppletContext().showDocument(thoughts.GetCurrentURL());
  490.  
  491.         return true;
  492.  
  493.     }
  494.  
  495.  
  496.  
  497.     public boolean mouseEnter(java.awt.Event evt, int x, int y) {
  498.  
  499.         // Showing who made this...
  500.  
  501.         showStatus("Fade.java by Giuseppe Gennaro");
  502.  
  503.         return true;
  504.  
  505.     }
  506.  
  507.  
  508.  
  509.     public void run() {
  510.  
  511.         int sleepfactor;
  512.  
  513.  
  514.  
  515.         while(runner != null)
  516.  
  517.         {  sleepfactor = thoughts.ChangeColors();
  518.  
  519.             repaint();
  520.  
  521.             try{runner.sleep(25*sleepfactor);}catch(InterruptedException e) {}
  522.  
  523.         }
  524.  
  525.     }
  526.  
  527.  
  528.  
  529.     public String getAppletInfo() {
  530.  
  531.         return "Fade by Giuseppe Gennaro";
  532.  
  533.     }
  534.  
  535.  
  536.  
  537.     ////////////////////////////////////////
  538.  
  539.     // OTHER FUNCTIONS
  540.  
  541.     ////////////////////////////////////////
  542.  
  543.  
  544.  
  545.     public void GetThoughts(Fade that){
  546.  
  547.         boolean done = false;
  548.  
  549.         int i=1;
  550.  
  551.  
  552.  
  553.         while(!done)
  554.  
  555.         {    String extension = String.valueOf(i);
  556.  
  557.             
  558.  
  559.             String fontParam = "font" + extension;
  560.  
  561.             String fontName;
  562.  
  563.             int fontSize, fontStyle;
  564.  
  565.             String textParam = "text" + extension;
  566.  
  567.             String urlParam = "url" + extension;
  568.  
  569.     
  570.  
  571.             String font = super.getParameter(fontParam);
  572.  
  573.             if(font == null)
  574.  
  575.             {    fontName = "TimesRoman";
  576.  
  577.                 fontSize = 12;
  578.  
  579.                 fontStyle = Font.PLAIN;
  580.  
  581.                 done = true;
  582.  
  583.             }
  584.  
  585.             else
  586.  
  587.             {    int comma1 = font.indexOf(","),
  588.  
  589.                     comma2 = font.lastIndexOf(",");
  590.  
  591.                 String name = font.substring(0, comma1);
  592.  
  593.                 String style = font.substring(comma1+1, comma2);
  594.  
  595.                 String size = font.substring(comma2+1);
  596.  
  597.                 
  598.  
  599.                 fontName = name;
  600.  
  601.                 fontSize = Integer.valueOf(size).intValue();
  602.  
  603.                 if(style.equalsIgnoreCase("PLAIN"))
  604.  
  605.                     fontStyle = Font.PLAIN;
  606.  
  607.                 else if(style.equalsIgnoreCase("BOLD"))
  608.  
  609.                     fontStyle = Font.BOLD;
  610.  
  611.                 else if(style.equalsIgnoreCase("ITALIC"))
  612.  
  613.                     fontStyle = Font.ITALIC;
  614.  
  615.                 else
  616.  
  617.                     fontStyle = Font.PLAIN;
  618.  
  619.             }
  620.  
  621.  
  622.  
  623.             String theText = that.getParameter(textParam);
  624.  
  625.             if(theText == null)
  626.  
  627.             {    theText = "No Text Given.";
  628.  
  629.                 done = true;
  630.  
  631.             }
  632.  
  633.             String theUrl = that.getParameter(urlParam);
  634.  
  635.  
  636.  
  637.             if(!done)
  638.  
  639.                 thoughts.AddThought(theText, theUrl, fontName, fontStyle, fontSize);
  640.  
  641.             i++;
  642.  
  643.         }
  644.  
  645.     }
  646.  
  647.  
  648.  
  649.     public int HexToInt(String value) {
  650.  
  651.         int answer = 0;
  652.  
  653.  
  654.  
  655.         if(value.substring(0,1).equalsIgnoreCase("a"))
  656.  
  657.             answer = 160;
  658.  
  659.         else if(value.substring(0,1).equalsIgnoreCase("b"))
  660.  
  661.             answer = 176;
  662.  
  663.         else if(value.substring(0,1).equalsIgnoreCase("c"))
  664.  
  665.             answer = 192;
  666.  
  667.         else if(value.substring(0,1).equalsIgnoreCase("d"))
  668.  
  669.             answer = 208;
  670.  
  671.         else if(value.substring(0,1).equalsIgnoreCase("e"))
  672.  
  673.             answer = 224;
  674.  
  675.         else if(value.substring(0,1).equalsIgnoreCase("f"))
  676.  
  677.             answer = 240;
  678.  
  679.         else
  680.  
  681.             answer = Integer.valueOf(value.substring(0,1)).intValue() * 16;
  682.  
  683.  
  684.  
  685.         if(value.substring(1).equalsIgnoreCase("a"))
  686.  
  687.             answer += 10;
  688.  
  689.         else if(value.substring(1).equalsIgnoreCase("b"))
  690.  
  691.             answer += 11;
  692.  
  693.         else if(value.substring(1).equalsIgnoreCase("c"))
  694.  
  695.             answer += 12;
  696.  
  697.         else if(value.substring(1).equalsIgnoreCase("d"))
  698.  
  699.             answer += 13;
  700.  
  701.         else if(value.substring(1).equalsIgnoreCase("e"))
  702.  
  703.             answer += 14;
  704.  
  705.         else if(value.substring(1).equalsIgnoreCase("f"))
  706.  
  707.             answer += 15;
  708.  
  709.         else
  710.  
  711.             answer += Integer.valueOf(value.substring(1)).intValue();
  712.  
  713.  
  714.  
  715.         return answer;
  716.  
  717.     }
  718.  
  719.  
  720.  
  721. }
  722.  
  723.