Example usage for javax.swing JPanel getFontMetrics

List of usage examples for javax.swing JPanel getFontMetrics

Introduction

In this page you can find the example usage for javax.swing JPanel getFontMetrics.

Prototype

public FontMetrics getFontMetrics(Font font) 

Source Link

Document

Gets the FontMetrics for the specified Font.

Usage

From source file:SWTUtils.java

/**
 * Create an awt font by converting as much information
 * as possible from the provided swt <code>FontData</code>.
 * <p>Generally speaking, given a font size, an swt font will
 * display differently on the screen than the corresponding awt
 * one. Because the SWT toolkit use native graphical ressources whenever
 * it is possible, this fact is platform dependent. To address
 * this issue, it is possible to enforce the method to return
 * an awt font with the same height as the swt one.
 *
 * @param device The swt device being drawn on (display or gc device).
 * @param fontData The swt font to convert.
 * @param ensureSameSize A boolean used to enforce the same size
 * (in pixels) between the swt font and the newly created awt font.
 * @return An awt font converted from the provided swt font.
 *///from   w w  w .j a  va 2  s . c  o  m
public static java.awt.Font toAwtFont(Device device, FontData fontData, boolean ensureSameSize) {
    int height = (int) Math.round(fontData.getHeight() * device.getDPI().y / 72.0);
    // hack to ensure the newly created awt fonts will be rendered with the
    // same height as the swt one
    if (ensureSameSize) {
        GC tmpGC = new GC(device);
        Font tmpFont = new Font(device, fontData);
        tmpGC.setFont(tmpFont);
        JPanel DUMMY_PANEL = new JPanel();
        java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(), fontData.getStyle(), height);
        if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) {
                height--;
                tmpAwtFont = new java.awt.Font(fontData.getName(), fontData.getStyle(), height);
            }
        } else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) {
                height++;
                tmpAwtFont = new java.awt.Font(fontData.getName(), fontData.getStyle(), height);
            }
        }
        tmpFont.dispose();
        tmpGC.dispose();
    }
    return new java.awt.Font(fontData.getName(), fontData.getStyle(), height);
}

From source file:org.ut.biolab.medsavant.client.view.genetics.variantinfo.OtherIndividualsGeneSubInspector.java

@Override
protected JPanel getIndividualSummaryPanel(String dnaID) {
    JPanel outerPanel = new JPanel();
    outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));

    //dnaIDVariantMap.get(DNAId) -- all variants within gene corresponding to individual
    Set<VariantRecord> variantRecords = getVariantRecords(dnaID);

    Map<Long, Set<VariantRecord>> positionVariantMap = new TreeMap<Long, Set<VariantRecord>>();
    for (VariantRecord variantRecord : variantRecords) {
        //Get all variants at that overlap at that position, regardless of individual.
        Set<VariantRecord> variantsAtPosition = positionVariantMap.get(variantRecord.getStartPosition());
        if (variantsAtPosition == null) {
            variantsAtPosition = new HashSet<VariantRecord>();
        }/*ww w.j a  v a2  s  . co  m*/
        variantsAtPosition.add(variantRecord);
        positionVariantMap.put(variantRecord.getStartPosition(), variantsAtPosition);
    }

    for (Map.Entry<Long, Set<VariantRecord>> e : positionVariantMap.entrySet()) {
        Long pos = e.getKey();
        Set<VariantRecord> variantsAtPosition = e.getValue();
        for (final VariantRecord variantRecord : variantsAtPosition) {
            final JPanel rowContainer = new JPanel();
            rowContainer.setLayout(new BoxLayout(rowContainer, BoxLayout.Y_AXIS));
            final JPanel row = new JPanel();
            row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
            row.setBorder(new EmptyBorder(0, MIDDLE_LEVEL_INDENT, 0, 0));
            final JLabel showDetailsButton = ViewUtil
                    .createIconButton(IconFactory.getInstance().getIcon(IconFactory.StandardIcon.EXPAND));
            String posStr = NumberFormat.getNumberInstance().format(pos);
            JLabel rowTitle = new JLabel(
                    " " + variantRecord.getZygosity().name() + " " + variantRecord.getType() + " @ " + posStr);
            row.add(showDetailsButton);
            row.add(rowTitle);
            row.add(Box.createHorizontalGlue());
            rowContainer.add(row);
            outerPanel.add(rowContainer);

            showDetailsButton.addMouseListener(new MouseAdapter() {
                private boolean expanded = true;

                @Override
                public void mousePressed(MouseEvent me) {
                    if (expanded) {
                        showDetailsButton
                                .setIcon(IconFactory.getInstance().getIcon(IconFactory.StandardIcon.COLLAPSE));
                        int last_level_indent = MIDDLE_LEVEL_INDENT
                                + IconFactory.getInstance().getIcon(IconFactory.StandardIcon.EXPAND)
                                        .getIconWidth()
                                + rowContainer.getFontMetrics(rowContainer.getFont()).charWidth(' ');
                        JPanel p = new JPanel();
                        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
                        p.setBorder(new EmptyBorder(0, last_level_indent, 0, 0));
                        JPanel textPanel = new JPanel();
                        textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS));
                        textPanel.add(new JLabel("Ref: " + variantRecord.getRef()));
                        textPanel.add(Box.createHorizontalGlue());
                        p.add(textPanel);

                        textPanel = new JPanel();
                        textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS));
                        textPanel.add(new JLabel("Alt: " + variantRecord.getAlt()));
                        textPanel.add(Box.createHorizontalGlue());
                        p.add(textPanel);

                        rowContainer.add(p);
                    } else {
                        showDetailsButton
                                .setIcon(IconFactory.getInstance().getIcon(IconFactory.StandardIcon.EXPAND));
                        rowContainer.removeAll();
                        rowContainer.add(row);
                    }
                    rowContainer.revalidate();
                    rowContainer.repaint();
                    expanded = !expanded;
                }
            });

        }
    }
    return outerPanel;
}