use the same positioning algorithm as in awt version