mutterings of a cynic

Friday, July 27, 2007

meaningless

While watching the news today I heard someone say, and I quote:


I was amazed last week when I saw it again for the first time


What the hell? Where do these people learn to talk? The Fisher-Price school for the linguistically challenged?


Wednesday, June 27, 2007

too long

Sometimes I do, say or think something that makes me realise I've been in Switzerland too long. Today I went one step further and actually wrote this sentence in an email.


Since it's an interface I'm not technically overriding anything, so the @Override annotation wouldn't if I added it compile.


Thankfully I caught it before sending the email out, but this really does go to show that perhaps I really have been here too long.


Tuesday, June 26, 2007

gratitude

Receiving praise is a very important aspect of social interactions. At work in particular, praise (when warranted) is extremely important. When you write some code that for one reason or another is impressive, it feels good when someone says "ooh, that's quite nice that is". That praise alone can be enough to sustain a person even if other aspects of their environment are less appealing for whatever reason.

A while ago I wrote a threading api which is quite large and (if I say so myself) quite useful. A few people have said nice things about it and that made me feel good. Today however I got a rather unusual, but rather fulfilling comment about it. I was actually outright thanked for having written it. That felt great.


Sunday, June 24, 2007

rounding errors in swing

I quite enjoyed blogging about layout managers the other day so I thought I'd follow up with another easy mistake to make in swing - positioning your components incorrectly due to rounding errors.

I recently wanted the equivalent of a GridLayout, but I wanted to be able to set the ratios of the grid sizes myself rather than have them all equal.

The required result is pictured below using the LayoutManager configured to lay the components out vertically using the ratios specified in their names.



As you can see here, the buttons are laid out such that all the available space (within the red border) is completely used up.

Naively, you would think that the code to produce that result looks like this (stripped down for easier reading):


float[] ratios = ...;
Component[] cc = parent.getComponents();

int x = insets.left;
int y = insets.top;
int tw = [avail width minus insets]
int th = [avail height minus insets]

for (int i = 0; i < cc.length; i++) {
  cc[i].setLocation(x, y);
  int h = (int) (th * ratios[i]);
  cc[i].setSize(tw, h);
  y += h;
}


However if you do that, the result looks like this:



Note that the bottom button is a good 3 pixels away from the border. Problem here is that we have a cumulative rounding error. Don't think that the solution is to round the calculation of each unit height either, if you do that, you'll end up using more vertical space than available.



So the solution is to not store the individual ratios at all, but rather store the cumulative ratios in an array that is 1 longer than the component array. For instance, rather than have an array like this [0.25, 0.5, 0.25] to lay out 3 components, use an array like this [0, 0.25, 0.75, 1] (make sure you manually clamp the last value to 1 to avoid another possible rounding error).

Here's some code:


float[] cumulativeRatios = ...;
int[] ys = new int[cumulativeRatios.length];

int tw = [avail width minus insets]
int th = [avail height minus insets]

for (int i = 0; i < cumulativeRatios.length; i++) {
  ys[i] = Math.round(th * cumulativeRatios[i]);
}

Component[] cc = parent.getComponents();
for (int i = 0; i < cc.length; i++) {
  cc[i].setLocation(insets.left, insets.top + ys[i]);
  cc[i].setSize(tw, ys[i + 1] - ys[i]);
}


Now you've spread the rounding errors over the available space in the parent and your components are laid out edge to edge - much nicer!

Incidentally, GridLayout doesn't use this mechanism of avoiding rounding errors. Here's a picture of the same test code using GridLayout with 5 rows and 1 column. This isn't necessarily wrong, it just might not be ideal in all circumstances.



I won't post the full code to the layout manager (unless someone asks for it), but rather leave it as an exercise for the reader.

As before, questions, comments and critique welcome.

Labels: , ,


Saturday, June 23, 2007

something new

I love learning new things. Recently I updated firefox on my various machines and it comes with a built in spell checker which I actually find very useful (since my speling is terrible). So, today I wrote an email and used the word schlep and it didn't give it a squiggly underline that would indicate a misspelt word. I found that amusing because my instant reaction was that they don't use a real dictionary, but rather a combination of real words and commonly used slang. I was however a little curious so I checked on dictionary.com and to my surprise, the word actually exists.


