• About Me
  • My Slides

WindyGallery's Weblog

~ I am a normal man in quite imperfect little World.

WindyGallery's Weblog

Monthly Archives: February 2010

Unzip by Java

21 Sunday Feb 2010

Posted by windygallery in Developer

≈ Leave a comment

Tags

unzip

package zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;

public class UnZip {

	static final int BUFFER = 2048;

	public UnZip(String input)	{
		try {
	         BufferedOutputStream dest = null;
	         BufferedInputStream is = null;
	         ZipEntry entry;
	         java.util.zip.ZipFile zipfile = new java.util.zip.ZipFile(input);
	         Enumeration e = zipfile.entries();
	         while(e.hasMoreElements()) {
	            entry = (ZipEntry) e.nextElement();
	            System.out.println("Extracting: " +entry);
	            is = new BufferedInputStream(zipfile.getInputStream(entry));
	            int count;
	            byte data[] = new byte[BUFFER];
	            FileOutputStream fos = new FileOutputStream(entry.getName());
	            dest = new BufferedOutputStream(fos, BUFFER);
	            while ((count = is.read(data, 0, BUFFER))
	              != -1) {
	               dest.write(data, 0, count);
	            }
	            dest.flush();
	            dest.close();
	            is.close();
	         }
	      } catch(Exception e) {
	         e.printStackTrace();
	      }

	}

	public static void main(String[] args) {
		String input 	= ".\\output\\compress.zip";
		UnZip app = new UnZip(input);
	}
}

Reference: http://java.sun.com/developer/technicalArticles/Programming/compression/

Zip by Java

21 Sunday Feb 2010

Posted by windygallery in Developer

≈ Leave a comment

Tags

zip

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFile {

	static final int BUFFER = 2048;	

	public ZipFile(String input, String output)	{
		try	{
	         BufferedInputStream origin = null;
	         FileOutputStream dest 	= new FileOutputStream(output);
	         ZipOutputStream out 	= new ZipOutputStream(new BufferedOutputStream(dest));
	         //out.setMethod(ZipOutputStream.DEFLATED);

	         byte data[] = new byte[BUFFER];

	         // get a list of files from current directory
	         File f = new File(input);
	         String files[] = f.list();

	         for (int i=0; i<files.length; i++) {
	            System.out.println("Adding: "+files[i]);
	            FileInputStream fi = new FileInputStream(input+files[i]);
	            origin = new BufferedInputStream(fi, BUFFER);
	            ZipEntry entry = new ZipEntry(input+files[i]);
	            out.putNextEntry(entry);
	            int count;
	            while((count = origin.read(data, 0,BUFFER)) != -1) {
	               out.write(data, 0, count);
	            }
	            origin.close();
	         }
	         out.close();
	     }
		catch(Exception e) {
	         e.printStackTrace();
	     }
	}

	public static void main(String[] args) {
		String input 	= "C:\\Program Files\\Mozilla Firefox\\";
		String output 	= ".\\output\\compress.zip";
		ZipFile app 	= new ZipFile(input,output);
	}
}

Reference: http://java.sun.com/developer/technicalArticles/Programming/compression/

Class diagrams

20 Saturday Feb 2010

Posted by windygallery in Developer

≈ Leave a comment

Tags

usecase diagram

Double Listeners

20 Saturday Feb 2010

Posted by windygallery in Developer

≈ Leave a comment

Tags

double listeners

This code was implemented for testing we can add two listeners to manage same event.

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class DoubleListener extends Applet {

	private static final long serialVersionUID = 2371255015431000864L;

	public void init()	{
		System.out.println("Init");
		this.addMouseListener(new MyMouseListener1());
		this.addMouseListener(new MyMouseListener2());
	}

	public DoubleListener()	{
		super();
		setSize(600,900);
		setVisible(true);
	}

	public void paint(Graphics g) {
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(), getHeight());
	}

}

