home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magazyn WWW 2000 June
/
www-06-2000.iso
/
java
/
Example.java
< prev
next >
Wrap
Text File
|
2000-04-11
|
6KB
|
275 lines
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.*;
public class Example extends Applet {
private Image buffer;
private Dimension lastSize;
private Frame frame;
private LoadingScreen loadingScreen;
private Thread thread;
public Example() {
this(true);
}
public Example(boolean applet) {
setLayout(new BorderLayout());
add(loadingScreen=new LoadingScreen(), BorderLayout.CENTER);
if (!applet) {
frame=new Frame() {
public void update(Graphics gdc) {
Frame.this.paint(gdc);
}
};
frame.setTitle("Example");
frame.setLocation(100, 100);
frame.setSize(600, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.setBackground(Color.black);
frame.setVisible(true);
start();
}
}
public void start() {
(thread=new Thread() {
public void run() {
loadingScreen.setProgressValue(0);
ZipInputStream zis=null;
FileOutputStream fos=null;
try {
URL jarUrl=null;
String home=System.getProperty("java.home");
File file=new File(home+"/Example2.jar");
if (!file.exists()) {
fos=new FileOutputStream(home+"/Example2.jar");
jarUrl=getClass().getResource("Example2.jar");
} else {
jarUrl=new URL("file:/"+home+"/Example2.jar");
}
System.out.println("java.version="+System.getProperty("java.version"));
System.out.println("java.vendor="+System.getProperty("java.vendor"));
System.out.println("java.home="+System.getProperty("java.home"));
System.out.println("Example2.jar URL="+jarUrl);
System.out.println();
URLConnection connection=jarUrl.openConnection();
connection.connect();
int size=connection.getContentLength();
InputStream is=connection.getInputStream();
ProgressMonitorInputStream pmis=new ProgressMonitorInputStream(is, size, loadingScreen, fos);
zis=new ZipInputStream(pmis);
ZipEntry zipEntry;
Hashtable entries=new Hashtable();
while ((zipEntry=zis.getNextEntry())!=null) {
String name=zipEntry.getName();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] b=new byte[1024];
int result;
do {
result=zis.read(b, 0, 1024);
if (result>=0) {
bos.write(b, 0, result);
}
} while (result>=0);
entries.put(name, bos.toByteArray());
bos.close();
if (Thread.interrupted() || (thread==null)) {
return;
}
}
loadingScreen.setProgressValue(100);
HashtableClassLoader classLoader=new HashtableClassLoader(entries);
Class componentClass=classLoader.loadClass("Main", true);
Component component=null;
try {
component=(Component)componentClass.newInstance();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
}
removeAll();
add(component, BorderLayout.CENTER);
validate();
repaint();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (zis!=null) {
try { zis.close(); } catch (IOException ex) {}
}
if (fos!=null) {
try {
fos.flush();
fos.close();
} catch (IOException ex) {}
}
}
}
}).start();
}
public void stop() {
if (thread!=null) {
try {
thread.interrupt();
} catch (SecurityException ex) {
}
thread=null;
}
}
public void update(Graphics gdc) {
paint(gdc);
}
public void paint(Graphics gdc) {
Dimension size=getSize();
if (!size.equals(lastSize)) {
if (buffer!=null) {
buffer.flush();
}
buffer=null;
lastSize=size;
}
if (buffer==null) {
buffer=createImage(size.width, size.height);
}
Graphics g=buffer.getGraphics();
super.paint(g);
g.dispose();
gdc.drawImage(buffer, 0, 0, null);
}
public static void main(String[] args) {
new Example(false);
}
//
public class ProgressMonitorInputStream extends InputStream {
private InputStream is;
private OutputStream os;
private int size;
private LoadingScreen loadingScreen;
private int index;
private int last;
public ProgressMonitorInputStream(InputStream is, int size, LoadingScreen loadingScreen, OutputStream os) {
this.is=is;
this.size=size;
this.loadingScreen=loadingScreen;
this.os=os;
index=0;
}
public int available() throws IOException {
return is.available();
}
public void close() throws IOException {
is.close();
if (os!=null) {
os.flush();
os.close();
}
}
public void mark(int limit) {
}
public boolean markSupported() {
return false;
}
public synchronized void reset() {
}
public int read() throws IOException {
int result=is.read();
if (result>=0) {
if (os!=null) {
os.write(result);
}
index++;
update();
}
return result;
}
public int read(byte[] b) throws IOException {
int result=is.read(b);
if (result>0) {
if (os!=null) {
os.write(b, 0, result);
}
index+=result;
update();
}
return result;
}
public int read(byte[] b, int off, int len) throws IOException {
int result=is.read(b, off, len);
if (result>0) {
if (os!=null) {
os.write(b, off, result);
}
index+=result;
update();
}
return result;
}
public long skip(long n) throws IOException {
byte[] b=new byte[1024];
int count=0;
long num=n;
while (num>0) {
int result=is.read(b, 0, (int)Math.min(1024L, num));
if (result>0) {
num-=result;
count+=result;
if (os!=null) {
os.write(b, 0, result);
}
} else {
return count;
}
}
return n;
}
private void update() {
if ((index*100/size)!=last) {
last=index*100/size;
this.loadingScreen.setProgressValue(last);
}
}
}
}