schlep /ʃlɛp/ Pronunciation Key - Show Spelled Pronunciation[shlep] Pronunciation Key - Show IPA Pronunciation verb, schlepped, schlep·ping, noun Slang.
–verb (used with object)
1. to carry; lug: to schlep an umbrella on a sunny day.
–verb (used without object)
2. to move slowly, awkwardly, or tediously: We schlepped from store to store all day.
–noun
3. Also, schlepper. someone or something that is tedious, slow, or awkward; drag.
Also, schlepp, shlep, shlepp.


Friday, June 22, 2007

know your layout manager

I often notice that people get really confused with the way java lays out components. Experience also shows that people struggle with what the sizing methods on JComponents really do. More than once I've seen code that does this kind of thing:

 JComponent c = ...;
 Dimension d = new Dimension(...);
 c.setMinimumSize(d);
 c.setMaximumSize(d);
 c.setPreferredSize(d);
 c.setSize(d);

Clearly that's insane, so I thought I might try to make people aware of a few little tricks to help size your components perfectly.

Here's a couple of points to remember:

  • The preferred size of a JComponent always tells you how big it wants to be. Mostly that size is sensible and it should be used.

  • If you set a preferred size of a component it is permanently altered. Stop and think before you do this. If you alter the preferred size of a combo box that has an item "Cat" (because you think it was too tall) like this:


     Dimension d = combo.getPreferredSize();
     d.height = 6;
     combo.setPreferredSize(d);


    Then when someone adds an element "Catamaran" you'll find your preferred width is now too small.

  • Know your layout manager! A LayoutManager will size a component in whichever way it feels is correct. If a LayoutManager ignores minimum size then don't get angry with your custom component because you set a minimum size and it's being ignored.

  • Don't be afraid to nest panels. Using layout gaps, borders and glue can get you quite far but for even the most basic setup of components nesting panels with individual layouts will take you to the finish line.


Here's a simple example illustrating a couple of those points.




 JPanel p = new JPanel(new BorderLayout());
 JScrollPane scroller = new JScrollPane(VERTICAL_SCROLLBAR_NEVER,  HORIZONTAL_SCROLLBAR_ALWAYS);
 scroller.setBorder(null);
 scroller.setViewportView(new JTextArea("My little scrolling thing "));
 p.add(scroller);


This is a panel containing a scrollpane (without its border cause it mucks up my example later on). If I want to add a widget of some sort next to the scrollbar then it'll adversely effect my size.



This was done by moving the scroll bar from out of its JScrollPane and into a custom panel next to another component.


 JPanel scrollBarPanel = new JPanel(new BorderLayout());
 JComboBox combo = new JComboBox(new String[] { "Some options" });
 scrollBarPanel.add(combo, BorderLayout.WEST);
 scrollBarPanel.add(scroller.getHorizontalScrollBar(), BorderLayout.CENTER);
 p.add(scrollBarPanel, BorderLayout.SOUTH);


Notice in the code that I'm adding the combo to the west side of a border layout. This means that I know that the combo will always get its preferred size and therefore look correct. The scroll bar on the other hand ends up in the center of the BorderLayout which means it gets all of the rest of the space.

Problem here is that the combo's preferred size is taller than the scroll bars and that makes the scroll bar look ugly!



Since I want to retain the correct width, my solution is mostly correct, but I just want to adjust the height of the combo, but retain its relative calculation of its preferred size (which means I don't want to just set its preferred size).

The solution I'm getting at here is that you should know your layout manager and not bother tweaking your components. The BorderLayout will have its minimum height set to the largest preferred height. That means that all we need to do it ensure that the largest preferred height in the panel is the scroll bar and not the combo.



The result, pictured above, was produced by embedding the combo into a panel with its preferred size calculation always clamping the height to 0. By that I mean literally creating a class called ZeroHeightPanel that extends JPanel and override a single method as follows:


@Override
public Dimension getPreferredSize() {
 final Dimension d = super.getPreferredSize();
 d.height = 0;
 return d;
}

This will now let your layout manager get preferred sizes of all the components it's laying out without having had to alter any settings on any of the components and without having to override any methods on the components. (Obviously the behaviour of the panel is altered, but my point is that if you get your JComponent from elsewhere in the API you won't be able to override it, but rather only embed it into something that you have control over.) Note that we are now laying out components following the composition over inheritance described in Item 14 of Josh Bloch's Effective Java. I believe that the rule can easily be applied to how UIs are laid out since we can easily compose a panel with altered sizing behaviour.

