Saturday, May 24, 2008

Photo Collage

The other day I was surfing the new on dzone, and came across a link to the following photoshop tutorial. I though this was cool, and since I don't have photoshop, I was going to try and create this via gimp, which I have done with many photoshop tutorials. The more I looked at this, the more I though about writing an application to automate this task, and just to see if I could do it. I wrote the application using java, and their is a screenshot of it in action below.

The application supports using any background image you like, adding extra polaroid like photos, adding drop shadows to the photos, and moving and rotating the photos. You can also save your newly created work of art as well, you can see the lovely image of my son fixing his bike that I created below.

I will probably provide more details of what was involved to create the application in another post, or I may update this post. There are still many things that could be done to make it more functional, but hey it's not bad for a few hours of work. It probably would have taken me more time to recreate this image in Photoshop or Gimp, but hey I'm no artist. You can try the application out for yourself by pressing the orange button below. Once the application opens up, click select photo to load a photo of your choice, don't worry the screen will still look black after doing this, then click add photo to add a polaroid. You should now see your background image showing through the photo, add a few more and have fun. NOTE: You will need to have java 5 or greater installed, if you do not have it you can click on the 'Get Java Software' button below.

Launch Button

GetJava Download Button

Wednesday, May 21, 2008

Dynamic Jasper Report Using Crosstabs

Today I was doing some searching for creating dynamic jasper reports, i.e. not have my columns defined in my report template. This article is a well known reference that discuss modifying the xml template at run time using velocity. This seemed like overkill for what I wanted to accomplish. So I continued searching and found references to using crosstabs to accomplish what I needed.

Unfortunately I was unable to find any examples on generating a report using crosstabs. The Jasper Reports example didn't apply to my scenario, and I feel it didn't apply to the majority of users needs. The forums for ireport and Jasper Reports contain many entries with developers struggling to get crosstabs working. So below is a simple example of how to accomplish this using a JRBeanCollectionDataSource.

The first thing I needed to do was create a simple bean to use in the datasource. I created a simple class called MyBean.

public class MyBean { String header; String row; String value;

public MyBean(String header, String row,String value){
this.row = row;
this.header = header;

this.value = value;

Next I created some simple beans and added them to my datasource. And generated the report based upon my template.

ArrayList beans = new ArrayList();
MyBean a1 = new MyBean("header1","row1","VA
MyBean a2 = new MyBean("header2","row1","VALUE2");
MyBean a3 = new MyBean("header3","row1","VALUE3");

MyBean b1 = new MyBean("header1","row2","VALUE1");
MyBean b2 = new MyBean("header2","row2","VALU
MyBean b3 = new MyBean("header3","row2","VALUE3");

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(beans);

So now I have my datasource defined. For each row that I generate I can have as many columns as I want giving each column a unique name. To add a new row, I simply create new beans with a new row name. And for each column I can pass in a value.

To create the report template, add three fields, as defined in our bean above.

Then we need to create a crosstab. When creating make a row group using the $F{row} value for the row group bucket expression, and the $F{header} for the header group bucket expression. Lastly set up the measure as follows.

And when you generate your report you should have something like the following. This could use some cleaning up but gives the general idea, and hey, it's more information than I was able to find.

As requested here is the jrxml file for the crosstab report. In addition I have started to experiment with using the jasper reports api to create a report from a template or from scratch. This works well for some other scenarios that I have come across. The one nice thing about crosstabs though, is that it handles the wrapping of columns to new pages when they exceed the page width. Another option is Dynamic Jasper, I briefly looked at this, and it looks pretty nice, but using the api directly was a better solution at the time. As with any report, I don't think there is one magic bullet to solve all problems, and each problem must be looked at uniquely to determine the best approach.