Text Box Example

    In this example, the MarvinBeans API is used to create a MTextBox containing formatted text. The image below shows the created MTextBox in a MSketchPane.

    MTextBox example

    The following paragraphs demonstrate the major parts of the MTextBoxExample.java code. The source of the created text box can also be downloaded from MTextBoxExample.mrv. To create a text box an empty Molecule should be created first. The Molecule has an MDocument attribute to which a MTextBox object can be set. If the MDocument object is null, it should be created first.

        Molecule mol = new Molecule();
        MDocument md = mol.getDocument();
        if (md == null) {
            md = new MDocument(mol);
        }
        MTextBox textBox = new MTextBox();
        md.addObject(textBox);

    The MTextDocument attribute contains the formatted text in the MTextBox. The MTextDocument can be accessed this way:

        textBox.getTextDocument();

    Creating Formatted Text

    The font of the formatted text is represented by a MFont object. The font family, font size, and font style should be set in the constructor. The following line of code will create a new "SansSerif" font with bold style and 12pt size:

        MFont boldMf = new MFont("SansSerif", MFont.BOLD, 12);

    The text's color and superscript/subscript information will be defined in MTextAttributes objects. The following piece of code will create a very simple MTextAttributes object (see the text "deuterium" in the picture above) with a blue text using the font defined in the previous step. The used 0 and 1 arguments are needed for the default values:

        MTextAttributes blueBoldMta = new MTextAttributes(0, 0, Color.blue, boldMf, 1, 0, 0);

    A bit more advanced use case with a superscript defined:

        MTextAttributes superScriptMta = new MTextAttributes(0, MTextAttributes.DEFAULT_SUPERSCRIPT_SUBLEVEL, Color.black, mf, MTextAttributes.DEFAULT_SUPERSCRIPT_SCALE, 0, MTextAttributes.DEFAULT_SUPERSCRIPT_DELTAY);

    Adding Formatted Text to the Text Box

    The formatted text should be added to the MTextDocument with the following method:

    `MTextDocument.append(java.lang.String, chemaxon.struc.graphics.MTextAttributes)`

    In the example we added the blue bold text "deuterium" to the MTextDocument with:

        textDocument.append(" deuterium", blueBoldMta);

    Setting the Coordinates and Rotation

    After the text is formatted and added to the MTextBox, we can set its coordinates with:

        textBox.setCorners(p1, p2);

    For transforming the coordinates a CTransform3D is needed. The following code creates an empty Ctransform3D then sets it to rotate the textbox around the Z-axis with 45 degrees. After that, the transformation center is set to be the center of the textbox and then the transformation is done on the textbox.

            CTransform3D ctrans=new CTransform3D();
            ctrans.setEuler(0, 0, 45);
            textBox.setTCenter(MRectangle.P_CENTER);
            textBox.transform(ctrans, 0, null);