I'm not saying that this is how you should handle all of your sizing issues, but I just want to point out that there are other solutions that aren't necessarily obvious. If you know your layout managers and know what rules they apply to laying components out then you should be able to produce your desired behaviour without giving yourself a headache.

As a last point, if a layout manager doesn't do what you want it to do, don't be afraid of writing one. They are easy to write, but make sure that you document what rules it's using to size its component(s).

Questions and critique are welcome.

Labels: , , ,


Thursday, June 14, 2007

interface on the fly

On rahel's suggestion I thought I should post something really useful that you can do with generics. In java you can map most things by just adding another interface but sometimes you really don't want to define a crappy interface like:


interface DisposableComponent {
  void dispose();
  Component getComponent();
}


So here's something that not everyone knows about generics:


interface Flappable {
  void flap();
}

interface Flippable {
  void flip();
}

class FlipFlapper {
  <T extends Flappable & Flippable> void flipAndFlap(T ff) {
    ff.flip();
    ff.flap();
  }
}

Labels: ,


Tuesday, May 15, 2007

swiss courtesy

In general I don't find the swiss the friendliest bunch. Not withstanding, there are plenty of friendly swiss people out there - I mean no offense by this statement, it's merely an observation.

There are some things however that I really like about their little courtesies. People greet each other at the doctor's surgery for instance. I like that. With the english being so reserved, it just wouldn't happen there.

By far the thing I like the most however, is the fact that there's more than one way to both greet and say goodbye to a group of people; furthermore, it's not unusual to do so. I just went to the corner shop to buy some beer and there was a young woman in the small shop with me. After my sale was finished it was perfectly natural to say goodbye collectively to both the shop assistant and the shopper.

When I leave switzerland it'll be the little things I miss.


Saturday, May 12, 2007

eurovision

I voted.

I've never voted for anything on television before, but voting for UKs awesome entry in eurovision this year was well worth my 70rp. Since bbc prime have decided that eurovision doesn't fit their demographic, it took quite some effort to both watch the contest while listening to terry (a eurovision staple).

Fortunately however the time delay created by watching swiss tv and listening to internet radio meant we could appreciate both the swiss commentators cynical and terry's sardonic comments.

It's waiting time now - the half time show is on. Britain are predicted to come 24th - that's flat last. Well at least they did better than DJ Bobo.


Thursday, May 10, 2007

how do they do it?

On the bus this morning I was thinking about deaf children and how they grow up. If a child is profoundly deaf, how do they learn to read? How do they mentally pronounce the words that they come to understand?

A quick google when I arrived at work has taught me that hearing aid and cockler cochlear implant technology is much better than I gave it credit for, and according to this, deaf children should be taught to understand and speak before learning things like written word, lip reading or sign language.

This quick lesson has made me happier in the knowledge that your child being born deaf isn't as tragic as it first may seem.


Saturday, May 05, 2007

cultural laws

In england you're not allowed to talk to people you don't know, even if they have a t-shirt on it that says "please talk to me". There needs to be some sort of unspoken request allow before any audible communication is permitted.

For instance, if you're wearing a cap that has the logo of a sports team on it, someone may want to tell you that they support the team too, so they might subtly nod. Your responses can be one of many:


  • A nod combined with a half smirk, half smile smile combined with a short exhalation through the nose means "I see you support the team. While I appreciate that, I don't support them, I just like the cap. Don't talk to me"

  • A nod and smile means "I support them too, but don't talk to me"

  • A nod, grunt and smile means "I support them too. If you want to talk, you're now welcome" (The grunt simply initialises audible communication).


This incredibly english subtlety combined with understated gestures defines the culture, but is wholly and sorely misunderstood, nay, unnoticed, by others. Wearing a sports cap in the states, for instance, is a license to have people talk crap to at you.

When I move there I'm going to have to be forgiving. Bless their souls, they just don't get it.


Thursday, May 03, 2007

coincidence

Having found out that apple have promised to support the final 802.11n spec with their current n devices, I was just looking to see if the mac minis support the n protocol.

I was disappointed to find out that they do not, however I did like the number of search results.


Wednesday, May 02, 2007

shhhhhhhhh

So I finally bought some noise cancelling headphones and decided to write a non-audiophile's review of them.



