• About Me
  • My Slides

WindyGallery's Weblog

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

WindyGallery's Weblog

Tag Archives: java

Java Interface

26 Monday Apr 2010

Posted by windygallery in Developer

≈ Leave a comment

Tags

interface, java

public class TestReadInterfaces {

 B b;
 C c;    

 public TestReadInterfaces()    {
 A a = new A();
 Class [] fs = a.getClass().getInterfaces();
 for (int i=0; i<fs.length; i++)    {
 System.out.println(fs[i].getName());
 }
 }

 public static void main(String[] args) {
 new TestReadInterfaces();
 }

}

class A implements B, C {

}

interface B {

}

interface C {

}

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: How to use interface

15 Tuesday Dec 2009

Posted by windygallery in Developer

≈ Leave a comment

Tags

interface, java

interface GunInterface {
public void shot();
}

class Bomber implements GunInterface {
public void shot() {
System.out.println(“Bombbb !?”);
}
}

class Bazuka implements GunInterface {
public void shot() {
System.out.println(“Bazuka”);
}
}

public class RefreshInterface {
public RefreshInterface()    {
Bomber     bomb = new Bomber();
Bazuka    bazu = new Bazuka();
GunInterface gun = bomb;
gun.shot();
gun = bazu;
gun.shot();
((GunInterface) bomb).shot();
((GunInterface) bazu).shot();
}
public static void main(String [] argv)    {
new RefreshInterface();
}
}

Java: Memory Usage

06 Monday Apr 2009

Posted by windygallery in Developer

≈ Leave a comment

Tags

java, Jconsole, Memory

JConsole
A graphical user interface monitoring tool for capturing information about performance, resource consumption of applications running on the Java platform.


.

Reference: http://java.sun.com/javase/6/docs/technotes/guides/management/jconsole.html

.
HOWTO: วิธีการตรวจสอบ memory leak ใน Java application
http://www.jhelp.net/article.aspx?id=10067

.
Determining Memory Usage in Java
http://www.javaspecialists.eu/archive/Issue029.html

Java: Saving Images

05 Sunday Apr 2009

Posted by windygallery in Developer

≈ Leave a comment

Tags

Image, java

public static void saveOldWay(String ref, BufferedImage img) {
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(ref));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
int quality = 5;
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(img);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Saves a BufferedImage to the given file, pathname must not have any
* periods “.” in it except for the one before the format, i.e. C:/images/fooimage.png
* @param img
* @param saveFile
*/
public static void saveImage(BufferedImage img, String ref) {
try {
String format = (ref.endsWith(“.png”)) ? “png” : “jpg”;
ImageIO.write(img, format, new File(ref));
} catch (IOException e) {
e.printStackTrace();
}
}

Reference: http://www.javalobby.org/articles/ultimate-image/#5

Java: How to Measure My Coding’s Speed ?

25 Wednesday Mar 2009

Posted by windygallery in Developer

≈ Leave a comment

Tags

counter, getTime, java

By Coding
========

// capture first time for measuring
long timeA = System.currentTimeMillis();

// do your business
…

// capture second time for measuring
long timeB = System.currentTimeMillis();

// Calculate the different in milliseconds.
System.out.println(“It took you “+(timeB-timeA)+”ms. for ….”);

By Eclipse Profiler
===============

http://www.eclipse.org/tptp/index.php

Solution for Fail Fast problem {Java Programming}

22 Sunday Mar 2009

Posted by windygallery in Developer

≈ Leave a comment

Tags

Fail Fast, java

The Java containers also have a mechanism to prevent more than one process from modifying the contents of a container. The problem occurs if you’re iterating through a container and some other process steps in and inserts, removes, or changes an object in that container. Maybe you’ve already passed that object, maybe it’s ahead of you, maybe the size of the container shrinks after you call size( )—there are many scenarios for disaster. The Java containers library incorporates a fail-fast mechanism that looks for any changes to the container other than the ones your process is personally responsible for. If it detects that someone else is modifying the container, it immediately produces a ConcurrentModificationException. This is the “fail-fast” aspect—it doesn’t try to detect a problem later on using a more complex algorithm.

It’s quite easy to see the fail-fast mechanism in operation—all you have to do is create an iterator and then add something to the collection that the iterator is pointing to, like this:

//: c09:FailFast.java
// Demonstrates the "fail fast" behavior.
import java.util.*;

public class FailFast {
  public static void main(String[] args) {
    Collection c = new ArrayList();
    Iterator it = c.iterator();
    c.add("An object");
    // Causes an exception:
    String s = (String)it.next();
  }
} ///:~

The exception happens because something is placed in the container after the iterator is acquired from the container. The possibility that two parts of the program could be modifying the same container produces an uncertain state, so the exception notifies you that you should change your code—in this case, acquire the iterator after you have added all the elements to the container.

Note that you cannot benefit from this kind of monitoring when you’re accessing the elements of a List using get( ).

Reference: http://www.cs.waikato.ac.nz/~jcleary/230/TIJ/html/Chap09.htm#Index1036

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