Getting started with Label Layout in Java

To obtain a label layout, apply the Annealing Label Layout algorithm to a manager directly.
To apply the layout to a manager:
  1. Create a manager object ( IlvManager) and fill it with obstacles and labels. For instance, if you want to place labels at the links of a graph, create a grapher and fill it with nodes and links (the obstacles) and additionally with the labels that should be placed at the links. In this case, the labels should be added using addObject instead of addNode because the labels should not be nodes at the same time.
    Unlike with diagram component and style sheets, you are not restricted to using IlvGeneralLink, or IlvSDMCompositeLink. You can place labels at any link.
  2. Create an instance of the Annealing Label Layout algorithm.
  3. Declare a handle for the corresponding layout report. The layout report is an object in which the layout algorithm stores information about its behavior. For details, see The label layout report.
  4. Attach the manager to the layout instance.
    Here, it is assumed that the labels are subclasses of IlvLabel, IlvZoomableLabel or IlvText. If they are not, the label model can be extended as illustrated in Labels and obstacles in Java.
  5. For each label, set a label descriptor.
    The label descriptor describes the conditions for the placement of the label. For instance, if a label should be placed close to the source or destination node of a link, use an IlvAnnealingPolylineLabelDescriptor with the corresponding options. For details, see Label descriptors.
  6. Modify the default settings for the layout parameters, if needed.
  7. Call the performLayout method.
  8. Read and display information from the layout report.
  9. When the layout instance is no longer needed, detach the manager from the layout instance.
A complete example of these steps can be found at the location <installdir>jviews-diagrammer89/codefragments/labellayout
It contains the following Java code:
// the JViews Graphic Framework
import ilog.views.*;
import ilog.views.graphic.*;

// the JViews Label Layout Framework
import ilog.views.graphlayout.labellayout.*;

// the JViews Annealing Label Layout
import ilog.views.graphlayout.labellayout.annealing.*;

// the Java AWT package
import java.awt.*;

// the Swing package
import javax.swing.*;


/**
 * A very simple example for the use of a Layout Algorithm.
 */
public class LayoutSample2
{
  public static final void main(String[] arg)
  {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        // Create the manager instance (subclass of IlvManager). Since we want
        // to place link labels, we use a grapher in this example.
        IlvGrapher grapher = new IlvGrapher();
 
        // Create the manager view instance
        IlvManagerView view = new IlvManagerView(grapher);
        view.setBackground(Color.white);
 
        // An Swing Frame to display 
        JFrame frame = new JFrame("Label Layout Sample");
 
        // Put the manager view inside an AWT Frame and show it
        frame.getContentPane().add(view);
        frame.setSize(400, 400);
        frame.setVisible(true);

        // Fill the grapher with nodes and links
        IlvReliefRectangle node1 = new IlvReliefRectangle(
                                     new IlvRect(0f, 0f, 50f, 50f));
        IlvReliefRectangle node2 = new IlvReliefRectangle(
                                     new IlvRect(200f, 0f, 50f, 50f));
        IlvReliefRectangle node3 = new IlvReliefRectangle(
                                     new IlvRect(0f, 200f, 50f, 50f));
        IlvReliefRectangle node4 = new IlvReliefRectangle(
                                     new IlvRect(200f, 200f, 50f, 50f));
        grapher.addNode(node1, false);
        grapher.addNode(node2, false);
        grapher.addNode(node3, false);
        grapher.addNode(node4, false);

        // set some nice colors
        setNodeColors(node1, node2, node3, node4);

        IlvLinkImage link1 = new IlvLinkImage(node1, node2, true);
        IlvLinkImage link2 = new IlvLinkImage(node1, node3, true);
        IlvLinkImage link3 = new IlvLinkImage(node2, node4, true);
        IlvLinkImage link4 = new IlvLinkImage(node4, node3, true);
        IlvLinkImage link5 = new IlvLinkImage(node1, node4, true);

        grapher.addLink(link1, false);
        grapher.addLink(link2, false);
        grapher.addLink(link3, false);
        grapher.addLink(link4, false);
        grapher.addLink(link5, false);

        // set some nice colors
        setLinkColors(link1, link2, link3, link4, link5);

        // Add labels. Labels are neither "nodes" nor "links", hence add them 
        // as objects. Since we perform layout later, the initial position 
        // doesn’t play a role.

        IlvPoint p = new IlvPoint(0f,0f);
        IlvZoomableLabel label1 = new IlvZoomableLabel(p, "Label1");
        IlvZoomableLabel label2 = new IlvZoomableLabel(p, "Label2");
        IlvZoomableLabel label3 = new IlvZoomableLabel(p, "Label3");
        IlvZoomableLabel label4 = new IlvZoomableLabel(p, "Label4");
        IlvZoomableLabel label5 = new IlvZoomableLabel(p, "Start Label");
        IlvZoomableLabel label6 = new IlvZoomableLabel(p, "End Label");

        grapher.addObject(label1, false);
        grapher.addObject(label2, false);
        grapher.addObject(label3, false);
        grapher.addObject(label4, false);
        grapher.addObject(label5, false);
        grapher.addObject(label6, false);


        // Declare a handle for the layout instance
        IlvAnnealingLabelLayout layout = new IlvAnnealingLabelLayout();

        // Declare a handle for the layout report
        IlvLabelLayoutReport layoutReport = null;

        // Attach the manager to the layout instance
        layout.attach(grapher);

        // For each label, set a label descriptor that specifies: label1 
           //  belongs to link1, label2 belongs to link2, and so on. link5 has          
        // 2 labels.

        layout.setLabelDescriptor(
            label1,
            new IlvAnnealingPolylineLabelDescriptor(
                label1, link1, IlvAnnealingPolylineLabelDescriptor.CENTER));
        layout.setLabelDescriptor(
            label2,
            new IlvAnnealingPolylineLabelDescriptor(
                label2, link2, IlvAnnealingPolylineLabelDescriptor.CENTER));
        layout.setLabelDescriptor(
            label3,
            new IlvAnnealingPolylineLabelDescriptor(
                label3, link3, IlvAnnealingPolylineLabelDescriptor.CENTER));
        layout.setLabelDescriptor(
            label4,
            new IlvAnnealingPolylineLabelDescriptor(
                label4, link4, IlvAnnealingPolylineLabelDescriptor.CENTER));
        layout.setLabelDescriptor(
            label5,
            new IlvAnnealingPolylineLabelDescriptor(
                label5, link5, IlvAnnealingPolylineLabelDescriptor.START));
        layout.setLabelDescriptor(
            label6,
            new IlvAnnealingPolylineLabelDescriptor(
                label6, link5, IlvAnnealingPolylineLabelDescriptor.END));

        // Modify the layout parameters, if needed
        layout.setLabelOffset(10);
        layout.setObstacleOffset(5);

        // Perform the layout and get the layout report
        layoutReport = layout.performLayout();

        // Print information from the layout report (optional)
        System.out.println("layout done in " +
                       layoutReport.getLayoutTime() +
                       " millisec., code = " +
                       layoutReport.getCode());
   
        // Fit the graph in the window
        view.fitTransformerToContent();
        // Redraw the grapher
        grapher.reDraw();

        // Detach the grapher from the layout instance
        layout.detach();
      }
    });
  }  
The following figure shows the graph produced by the sample Java™ application.
Picture
showing the result of the sample application
Output from sample Java application