home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / WIN95 / IAVAZIP.EXE / DATA.Z / ImageLoader.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-08-18  |  5.7 KB  |  225 lines

  1. package com.sfs.iavazip.viewer;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.MediaTracker;
  7. import java.awt.Toolkit;
  8. import java.awt.image.DirectColorModel;
  9. import java.io.DataInputStream;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.net.URL;
  14.  
  15. public class ImageLoader implements Runnable {
  16.    public boolean imageLoaded;
  17.    public Image newImage;
  18.    protected ProgressStatus imageClient;
  19.    protected int[] frameBuffer;
  20.    protected FileInput imageFile = new FileInput();
  21.    protected boolean debug;
  22.    protected int xRes;
  23.    protected int yRes;
  24.    protected int actLine;
  25.    protected int lastPercent;
  26.    protected int actPercent;
  27.    private static Component client;
  28.    private MediaTracker tracker;
  29.    private ImageLoader newLoader;
  30.    private DirectColorModel colorModel;
  31.    private DataInputStream data;
  32.    private Thread runner;
  33.    private static int done;
  34.    private String theFileName;
  35.    private ImageClassLoader classLoader;
  36.    protected ImageLoaderProducer myProd;
  37.  
  38.    public final void initFrameBuffer(int var1, int var2) {
  39.       this.xRes = var1;
  40.       this.yRes = var2;
  41.       this.frameBuffer = new int[var1 * var2];
  42.    }
  43.  
  44.    protected final void sleep(int var1) {
  45.       try {
  46.          Thread.sleep((long)var1);
  47.       } catch (InterruptedException var2) {
  48.       }
  49.    }
  50.  
  51.    public final void setClient(ProgressStatus var1) {
  52.       this.imageClient = var1;
  53.    }
  54.  
  55.    protected void loadPicture() throws IOException {
  56.       this.print("default loadPicture() method");
  57.    }
  58.  
  59.    public Image load(URL var1, Component var2) {
  60.       String var3 = var1.getFile().toUpperCase();
  61.       this.print("URL file name: " + var1.getFile());
  62.       client = var2;
  63.       this.colorModel = new DirectColorModel(24, 16711680, 65280, 255);
  64.       this.theFileName = var3;
  65.       if (var3.endsWith("GIF") | var3.endsWith("JPG") | var3.endsWith("JPEG")) {
  66.          this.tracker = new MediaTracker(client);
  67.          this.newImage = Toolkit.getDefaultToolkit().getImage(var1);
  68.          this.tracker.addImage(this.newImage, 0);
  69.  
  70.          try {
  71.             this.tracker.waitForID(0);
  72.          } catch (InterruptedException var7) {
  73.             this.print(((Throwable)var7).getMessage());
  74.          }
  75.  
  76.          this.imageLoaded = true;
  77.       } else {
  78.          try {
  79.             this.data = new DataInputStream(var1.openStream());
  80.             this.startLoadProcess(var3, this.data);
  81.          } catch (IOException var6) {
  82.             this.print(((Throwable)var6).getMessage());
  83.          }
  84.       }
  85.  
  86.       return this.newImage;
  87.    }
  88.  
  89.    public Image load(String var1, Component var2) {
  90.       String var3 = var1.toUpperCase();
  91.       client = var2;
  92.       this.colorModel = new DirectColorModel(24, 16711680, 65280, 255);
  93.       this.theFileName = var3;
  94.       if (var3.endsWith("GIF") | var3.endsWith("JPG") | var3.endsWith("JPEG")) {
  95.          this.tracker = new MediaTracker(client);
  96.          this.newImage = Toolkit.getDefaultToolkit().getImage(var1);
  97.          this.tracker.addImage(this.newImage, 0);
  98.  
  99.          try {
  100.             this.tracker.waitForID(0);
  101.          } catch (InterruptedException var7) {
  102.             this.print(((Throwable)var7).getMessage());
  103.          }
  104.  
  105.          this.imageLoaded = true;
  106.       } else {
  107.          try {
  108.             this.data = new DataInputStream(new FileInputStream(var1));
  109.             this.startLoadProcess(var3, this.data);
  110.          } catch (FileNotFoundException var6) {
  111.             this.print(((Throwable)var6).getMessage());
  112.          }
  113.       }
  114.  
  115.       return this.newImage;
  116.    }
  117.  
  118.    public final void debug(boolean var1) {
  119.       this.debug = var1;
  120.    }
  121.  
  122.    public String getInfo() {
  123.       return "";
  124.    }
  125.  
  126.    private boolean startLoadProcess(String var1, DataInputStream var2) {
  127.       Object var3 = null;
  128.       int var4 = var1.length();
  129.       int var5 = var1.lastIndexOf(".");
  130.       String var6 = var1.substring(var5 + 1, var4);
  131.       this.newImage = client.createImage(1, 1);
  132.  
  133.       try {
  134.          Class var7 = this.getClass();
  135.          Class var16;
  136.          if (var7.getClassLoader() == null) {
  137.             this.classLoader = new ImageClassLoader();
  138.             var16 = this.classLoader.loadClass(var7.getName() + var6, true);
  139.          } else {
  140.             try {
  141.                var16 = Class.forName("zfileio.ImageLoader" + var6);
  142.                var16.getName();
  143.             } catch (ClassNotFoundException var14) {
  144.                var16 = null;
  145.                this.print(((Throwable)var14).getMessage());
  146.             }
  147.          }
  148.  
  149.          if (var16 == null) {
  150.             this.print("ImageLoader subclass 'ImageLoader" + var6 + ".class' not found.");
  151.          }
  152.  
  153.          try {
  154.             this.print("Invoking ImageLoader class: " + var16.toString());
  155.             this.newLoader = (ImageLoader)var16.newInstance();
  156.             this.newLoader.debug(this.debug);
  157.             this.newLoader.setClient(this.imageClient);
  158.             this.newLoader.imageFile.use(var2);
  159.             this.print("Starting loader " + this.newLoader.toString());
  160.  
  161.             try {
  162.                this.newLoader.readHeader();
  163.             } catch (IOException var10) {
  164.                this.print(((Throwable)var10).getMessage());
  165.             }
  166.  
  167.             if (client == null) {
  168.                return false;
  169.             }
  170.  
  171.             this.newLoader.myProd = new ImageLoaderProducer();
  172.             this.newLoader.myProd.setDimensions(this.newLoader.xRes, this.newLoader.yRes, this.colorModel);
  173.             this.newImage = this.newLoader.myProd.createImage(client);
  174.             this.start();
  175.          } catch (IOException var11) {
  176.             this.print(((Throwable)var11).getMessage());
  177.          } catch (InstantiationException var12) {
  178.             this.print(((Throwable)var12).getMessage());
  179.          } catch (IllegalAccessException var13) {
  180.             this.print(((Throwable)var13).getMessage());
  181.          }
  182.       } catch (ClassNotFoundException var15) {
  183.          this.print(((Throwable)var15).getMessage());
  184.       }
  185.  
  186.       return false;
  187.    }
  188.  
  189.    public void start() {
  190.       this.runner = new Thread(this);
  191.       this.runner.start();
  192.    }
  193.  
  194.    protected void readHeader() throws IOException {
  195.       this.print("default readHeader() method");
  196.    }
  197.  
  198.    public void run() {
  199.       try {
  200.          this.print("Loading the image...");
  201.          this.newLoader.loadPicture();
  202.          this.print("Updating the frame's buffer...");
  203.          this.newLoader.myProd.update(this.newLoader.frameBuffer);
  204.          Graphics var1 = client.getGraphics();
  205.          client.paint(var1);
  206.          if (this.imageClient != null) {
  207.             this.imageClient.imageLoaded(100, this.newImage, "Image completed ");
  208.          }
  209.       } catch (IOException var3) {
  210.          this.print(((Throwable)var3).getMessage());
  211.       }
  212.  
  213.       this.runner.stop();
  214.       this.runner = null;
  215.       System.gc();
  216.    }
  217.  
  218.    protected final void print(String var1) {
  219.       if (this.debug) {
  220.          System.out.println(var1);
  221.       }
  222.  
  223.    }
  224. }
  225.