class MyMouseListener1 implements MouseListener	{
	public void mouseClicked(MouseEvent e) {
		System.out.println("MyMouseListener1::click()");
	}
	public void mouseEntered(MouseEvent e) {	}
	public void mouseExited(MouseEvent e)  {	}
	public void mousePressed(MouseEvent e) {	}
	public void mouseReleased(MouseEvent e) {	}
}

class MyMouseListener2 implements MouseListener	{
	public void mouseClicked(MouseEvent e) {
		System.out.println("MyMouseListener2::click()");
	}
	public void mouseEntered(MouseEvent e) {	}
	public void mouseExited(MouseEvent e)  {	}
	public void mousePressed(MouseEvent e) {	}
	public void mouseReleased(MouseEvent e) {	}
}

Events and EventListener

18 Thursday Feb 2010

Posted by windygallery in Developer

≈ Leave a comment

Tags

eventListener, Events, java, tutorial

My Simple example with 4 classes for understanding how event and eventListener work together.

------------------------------
package myevents;
import java.util.EventObject;

public class MyEvent extends EventObject {

 private static final long serialVersionUID = 5736098458773344230L;
 private Object data;

 public MyEvent(Object source, Object _data)    {
   super(source);
   data = _data;
 }

 public Object getData()    {
   return data;
 }
}
-------------------------------
package myevents;
import java.util.EventListener;

public interface MyEventListener extends EventListener    {
  public void myEventHappend(MyEvent e);
}
------------------------------
package myevents;
public class PracticalInterface implements MyEventListener    {

 @Override
 public void myEventHappend(MyEvent e) {
    System.out.println((String) e.getData());
 }
}
------------------------------
package myevents;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class AppProgram extends JFrame {

private Vector listeners = new Vector();
private JTextArea _tar  = new JTextArea(22,40);
private JButton _bt1    = new JButton("Yeah");
private JButton _bt2    = new JButton("Yoo");

public AppProgram()    {
super("Application");
setLayout(new FlowLayout());
_bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
shootEvent("Yeah !!");
}
});
_bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
shootEvent("Yoo !?");
}
});
this.add(_tar);
this.add(_bt1);
this.add(_bt2);
}

public void addListener(MyEventListener l)    {
listeners.add(l);
}

public void removeListener(MyEventListener l)    {
listeners.remove(l);
}

public void shootEvent(String text)    {
MyEvent me = new MyEvent(this, text);
if (listeners.size() > 0)    {
((MyEventListener)listeners.firstElement()).myEventHappend(me);
}
}

public static void main(String[] args) {
AppProgram app = new AppProgram();
PracticalInterface lis = new PracticalInterface();
app.addListener(lis);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(400,600);
app.setVisible(true);
}

}
------------------------------
Hope you like it :)
------------------------------

Java: Run instance from Abstract class

04 Thursday Feb 2010

Posted by windygallery in Developer

≈ Leave a comment

Tags

abstract, getInstance(), singleton

public abstract class CallAbstractClass {

public static CallAbstractClass getInstance()    {return new HiddenClass();}
public abstract String print();

public static void main(String[] args) {
CallAbstractClass instance = CallAbstractClass.getInstance();
System.out.println(instance.print());
}
}

class HiddenClass extends CallAbstractClass    {
public HiddenClass()  {    }
public String print() {  return “Yeah”;  }
}

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Concept : Singleton

class Singleton  {

  private static Singleton instance = null;

  private Singleton() { ... }

  public static Singleton getInstance() {
     if (instance==null){
           instance = new Singleotn();
     }
     return instance;
  }

  public void otherMethod()  { ... }

}

Subscribe

  • Entries (RSS)
  • Comments (RSS)

Archives

  • August 2017
  • November 2015
  • August 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • May 2012
  • February 2011
  • January 2011
  • August 2010
  • June 2010
  • May 2010
  • April 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • April 2009
  • March 2009
  • November 2008
  • October 2008

Categories

  • Developer
  • Events
  • Games
  • IDeas
  • Love
  • Photos

Meta

  • Register
  • Log in

Blog at WordPress.com.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • WindyGallery's Weblog
    • Already have a WordPress.com account? Log in now.
    • WindyGallery's Weblog
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...