Thursday, May 8, 2008
Custom events and listeners in Java
There are three main components in event-driven programming:
*EventObject class
*EventListener interface
*EventGenerator class
The EventObject class extends the java.util.EventObject class and knows the event generator object.
The EventListener interface supplies the method that is to be invoked when the event is generated.
The EventGenerator contains the list of eventlisteners registered for that event. Whenever the event is generated, it iterates through the listeners and invokes their eventlistener method eg. public void actionPerformed() for an ActionEvent.
Undo/Redo implementation in Java
Swing Undo Package
For each operation undo and redo are defined and added to the UndoManager object as an UndoableEdit.
A complete example
An example with Drawing Panel
Friday, December 14, 2007
Use threads to collect user input in forms
How to use threads in Java to get user input in forms?
You have to create a new form, pass the parameters (such as labels to be displayed on the form) and display the form. But how do we get the information input by the user to such forms. You can supply getters in the form implementation class. But the form instance has to first collect the information in the fields in order to access them. So how do we know when these values are obtained?
This is where we have to use threads. Create the form instance and invoke it in a new thread. Then issue a synchronized wait. Once the user inputs all information in the form, it can issue a synchronized notify in order to let the waiting object know that the information entered by the user are now available through the getters.
Eg.
Suppose you have a popupmenu which has an item to enter the information. So when the user clicks this form, the form has to appear. The user submits the information by clicking the OK button. Then the waiting object needs to be notified.
JMenuItem createNew=new JMenuItem("Enter info”);
menuItems.add(createNew);
createNew.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
CollectInfo infoCollector=new CollectInfo ();
Thread t=new Thread(infoCollector);
t.start();
}});
The CollectInfo class implements the Runnable interface.
class CollectInfo implements Runnable
{
WaitObject obj; //just an object to wait on.
public void run()
{
InfoForm form=new InfoForm (obj,"Enter Info”);
form.setEnabled(true);
synchronized(obj)
{
try {
obj.wait();
} catch (InterruptedException ex) {
System.err.println(ex.getMessage());
}
}
//use getters of InfoForm to access the input data
}
}
class InfoForm extends JFrame
{
WaitObject object;
InfoForm(WaitObject obj, String title)
{
this.object=obj;
setTitle(title);
JButton okBtn=new JButton("OK");
okBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
synchronized (object) {
object.notifyAll();//notifies the obj of CollectInfo class waiting.
done=true;
}
dispose();
}
});
}
…….
….
//other controls on the frame and the getter methods
}
Thursday, December 13, 2007
Using ANT for Java
Ant tutorials:
- http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-ant.html?page=1
- http://supportweb.cs.bham.ac.uk/docs/tutorials/docsystem/build/tutorials/ant/ant.html
- http://www.itpath.com/index.php?topic=user_guide
- http://ant.apache.org/manual/index.html
- Sample ant build file : http://www-flare.cs.ucl.ac.uk/staff/G.Roberts/2007/antbuildfile.html
Using JUNG 2 for dynamic graph drawing
The JUNG 2 alpha 2 is available at sourceforge.net. It has more features and more capabilities than previous versions and a better design. The samples along with the source provide a way to implement most of them. Notable features are:
· We can create our own vertex and edge types and can provide distinct colours and labels to them. The customized vertices and edges are supported everywhere(layout, visualizationviewer etc.)
· Zoom is available. Also there is Satellite view.
· Support for expand/collapse trees and vertices.(The collapsed vertex is a forest and is represented by a single vertex.).
· Tree layout, Spring layout, Balloon Layout etc. are supported. (We need tree layout to work with expand/collapse. If a node has more than one parent, expansion will not display the two edges.)
· VertexLabelRenderer:edu\uci\ics\jung\visualization\renderers package has VertexLabelRenderer which can be added to the renderer of the VisualizationViewer. It has the methods setPositioner and setPosition to position the vertices. The arguments to these methods are instances of classes available in the same package.
· Transformer:We can use the Transformer to apply any attribute(colour, stroke etc.) to the vertices and edges of the graph. Create a Transformer object and add this as a parameter to the setVertexStrokeTransformer method, for example, which is applied to the RenderContext of VisualizationViewer. This helps in providing different colour, stroke etc. to vertices and edges depending on their nature.
· Rendering vertices as icons:We can use the vertexIconTransformer and vertexIconShapeTransformer of VertexImageShaperDemo. The idea is that we should have an icon for the vertexIconTransformer and a shape of the icon obtained from FourPassImageShaper for the vertexIconShapeTransformer. Also a LayeredIcon is provided in order that we may use more than one icon at a time to render one vertex to indicate different details.
How to create a makefile and use it to compile multiple files together
Suppose we have 3 files insort.c, qsort.c, shellsort.c each implementing different types of sorts and we have a main program sortdriver.c which gives the option to choose one among these different types of sort and we have a header file sort.h which has declarations for all the 3 sort functions.
(Don’t worry, these has nothing to do with Makefile J )
Essentially, we have three .c files: insort.c, qsort.c, shellsort.c
Then we have a header file sort.h which has declarations of functions used by all the three .c files. And we have the sortdriver.c which contains the main which uses these functions.
The dependencies are:
shellsort.o depends on shellsort.c and sort.h
qsort.o depends on qsort.c and sort.h
insort.o depends on insort.c and sort.h
sortdriver.o depends on sortdriver.c and sort.h
and finally, the executable sorting depends on sortdriver.o, insort.o, qsort.o and shellsort.o
Makefile is the common name for makefiles.
In Makefile, all we need to specify is these dependencies and the respective commands for compilation. Type make to compile the whole module specified by make. If some other name is used for makefile, type make –f mymakefile to us the make utility.
So type vi Makefile
In the new file, type this for creating the executable sorting.
sorting: sortdriver.o insort.o qsort.o shellsort.o
cc sortdriver.o insort.o qsort.o shellsort.o –o sorting
sortdriver.o: sortdriver.c sort.h
cc –c sortdriver.c
insort.o: insort.c sort.h
cc –c insort.c
qsort.o: qsort.c sort.h
cc –c qsort.c
shellsort.o: shellsort.c sort.h
cc –c shellsort.c
#This symbol is used for comments
# Read the makefile from bottom to top to understand the dependencies
#The space in front of commands is a tab
Now if your makefile’s name is Makefile, type make to compile your whole program whenever you make changes to any of these files. Else, type make –f mymakefile if you makefile’s name is mymakefile.