Recently I had a small requirement for a project I am currently working at in my spare time: centralize a text inside a given rectangular area (if I already know the x, y, w, h dimensions of the rectangle).

The main idea was to use the context.measureText(text).width; method, as it returns the width of the specified text in pixels – exactly what I needed.

Eventually I’ve came up with the following code:

And I thought it was working perfectly:

Continue reading

Recently I’ve encountered a rather strange behaviour related to Java Web Start.

If, in your .jnlp file you are referencing some signed jars, sometimes the verification fails miserably with the following stacktrace:

If you are 100% sure that your jars are correctly signed, check if your JRE setup have “Keep temporary files on my computer” option enabled. That solved the problem for me.

So if you are on Windows: Control Panel -> Java and then:

Hope it helps.

In this article I am going to show you how to write a simple file monitor. So even though Java 7 comes with a low-level API to watch for file system changes (article here), fow now we will be using using the Commons IO library from the Apache Foundation, mainly the package.

The first step will be to define the location that we are going to monitor. For this I’ve created a temporary folder of my desktop, and defined a String constant pointing to that newly created location:

The next step will be to define a polling interval: how often we will “look” for file-system changes. The value is expressed in milliseconds:

Now we will have to build a File object out of the folder we are monitoring:

At this point Commons IO comes into picture. In order to make the system monitor actually work we will need at least one instance of the following: FileAlterationObserver, FilterAlterationMonitor and FileAlterationListenerAdaptor .

And then we will proceed to add the listener to the observer, add the observer to the monitor, and start the monitor:

After compiling & running the resulting code, every change I do in the folder that I monitor is being recorded:

The full sourcecode for the simple file monitor: Continue reading

One of my favorite online resources for programming challenges is called Project Euler . More than certain you’ve heard about it, otherwise what would be the reason to visit this page.

Problem 22 is a simple to medium challenge . In order to solve it you’ll need basic programming knowledge: reading a line from a file, apply some functions on a string, lambda functions and python list comprehension .

Here is the requirment:

Using names.txt (right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

And this was my “condensed” solution:

There’s no need for additional comments, the code is pretty straight-forward.