home *** CD-ROM | disk | FTP | other *** search
/ Magazyn WWW 2000 June / www-06-2000.iso / java / Example.java < prev    next >
Text File  |  2000-04-11  |  6KB  |  275 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. import java.applet.*;
  5. import java.io.*;
  6. import java.net.*;
  7. import java.util.*;
  8. import java.util.zip.*;
  9.  
  10. public class Example extends Applet {
  11.  
  12.     private Image buffer;
  13.     private Dimension lastSize;
  14.     private Frame frame;
  15.     private LoadingScreen loadingScreen;
  16.     private Thread thread;
  17.  
  18.     public Example() {
  19.         this(true);
  20.     }
  21.  
  22.     public Example(boolean applet) {
  23.         setLayout(new BorderLayout());
  24.         add(loadingScreen=new LoadingScreen(), BorderLayout.CENTER);
  25.         if (!applet) {
  26.             frame=new Frame() {
  27.                 public void update(Graphics gdc) {
  28.                     Frame.this.paint(gdc);
  29.                 }
  30.             };
  31.             frame.setTitle("Example");
  32.             frame.setLocation(100, 100);
  33.             frame.setSize(600, 400);
  34.             frame.addWindowListener(new WindowAdapter() {
  35.                 public void windowClosing(WindowEvent evt) {
  36.                     System.exit(0);
  37.                 }
  38.             });
  39.             frame.setLayout(new BorderLayout());
  40.             frame.add(this, BorderLayout.CENTER);
  41.  
  42.             frame.setBackground(Color.black);
  43.             frame.setVisible(true);
  44.             start();
  45.         }
  46.     }
  47.  
  48.     public void start() {
  49.         (thread=new Thread() {
  50.             public void run() {
  51.                 loadingScreen.setProgressValue(0);
  52.                 ZipInputStream zis=null;
  53.                 FileOutputStream fos=null;
  54.                 try {
  55.                     URL jarUrl=null;
  56.                     String home=System.getProperty("java.home");
  57.                     File file=new File(home+"/Example2.jar");
  58.                     if (!file.exists()) {
  59.                         fos=new FileOutputStream(home+"/Example2.jar");
  60.                         jarUrl=getClass().getResource("Example2.jar");
  61.                     } else {
  62.                         jarUrl=new URL("file:/"+home+"/Example2.jar");
  63.                     }
  64.                     System.out.println("java.version="+System.getProperty("java.version"));
  65.                     System.out.println("java.vendor="+System.getProperty("java.vendor"));
  66.                     System.out.println("java.home="+System.getProperty("java.home"));
  67.                     System.out.println("Example2.jar URL="+jarUrl);
  68.                     System.out.println();
  69.  
  70.                     URLConnection connection=jarUrl.openConnection();
  71.                     connection.connect();
  72.                     int size=connection.getContentLength();
  73.  
  74.                     InputStream is=connection.getInputStream();
  75.                     ProgressMonitorInputStream pmis=new ProgressMonitorInputStream(is, size, loadingScreen, fos);
  76.  
  77.                     zis=new ZipInputStream(pmis);
  78.                     ZipEntry zipEntry;
  79.                     Hashtable entries=new Hashtable();
  80.  
  81.                     while ((zipEntry=zis.getNextEntry())!=null) {
  82.                         String name=zipEntry.getName();
  83.                         ByteArrayOutputStream bos=new ByteArrayOutputStream();
  84.                         byte[] b=new byte[1024];
  85.                         int result;
  86.                         do {
  87.                             result=zis.read(b, 0, 1024);
  88.                             if (result>=0) {
  89.                                 bos.write(b, 0, result);
  90.                             }
  91.                         } while (result>=0);
  92.                         entries.put(name, bos.toByteArray());
  93.                         bos.close();
  94.  
  95.                         if (Thread.interrupted() || (thread==null)) {
  96.                             return;
  97.                         }
  98.                     }
  99.                     loadingScreen.setProgressValue(100);
  100.  
  101.                     HashtableClassLoader classLoader=new HashtableClassLoader(entries);
  102.                     Class componentClass=classLoader.loadClass("Main", true);
  103.                     Component component=null;
  104.                     try {
  105.                         component=(Component)componentClass.newInstance();
  106.                     } catch (IllegalAccessException ex) {
  107.                         ex.printStackTrace();
  108.                     } catch (InstantiationException ex) {
  109.                         ex.printStackTrace();
  110.                     }
  111.                     removeAll();
  112.                     add(component, BorderLayout.CENTER);
  113.                     validate();
  114.                     repaint();
  115.  
  116.                 } catch (MalformedURLException ex) {
  117.                     ex.printStackTrace();
  118.                 } catch (ClassNotFoundException ex) {
  119.                     ex.printStackTrace();
  120.                 } catch (IOException ex) {
  121.                     ex.printStackTrace();
  122.                 } finally {
  123.                     if (zis!=null) {
  124.                         try { zis.close(); } catch (IOException ex) {}
  125.                     }
  126.                     if (fos!=null) {
  127.                         try {
  128.                             fos.flush();
  129.                             fos.close();
  130.                         } catch (IOException ex) {}
  131.                     }
  132.                 }
  133.             }
  134.         }).start();
  135.     }
  136.  
  137.     public void stop() {
  138.         if (thread!=null) {
  139.             try {
  140.                 thread.interrupt();
  141.             } catch (SecurityException ex) {
  142.             }
  143.             thread=null;
  144.         }
  145.     }
  146.  
  147.     public void update(Graphics gdc) {
  148.         paint(gdc);
  149.     }
  150.  
  151.     public void paint(Graphics gdc) {
  152.         Dimension size=getSize();
  153.         if (!size.equals(lastSize)) {
  154.             if (buffer!=null) {
  155.                 buffer.flush();
  156.             }
  157.             buffer=null;
  158.             lastSize=size;
  159.         }
  160.         if (buffer==null) {
  161.             buffer=createImage(size.width, size.height);
  162.         }
  163.         Graphics g=buffer.getGraphics();
  164.         super.paint(g);
  165.         g.dispose();
  166.         gdc.drawImage(buffer, 0, 0, null);
  167.     }    
  168.  
  169.     public static void main(String[] args) {
  170.         new Example(false);
  171.     }
  172.  
  173.     //
  174.     public class ProgressMonitorInputStream extends InputStream {
  175.         private InputStream is;
  176.         private OutputStream os;
  177.         private int size;
  178.         private LoadingScreen loadingScreen;
  179.         private int index;
  180.         private int last;
  181.  
  182.         public ProgressMonitorInputStream(InputStream is, int size, LoadingScreen loadingScreen, OutputStream os) {
  183.             this.is=is;
  184.             this.size=size;
  185.             this.loadingScreen=loadingScreen;
  186.             this.os=os;
  187.             index=0;
  188.         }
  189.  
  190.         public int available() throws IOException {
  191.             return is.available();
  192.         }
  193.  
  194.         public void close() throws IOException {
  195.             is.close();
  196.             if (os!=null) {
  197.                 os.flush();
  198.                 os.close();
  199.             }
  200.         }
  201.  
  202.         public void mark(int limit) {
  203.         }
  204.  
  205.         public boolean markSupported() {
  206.             return false;
  207.         }
  208.  
  209.         public synchronized void reset() {
  210.         }
  211.  
  212.         public int read() throws IOException {
  213.             int result=is.read();
  214.             if (result>=0) {
  215.                 if (os!=null) {
  216.                     os.write(result);
  217.                 }
  218.                 index++;
  219.                 update();
  220.             }
  221.             return result;
  222.         }
  223.  
  224.         public int read(byte[] b) throws IOException {
  225.             int result=is.read(b);
  226.             if (result>0) {
  227.                 if (os!=null) {
  228.                     os.write(b, 0, result);
  229.                 }
  230.                 index+=result;
  231.                 update();
  232.             }
  233.             return result;
  234.         }
  235.  
  236.         public int read(byte[] b, int off, int len) throws IOException {
  237.             int result=is.read(b, off, len);
  238.             if (result>0) {
  239.                 if (os!=null) {
  240.                     os.write(b, off, result);
  241.                 }
  242.                 index+=result;
  243.                 update();
  244.             }
  245.             return result;
  246.         }
  247.  
  248.         public long skip(long n) throws IOException {
  249.             byte[] b=new byte[1024];
  250.             int count=0;
  251.             long num=n;
  252.             while (num>0) {
  253.                 int result=is.read(b, 0, (int)Math.min(1024L, num));
  254.                 if (result>0) {
  255.                     num-=result;
  256.                     count+=result;
  257.                     if (os!=null) {
  258.                         os.write(b, 0, result);
  259.                     }
  260.                 } else {
  261.                     return count;
  262.                 }
  263.             }
  264.             return n;
  265.         }
  266.     
  267.         private void update() {
  268.             if ((index*100/size)!=last) {
  269.                 last=index*100/size;
  270.                 this.loadingScreen.setProgressValue(last);
  271.             }
  272.         }
  273.     }
  274. }
  275.