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 / DYNARULE / DYNARULE.EXE / dynarule2.java < prev    next >
Encoding:
Java Source  |  1996-03-11  |  3.9 KB  |  146 lines

  1. // dynarule2 - Scalable horizontal rule with moving image(s)
  2.  
  3. //
  4.  
  5. // (c) 1996 Tor Ringstad (torhr@nvg.unit.no)
  6.  
  7. //
  8.  
  9. // $Id: dynarule2.java,v 1.1 1996/02/02 14:31:45 torhr Exp torhr $
  10.  
  11. // $Source: /home/stud/torhr/public_html/java/dynarule/RCS/dynarule2.java,v $
  12.  
  13. //
  14.  
  15. // This is the barebone version!
  16.  
  17.  
  18.  
  19. // Todo:
  20.  
  21. //   o Handle resizing
  22.  
  23. //   o Better color handling
  24.  
  25.  
  26.  
  27. import java.awt.*;
  28.  
  29. import java.lang.*;
  30.  
  31.  
  32.  
  33. public class dynarule2 extends java.applet.Applet implements Runnable {
  34.  
  35.  
  36.  
  37.   int xs,ys;      // x- and y-size of the applets window
  38.  
  39.   int size;       // the size (thickness) of the rule
  40.  
  41.   int dy;         // displacement of the line
  42.  
  43.   int num;        // number of copies of image
  44.  
  45.   Image img;      // holds the image
  46.  
  47.   Color fg,bg;    // foreground and background color
  48.  
  49.   Color lc,dc;    // light and dark color (calc'ed from foreground)
  50.  
  51.   float am,of;    // amplitude and offset
  52.  
  53.   float x;        // sine counter
  54.  
  55.   float dist;     // distance between images
  56.  
  57.   float speed;    // speed of the images (step on the sine curve)
  58.  
  59.  
  60.  
  61.   MediaTracker tracker = null;
  62.  
  63.   Image buffer_i = null;
  64.  
  65.   Graphics buffer_g = null;
  66.  
  67.   Thread kicker = null;
  68.  
  69.   float Pi2 = (float)(Math.PI * 2);
  70.  
  71.  
  72.  
  73.   public void init() {
  74.  
  75.     String s;
  76.  
  77.     tracker = new MediaTracker(this);
  78.  
  79.  
  80.  
  81.     xs = size().width;
  82.  
  83.     ys = size().height;
  84.  
  85.     s = getParameter("img");
  86.  
  87.     img = getImage(getDocumentBase(), s);
  88.  
  89.     tracker.addImage(img,0);
  90.  
  91.     s = getParameter("size");
  92.  
  93.     size = (s == null) ? 1 : Integer.valueOf(s).intValue();
  94.  
  95.     s = getParameter("num");
  96.  
  97.     num = (s == null) ? 1 : Integer.valueOf(s).intValue();
  98.  
  99.     s = getParameter("dy");
  100.  
  101.     dy = (s == null) ? 50 : Integer.valueOf(s).intValue();
  102.  
  103.     s = getParameter("dist");
  104.  
  105.     dist = (s == null) ? 1 : Float.valueOf(s).floatValue();
  106.  
  107.     s = getParameter("speed");
  108.  
  109.     speed = (s == null) ? Pi2/400 : Float.valueOf(s).floatValue();
  110.  
  111.  
  112.  
  113.     s = getParameter("bgcolor");
  114.  
  115.     bg = (s == null) ? getBackground() : 
  116.  
  117.                        new Color(Integer.valueOf(s,16).intValue());
  118.  
  119.     s = getParameter("fgcolor");
  120.  
  121.     fg = (s == null) ? bg :
  122.  
  123.                        new Color(Integer.valueOf(s,16).intValue());
  124.  
  125.     lc = fg.brighter();
  126.  
  127.     dc = fg.darker();
  128.  
  129.  
  130.  
  131.     buffer_i = createImage(size().width,size().height);
  132.  
  133.     buffer_g = buffer_i.getGraphics();
  134.  
  135.   }
  136.  
  137.  
  138.  
  139.   public void start() {
  140.  
  141.     try {
  142.  
  143.       showStatus("dynarule2: Loading image...");
  144.  
  145.       tracker.waitForID(0);
  146.  
  147.       showStatus("");
  148.  
  149.     } catch (InterruptedException c) {
  150.  
  151.       showStatus("dynarule2: Image loading interrupted");
  152.  
  153.       return;
  154.  
  155.     }
  156.  
  157.  
  158.  
  159.     am = (xs - img.getWidth(this)) / 2;
  160.  
  161.     of = am;
  162.  
  163.     x = 0;
  164.  
  165.  
  166.  
  167.     if (kicker==null) {
  168.  
  169.       kicker=new Thread(this);
  170.  
  171.       kicker.start();
  172.  
  173.     }
  174.  
  175.   }
  176.  
  177.  
  178.  
  179.   public void stop() {
  180.  
  181.     if (kicker != null) {
  182.  
  183.       kicker.stop();
  184.  
  185.       kicker = null;
  186.  
  187.     }
  188.  
  189.   }
  190.  
  191.  
  192.  
  193.   public void run() {
  194.  
  195.     while(true) {
  196.  
  197.       x += speed;
  198.  
  199.       if (x > Pi2) x -= Pi2;
  200.  
  201.       repaint();
  202.  
  203.       try {kicker.sleep(15);} catch (InterruptedException e) {}
  204.  
  205.     }
  206.  
  207.   }
  208.  
  209.  
  210.  
  211.   public void paint(Graphics g) {
  212.  
  213.  
  214.  
  215.     // Clear the entire background
  216.  
  217.  
  218.  
  219.     buffer_g.setColor(bg);
  220.  
  221.     buffer_g.fillRect(0,0,xs,ys);
  222.  
  223.  
  224.  
  225.     // Draw the rule
  226.  
  227.  
  228.  
  229.     buffer_g.setColor(lc);
  230.  
  231.     buffer_g.drawLine(xs-1,dy+size,0,dy+size);
  232.  
  233.     buffer_g.drawLine(xs-1,dy+size,xs-1,dy);
  234.  
  235.     buffer_g.setColor(dc);
  236.  
  237.     buffer_g.drawLine(0,dy,xs-1,dy);
  238.  
  239.     buffer_g.drawLine(0,dy,0,dy+size);
  240.  
  241.  
  242.  
  243.     // Draw all the images
  244.  
  245.  
  246.  
  247.     float xb = x;
  248.  
  249.     for(int a = 0; a < num; a++) {
  250.  
  251.       int xpos = (int)(Math.sin(xb)*am + of);
  252.  
  253.       buffer_g.drawImage(img,xpos,0,this);
  254.  
  255.       xb += dist;
  256.  
  257.     }
  258.  
  259.  
  260.  
  261.     // Copy the offscreen working area to the visible one
  262.  
  263.  
  264.  
  265.     g.drawImage(buffer_i,0,0,this);
  266.  
  267.   }
  268.  
  269.  
  270.  
  271.   public final synchronized void update(Graphics g) {
  272.  
  273.     paint(g);
  274.  
  275.   }
  276.  
  277.  
  278.  
  279.  
  280.  
  281. }
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.