import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
class main2 extends JWindow implements ActionListener{
public BufferedImage start1,hg,exit1;
public JButton exit,start;
private Thread thread;
public void display()
{
setBounds(400,400,500,300);
setVisible(true);
}
private void load() {
try {
start1 = ImageIO.read(new File("C:\\Users\\User\\Desktop\\titles\\start.png"));
hg = ImageIO.read(new File("C:\\Users\\User\\Desktop\\titles\\hg.png"));
exit1 = ImageIO.read(new File("C:\\Users\\User\\Desktop\\titles\\exit.png"));
} catch(IOException e) {
System.out.println("failed");
}
}
public main2() {
try {
try {
getContentPane().add(new JPanelWithBackground(""));
load();
display();
requestFocusInWindow();
this.setFocusable(true);
exit = new JButton("exit");
start = new JButton("start");
start.addActionListener(this);
start.setFocusable(true);
JProgressBar current = new JProgressBar(0, 100);
Container c = getContentPane();
c.setLayout(null);
c.add(current);
c.add(start);
c.add(exit);
start.setBounds(260,100,90,30);
exit.setIcon(new ImageIcon(exit1));
exit.setBounds(450, 0, 30, 30);
exit.setBackground(null);
exit.setBorder(null);
exit.setBorderPainted(false);
exit.setContentAreaFilled(false);
exit.setFocusPainted(false);
exit.setVisible(true);
exit.addActionListener(this);
setVisible(true);
start.setIcon(new ImageIcon(start1));
String site="http://host.bugs3.com/hg/update.zip";
if(!new File("C://The Hunger Games").exists()){
new File("C://The Hunger Games").mkdir();
} else {
}
String folder="C://The Hunger Games//update.zip";
JFrame frm=new JFrame();
current.setSize(50,50);
// current.setValue(0);
current.setBounds(50, 50, 350, 25);
current.setStringPainted(true);
current.setBackground(Color.green);
current.setForeground(Color.red);
try {
URL url=new URL(site);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
int filesize = connection.getContentLength();
float totalDataRead=0;
java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(folder);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int i=0;
while((i=in.read(data,0,1024))>=0)
{
totalDataRead=totalDataRead+i;
bout.write(data,0,i);
float Percent=(totalDataRead*100)/filesize;
current.setValue((int)Percent);
}
bout.close();
in.close();
}
catch(Exception e)
{
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null,e.getMessage(), "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
catch(IOException ex)
{
Logger.getLogger(main2.class.getName()).log(
Level.SEVERE, null, ex);
}
unzipFile("C://The Hunger Games//update.zip");
} catch (IOException ex) {
Logger.getLogger(main2.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void unzipFile(String filePath) throws IOException{
FileInputStream fis = null;
ZipInputStream zipIs = null;
ZipEntry zEntry = null;
try {
fis = new FileInputStream(filePath);
zipIs = new ZipInputStream(new BufferedInputStream(fis));
while((zEntry = zipIs.getNextEntry()) != null){
try{
byte[] tmp = new byte[4*1024];
FileOutputStream fos = null;
String opFilePath = "C://The Hunger Games//"+zEntry.getName();
System.out.println("Extracting file to "+opFilePath);
fos = new FileOutputStream(opFilePath);
int size = 0;
while((size = zipIs.read(tmp)) != -1){
fos.write(tmp, 0 , size);
}
fos.flush();
fos.close();
} catch(Exception ex){
}
}
zipIs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == exit) {
System.exit(0);
}
if(e.getSource() == start) {
dispose();
try {
Process ps=Runtime.getRuntime().exec(new String[]{"java","-jar","C://The Hunger Games//HungerGamesClient.jar"});
ps.waitFor();
java.io.InputStream is=ps.getInputStream();
byte b[]=new byte[is.available()];
is.read(b,0,b.length);
System.out.println(new String(b));
} catch (IOException ex) {
Logger.getLogger(main2.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(main2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}