The first time I put them on I was really impressed. I was standing in a quiet apartment and only having put them did I notice that there used to be a sound of the air-conditioning and faint background road noise. The nullification of the sound was quite amazing.

I had heard that the bose quiet comfort headphones were substantially better than other companies' offerings, so while I was in the states I put it to the test. I went into a sony store in boston and tried out their headphones and I also tried out all of the headphones on sale at logan airport. I had heard from a friend of a friend that the best way to save $100 on noise cancelling headphones was to just buy the bose ones first. Having tried at least 5 others I have to agree. The bose phones may well be costly, but if you want it to work, then why bother spending half the amount on something substandard?

So, back to the sound itself. The way the noise cancelling works seems to be quite interesting. Having played around with them a while I have come to the conclusion that they only attempt to cancel repetitive noise. This means that you can have a conversation with someone standing next to you without having to take them off. That itself leads to an interesting effect on a plane; because the noise cancellation is so good, you normalise the volume of your own voice according to the volume of the person you're talking to minus the background noise which means that when you speak they can't hear you because you're talking too quietly.

The best effect from the headphones is when the repetitive background noise itself is loud, so on an airplane the effect is unmatched. I was watching a video with my wife on her ipod on the plane using an audio splitter. She was using in-ear buds while I was using the headphones. I barely needed any volume at all to be able to clearly hear the audio. We switched for a while and it was apparent that the clarity of the headphones was great. Using the earbuds you heard less, less clearly with more background noise and they were less comfortable.

Comfort brings me to another point. They are called quitecomfort for a reason. I wore them for 7 hours without discomfort. They also excel on their packaging. They come in a light but solid case that's moulded for the headphoens and all their accessories, including battery charger, cables and aircraft adapter. Additionally, the moulded portion of the case set aside for the included accessories is only attached with velcro which means it can be removed if you want to make room for your mp3 player. It's nicely styled and it's obvious that they really thought about the packaging.

So, are they worth their price tag? They cost $350 so they have a lot to make up for. Earlier I mentioned that they cancel repetitive background noise extremely well - this is very very true. To get the greatest effect just wear them for 5 minutes then take them off and you're aware of sounds that you didn't know were there. The fact that you didn't know that they were there is my point here. If you didn't know they were there, why did you need it cancelling? The brain already does an awesome job of sound normalisation and cancellation by itself... I'm in 2 minds about whether they justify their price tag. For someone like me, they definitely don't, although at the same I don't feel like I wasted my money. If you struggle with noise on a plane, you can't go wrong, if you don't then it's an expensive toy. Justify it whichever way you choose.

The best way to get the effect without shelling out 350 bucks (unless you break them) is to just borrow mine. I'd be more than happy to lend them to you.

Labels: ,


Thursday, April 19, 2007

ignorant and insular

I'm booking a car for next week when I'm in the US. While filling out the form they tell me that it's crucial for me to give them a phone number and there's an option to enter a US phone number or a european one. I clicked on european, only to be presented with this:



Poor old switzerland, but I'm really glad that they included mexico and canada.


Thursday, April 12, 2007

the plunge

So I've finally taken the plunge, albeit by proxy; there is now a mac in the house. I was quite intrigued about the setup process on the mac since I've never used, let alone set one up before, so we sat down together for the grand opening.



