/** * Finds optimized <tt>Label</tt> representations for the registered * <tt>LabelDisplayElement</tt>s. * * <p> * * @param g */ public void optimize(Graphics2D g) throws Exception { choices.clear(); double scale = mapView.getScale(g); GeoTransform projection = mapView.getProjection(); // used to signal the LabelDisplayElement that it should // not create Labels itself (in any case) Label[] dummyLabels = new Label[0]; // collect LabelChoices for all LabelDisplayElements for (int i = 0; i < displayElements.size(); i++) { LabelDisplayElement element = (LabelDisplayElement) displayElements.get(i); if (!element.doesScaleConstraintApply(scale)) continue; element.setLabels(dummyLabels); choices.addAll(LabelChoiceFactory.createLabelChoices(element, g, projection)); } buildCollisionMatrix(); // do the magic try { anneal(); } catch (Exception e) { e.printStackTrace(); } // sets the optimized labels to the LabelDisplayElements, so // they are considered when the DisplayElements are painted the next // time for (int i = 0; i < choices.size(); i++) { LabelChoice choice = (LabelChoice) choices.get(i); choice.getElement().addLabel(choice.getSelectedLabel()); } }