home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / vjplusb / msdev / samples / sun / scrollingimages / imagetape.java < prev    next >
Text File  |  1996-07-10  |  3KB  |  118 lines

  1.  
  2. import java.awt.*;
  3.  
  4. public class imagetape extends java.applet.Applet implements Runnable {
  5.     Image imgs[];
  6.     int nimgs;
  7.     int imgwidth;
  8.     int x, newx;
  9.     int dist = 5;
  10.     int timeout = 200;
  11.     Thread scroller;
  12.     String dir;
  13.  
  14.     public void init() {
  15.     String at = getParameter("img");
  16.     dir = (at != null) ? at : "doc:/demo/images/duke";
  17.     at = getParameter("speed");
  18.     timeout = 1000 / ((at == null) ? 4 : Integer.valueOf(at).intValue());
  19.     at = getParameter("dir");
  20.     dist = (at == null) ? 5 : Integer.valueOf(at).intValue();
  21.     at = getParameter("nimgs");
  22.     nimgs = (at == null) ? 16 : Integer.valueOf(at).intValue();
  23.     newx = x = size().width;
  24.  
  25.     imgs = new Image[nimgs];
  26.     for (int i = 0 ; i < nimgs ; i++) {
  27.         imgs[i] = getImage(getDocumentBase(), dir + "/T" + (i+1) + ".gif");
  28.     }
  29.     }
  30.  
  31.     public void start() {
  32.     if (scroller == null) {
  33.         scroller = new Thread(this);
  34.         scroller.start();
  35.     }
  36.     }
  37.     public void stop() {
  38.     if (scroller != null) {
  39.         scroller.stop();
  40.         scroller = null;
  41.     }
  42.     }
  43.  
  44.     public void run() {
  45.     while (true) {
  46.     try {Thread.currentThread().sleep(timeout);} catch (InterruptedException e){}
  47.         scroll(dist);
  48.     }
  49.     }
  50.  
  51.     synchronized void scroll(int dist) {
  52.     newx += dist;
  53.     repaint();
  54.     }
  55.  
  56.     public void update(Graphics g) {
  57.     g.setColor(Color.lightGray);
  58.  
  59.     if (newx != x) {
  60.         //g.clipRect(1, 1, size().width-2, size().height-2);
  61.         int dist = newx - x;
  62.         if (dist > 0) {
  63.         g.copyArea(1, 1, (size().width-2) - dist, size().height-2, dist, 0);
  64.         for (x = newx ; x > size().width ; x -= Math.max(size().width - 2, imgwidth));
  65.         paint(g, 1, dist + 1);
  66.         } else {
  67.         g.copyArea(1 - dist, 1, (size().width-2) + dist, size().height-2, 1, 1);
  68.         for (x = newx ; x < 0 ; x += Math.max(size().width - 2, imgwidth));
  69.         paint(g, (size().width-1) + dist, size().width-1);
  70.         }
  71.     } else {
  72.         paint(g);
  73.     }
  74.     }
  75.  
  76.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
  77.     if ((flags & WIDTH) != 0) {
  78.         imgwidth += img.getWidth(this);
  79.     }
  80.     return super.imageUpdate(img, flags, x, y, w, h);
  81.     }
  82.  
  83.     public void paint(Graphics g, int fromx, int tox) {
  84.     int x = this.x;
  85.     newx = x;
  86.  
  87.     g.setColor(Color.lightGray);
  88.     g.fillRect(fromx, 1, tox - fromx, size().height-2);
  89.     g.clipRect(fromx, 1, tox - fromx, size().height-2);
  90.     g.setColor(Color.black);
  91.  
  92.     for (int i = 0 ; i < nimgs ; i++) {
  93.         if (imgs[i] == null) {
  94.         continue;
  95.         }
  96.  
  97.         int w = imgs[i].getWidth(this);
  98.         int h = imgs[i].getHeight(this);
  99.         if ((w > 0) && (h > 0)) {
  100.         if ((x + w > fromx) && (x < tox)) {
  101.             g.drawImage(imgs[i], x, size().height - (h+1), this);
  102.         }
  103.         if ((x + w) > size().width) {
  104.             x -= Math.max(size().width - 2, imgwidth);
  105.             if ((x + w > fromx) && (x < tox)) {
  106.             g.drawImage(imgs[i], x, size().height - (h + 1), this);
  107.             }
  108.         }
  109.         x += w;
  110.         }
  111.     }
  112.     }
  113.     public void paint(Graphics g) {
  114.     g.draw3DRect(0, 0, size().width, size().height, true);
  115.     paint(g, 1, size().width-1);
  116.     }
  117. }
  118.