Just as we turned on the mac and clicked the first setup option I got a call for PC help from my brother with in law who recently bought a new laptop running XP. Thankfully windows has really nice remote assistance options which allowed me to take control and sort the problem out. The problem was that when you get a new machine running windows it never just has windows on it. It has windows plus some custom sound management drivers, plus custom sound software, plus custom cd burning software, not forgetting the custom wireless, network and bluetooth configuration applications, also with the sponsored chat client installed, with the appropriate ads and of course the 5 different links to trouble shooting and help documents and also including custom drivers for the mouse, trackpad or touchpad, plus some floaty desktop widget to control some of the custom stuff, plus a custom firewall, plus some spyware and adware controls, plus the laptop maintenance utilities, display configuration utils and finally of course a custom power management interface. The problem with all of that is that absolutely none of it is needed, because they all come with windows. (Additionally, in the case of my brother in law's laptop, there was so much crap on there that the apps actually conflicted with each other). In fact, not only do they come with windows, but the guys at microsoft spent much more time thinking about how it should look and work, the guys that wrote the software were better and they all got paid more for it. So why oh why do I need to spend hours of my time removing a load of crap from a computer that never needed to be there in the first place, then patching up the holes? Because microsoft don't sell the hardware.

By the time I had finished with my mass uninstalling session support request to make the brand new laptop bearable, not only had my absolutely non-tech savvy wife who had never used a mac before finished the mac setup and created her user account, she had also connected to the wireless network, transferred her contacts from her phone to her mac address book with bluetooth, synched iCal with her google calendar, setup up her desktop preferences (screensaver, dock prefs, region stuff etc) and she also had enough time to spare to take a few snaps with the built in camera and play with all of the apps that came with the machine while all the time squealing with pleasure at the nice little features that she experimented with and they just worked. Yesterday was a good day for the mac and a bad one for the pc.

Mac vs PC, 1:0


Wednesday, April 11, 2007

retro art

This is really pretty cool - donkey kong mural made out of post-it notes.



The UCSC engineering faculty had ten people and 14,000 post-it notes lying around, with five hours to kill. What did they do? What ANYONE would/should have in the same situation: they made an enormous Donkey Kong mural that must be pushing 40-feet tall.


Monday, April 02, 2007

comparative perceptions

As you will soon be aware (assuming you read the next post) or are already aware (if you've already read it, though I find that unlikely) I have a new phone.

What I hadn't previously mentioned is that my wife decided to get a new phone on the same day. She is much less stalwart than I am about her choice in phones so has a greater choice at her disposal. She settled on the KRZR K1. Although I would never have chosen it for myself, I quite like it. It's small, but heavy which makes it feel metallic and solid, rather than overweight and cumbersome.

When I was looking at it on the table next to my old 6310i I realised that in actual fact, aside from being narrower, it is the same height and (surprisingly) the same thickness. The thing was though, it looked quite a lot smaller. This got me onto how we look at devices and how we determine whether we think they are large or small.



Courtesy of sizeasy, the above image shows the top down view of the comparative sizes of the 3 phones. Since within 0.5mm they are the same height, I chose this as the most representative view.

I came to the conclusion that expectations entirely define perception of size. I don't expect phones to be thin, so my phone, although it's the widest by far, to me seems small. Dimensionally the nokia is indeed closest to a standard house brick which has the relative dimensions 100 x 45.3 x 28.1. The KRZR to me seems narrow but thick. Since it is only a whisker narrower than the nokia I presume this perception comes from the fact that it's thinner, but at the same time my perception of it being thick comes from the fact that I see it as a small phone, but it's thicker than a phone that I consider to be small-ish.

I think I've failed miserable in translating my thoughts into my blog, so I'm going to quit before I can get any further behind.

Labels: , , ,


here comes a new challenger!


Is there any hope for me? LG and Motorola would have to improve their software dramatically, ericsson would need to make slight design improvements and slight software improvements and nokia need to either use the right software on the nice phones or improve their designs. In short, no - there is no hope.


It turns out that I was wrong. There is hope, and it comes from a mobile phone manufacturer that I had completely forgotten about - Samsung.




I went into the shop on Friday to whinge a little more about the lack of phones that interest me and noticed a slim black samsung on the rack. I asked the guy behind the counter if I could play with a real one. I played with it for about 15 minutes and I found it ok. It was presented well and didn't have some of the major deficiencies that I found with other software. Also style wise the phone is pretty, it's slim, has a nice screen and is quad band.

After getting home I checked out some reviews and they were positive. I went into another store to get a better price and was warned that it's no ericsson or nokia with its battery life which I found frustrating, but not too off putting. So long story short, I decided to buy it and see.

Having played with it for 3 days now I have some opinions. Although I could now go on for hours about all the things I dislike about it, I have to attribute that to my nature. The truth is that deep down I do like it even though there are some things that I need to get used to.

Labels: , ,


Wednesday, March 28, 2007

irony

My passport is damaged so I sent an application for a new one along with the passport itself to the embassy last week. Today I got a letter from the embassy saying telling me that they cannot accept my passport as proof of id because it's damaged..


foley

I was just made aware of a rather amusing video clip (found here) of smashing vegetables to simulate the sounds of video game orientated decapitations, amputations and various other forms of visceral mutilations.

It's interesting in a gross kinda way.


From kotaku

Labels: , ,