Example usage for java.awt Font deriveFont

List of usage examples for java.awt Font deriveFont

Introduction

In this page you can find the example usage for java.awt Font deriveFont.

Prototype

public Font deriveFont(Map<? extends Attribute, ?> attributes) 

Source Link

Document

Creates a new Font object by replicating the current Font object and applying a new set of font attributes to it.

Usage

From source file:com.haulmont.cuba.desktop.gui.components.SwingXTableSettings.java

protected void loadFontPreferences(Element element) {
    // load font preferences
    String fontFamily = element.attributeValue("fontFamily");
    String fontSize = element.attributeValue("fontSize");
    String fontStyle = element.attributeValue("fontStyle");
    String fontUnderline = element.attributeValue("fontUnderline");
    if (!StringUtils.isBlank(fontFamily) && !StringUtils.isBlank(fontSize)
            && !StringUtils.isBlank(fontUnderline) && !StringUtils.isBlank(fontStyle)) {

        try {//from ww w. ja v a 2  s  .c  om
            int size = Integer.parseInt(fontSize);
            int style = Integer.parseInt(fontStyle);

            String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
                    .getAvailableFontFamilyNames();
            int fontIndex = Arrays.asList(availableFonts).indexOf(fontFamily);
            if (fontIndex < 0) {
                log.debug("Unsupported font family, font settings not loaded");
                return;
            }

            Configuration configuration = AppBeans.get(Configuration.NAME);
            DesktopConfig desktopConfig = configuration.getConfig(DesktopConfig.class);
            int sizeIndex = desktopConfig.getAvailableFontSizes().indexOf(size);

            if (sizeIndex < 0) {
                log.debug("Unsupported font size, font settings not loaded");
                return;
            }

            Boolean underline = BooleanUtils.toBooleanObject(fontUnderline);

            @SuppressWarnings("MagicConstant")
            Font font = new Font(fontFamily, style, size);
            if (underline != null && Boolean.TRUE.equals(underline)) {
                Map<TextAttribute, Integer> attributes = new HashMap<>();
                attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
                font = font.deriveFont(attributes);
            }
            table.setFont(font);
        } catch (NumberFormatException ex) {
            log.debug("Broken font definition in user setting");
        }
    }
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.legend.SmartLegendTitle.java

private Block createFlankedShapeLegendItem(FlankedShapeLegendItem item) {
    BlockContainer result = null;//from  ww w  .java 2s  .  com
    LegendGraphic lg = new CustomLegendGraphic(item.getShape(), item.getFillPaint());
    lg.setFillPaintTransformer(item.getFillPaintTransformer());
    lg.setShapeFilled(item.isShapeFilled());
    lg.setLine(item.getLine());
    lg.setLineStroke(item.getLineStroke());
    lg.setLinePaint(item.getLinePaint());
    lg.setLineVisible(item.isLineVisible());
    lg.setShapeVisible(item.isShapeVisible());
    lg.setShapeOutlineVisible(item.isShapeOutlineVisible());
    lg.setOutlinePaint(item.getOutlinePaint());
    lg.setOutlineStroke(item.getOutlineStroke());
    lg.setPadding(this.getLegendItemGraphicPadding());

    LegendItemBlockContainer legendItem = new LegendItemBlockContainer(new BorderArrangement(),
            item.getDataset(), item.getSeriesKey());
    Font textFont = item.getLabelFont();
    if (textFont == null) {
        textFont = getItemFont();
    }
    Paint textPaint = item.getLabelPaint();
    if (textPaint == null) {
        textPaint = getItemPaint();
    }

    ColoredBlockContainer graphicsContainer = new ColoredBlockContainer(new Color(0, 0, 0, 0),
            new BorderArrangement());

    LabelBlock labelBlock;
    Font smallerTextFont = textFont.deriveFont(textFont.getSize() * .8f);
    Font labelTextFont = textFont;

    labelBlock = new LabelBlock(item.getLeftShapeLabel(), smallerTextFont, textPaint);
    graphicsContainer.add(labelBlock, RectangleEdge.LEFT);

    graphicsContainer.add(lg, null);

    labelBlock = new LabelBlock(item.getRightShapeLabel(), smallerTextFont, textPaint);
    graphicsContainer.add(labelBlock, RectangleEdge.RIGHT);

    legendItem.add(graphicsContainer, getLegendItemGraphicEdge());

    labelBlock = new LabelBlock(item.getLabel(), labelTextFont, textPaint);
    labelBlock.setPadding(getItemLabelPadding());
    legendItem.add(labelBlock);
    legendItem.setToolTipText(item.getToolTipText());
    legendItem.setURLText(item.getURLText());

    result = new BlockContainer(new CenterArrangement());
    result.add(legendItem);

    return result;
}

From source file:org.openmicroscopy.shoola.agents.metadata.rnd.GraphicsPane.java

/** Initializes the components. */
private void initComponents() {
    IconManager icons = IconManager.getInstance();
    viewedBy = new JXTaskPane();
    viewedBy.setCollapsed(true);//from   w  w w  .  ja  v  a  2s . co  m
    viewedBy.setVisible(false);
    Font font = viewedBy.getFont();
    viewedBy.setFont(font.deriveFont(font.getSize2D() - 2));
    viewedBy.setTitle(ManageRndSettingsAction.NAME_OWNER);
    viewedBy.setIcon(icons.getIcon(IconManager.RND_OWNER));
    controlsBar = new PreviewControlBar(controller, model);
    uiDelegate = new GraphicsPaneUI(this, model);
    codomainSlider = new TwoKnobsSlider(RendererModel.CD_START, RendererModel.CD_END, model.getCodomainStart(),
            model.getCodomainEnd());
    codomainSlider.setBackground(UIUtilities.BACKGROUND_COLOR);
    codomainSlider.setPaintLabels(false);
    codomainSlider.setPaintEndLabels(false);
    codomainSlider.setPaintTicks(false);
    codomainSlider.setColourGradients(Color.BLACK, Color.WHITE);
    codomainSlider.addPropertyChangeListener(this);

    domainSlider = new TextualTwoKnobsSlider();
    domainSlider.setBackground(UIUtilities.BACKGROUND_COLOR);
    initDomainSlider();
    domainSlider.getSlider().setPaintLabels(false);
    domainSlider.getSlider().setPaintEndLabels(false);
    domainSlider.getSlider().setPaintTicks(false);
    domainSlider.addPropertyChangeListener(this);
    maxLabel = new JLabel(formatValue(model.getGlobalMax()));
    minLabel = new JLabel(formatValue(model.getGlobalMin()));
    maxLabel.setBackground(UIUtilities.BACKGROUND_COLOR);
    minLabel.setBackground(UIUtilities.BACKGROUND_COLOR);
    sliders = new ArrayList<ChannelSlider>();
    if (model.getModuloT() != null || !model.isLifetimeImage()) {
        List<ChannelData> channels = model.getChannelData();
        Iterator<ChannelData> i = channels.iterator();
        ChannelSlider slider;
        int columns = 0;
        while (i.hasNext()) {
            slider = new ChannelSlider(this, model, controller, i.next());
            columns = Math.max(columns, slider.getColumns());
            sliders.add(slider);
        }
        Iterator<ChannelSlider> j = sliders.iterator();
        while (j.hasNext()) {
            j.next().setColumns(columns);
        }
    }
    previewToolBar = new PreviewToolBar(controller, model);
}

From source file:com.github.benchdoos.weblocopener.weblocOpener.gui.EditDialog.java

private void setTextFieldFont(Font font, TextAttribute attribute1, int attribute2) {
    Map attributes = font.getAttributes();
    attributes.put(attribute1, attribute2);
    textField.setFont(font.deriveFont(attributes));
}

From source file:JXTransformer.java

private JPanel createDemoPanel() {
        JPanel buttonPanel = new JPanel(new GridLayout(3, 2));
        TitledBorder titledBorder = BorderFactory.createTitledBorder("Try three sliders below !");
        Font titleFont = titledBorder.getTitleFont();
        titledBorder.setTitleFont(titleFont.deriveFont(titleFont.getSize2D() + 10));
        titledBorder.setTitleJustification(TitledBorder.CENTER);
        buttonPanel.setBorder(titledBorder);
        JButton b = new JButton("JButton");
        b.setPreferredSize(new Dimension(100, 50));
        buttonPanel.add(createTransformer(b));

        Vector<String> v = new Vector<String>();
        v.add("One");
        v.add("Two");
        v.add("Three");
        JList list = new JList(v);
        buttonPanel.add(createTransformer(list));

        buttonPanel.add(createTransformer(new JCheckBox("JCheckBox")));

        JSlider slider = new JSlider(0, 100);
        slider.setLabelTable(slider.createStandardLabels(25, 0));
        slider.setPaintLabels(true);// ww  w  .j  ava 2s . c  om
        slider.setPaintTicks(true);
        slider.setMajorTickSpacing(10);
        buttonPanel.add(createTransformer(slider));

        buttonPanel.add(createTransformer(new JRadioButton("JRadioButton")));
        final JLabel label = new JLabel("JLabel");
        label.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                Font font = label.getFont();
                label.setFont(font.deriveFont(font.getSize2D() + 10));
            }

            public void mouseExited(MouseEvent e) {
                Font font = label.getFont();
                label.setFont(font.deriveFont(font.getSize2D() - 10));
            }
        });
        buttonPanel.add(createTransformer(label));

        return buttonPanel;
    }

From source file:com.github.lucapino.sheetmaker.renderer.JavaTemplateRenderer.java

public void drawString(Graphics g, String text, RectangularShape bounds, Align align, double angle,
        boolean multiline) {
    Graphics2D g2 = (Graphics2D) g;
    Font font = g2.getFont();
    if (angle != 0) {
        g2.setFont(font.deriveFont(AffineTransform.getRotateInstance(Math.toRadians(angle))));
    }/* ww  w  .  j  a v  a  2 s . c  om*/

    Rectangle2D sSize = g2.getFontMetrics().getStringBounds(text, g2);
    Point2D pos = getPoint(bounds, align);
    double x = pos.getX();
    double y = pos.getY() + sSize.getHeight();

    switch (align) {
    case TopCenter:
    case BottomCenter:
    case Center:
        x -= (sSize.getWidth() / 2);
        break;
    case TopRight:
    case MiddleRight:
    case BottomRight:
        x -= (sSize.getWidth());
        break;
    case BottomLeft:
    case MiddleLeft:
    case TopLeft:
        break;
    }
    if (multiline) {
        // Create a new LineBreakMeasurer from the paragraph.
        // It will be cached and re-used.
        //if (lineMeasurer == null) {
        AttributedCharacterIterator paragraph = new AttributedString(text).getIterator();
        paragraphStart = paragraph.getBeginIndex();
        paragraphEnd = paragraph.getEndIndex();
        FontRenderContext frc = g2.getFontRenderContext();
        lineMeasurer = new LineBreakMeasurer(paragraph, frc);
        //}

        // Set break width to width of Component.
        float breakWidth = (float) bounds.getWidth();
        float drawPosY = (float) y;
        // Set position to the index of the first character in the paragraph.
        lineMeasurer.setPosition(paragraphStart);

        // Get lines until the entire paragraph has been displayed.
        while (lineMeasurer.getPosition() < paragraphEnd) {

            // Retrieve next layout. A cleverer program would also cache
            // these layouts until the component is re-sized.
            TextLayout layout = lineMeasurer.nextLayout(breakWidth);

            // Compute pen x position. If the paragraph is right-to-left we
            // will align the TextLayouts to the right edge of the panel.
            // Note: this won't occur for the English text in this sample.
            // Note: drawPosX is always where the LEFT of the text is placed.
            float drawPosX = layout.isLeftToRight() ? (float) x : (float) x + breakWidth - layout.getAdvance();

            // Move y-coordinate by the ascent of the layout.
            drawPosY += layout.getAscent();

            // Draw the TextLayout at (drawPosX, drawPosY).
            layout.draw(g2, drawPosX, drawPosY);

            // Move y-coordinate in preparation for next layout.
            drawPosY += layout.getDescent() + layout.getLeading();
        }
    } else {
        g2.drawString(text, (float) x, (float) y);
    }
    g2.setFont(font);
}

From source file:daylightchart.sunchart.chart.SunChart.java

@SuppressWarnings("unchecked")
private void createTitles(final ChartOptions chartOptions, final Font titleFont) {

    // Clear all titles and subtitles
    setTitle((TextTitle) null);/*from   w ww .j  av  a  2  s .c  om*/
    for (final Title subtitle : (List<Title>) getSubtitles()) {
        if (subtitle instanceof TextTitle) {
            removeSubtitle(subtitle);
        }
    }

    // Build new titles and legend
    final Location location = sunChartData.getLocation();
    final boolean showTitle = chartOptions.getTitleOptions().getShowTitle();
    if (location != null && showTitle) {
        final TextTitle title = new TextTitle(location.toString(), titleFont);
        setTitle(title);

        final Font subtitleFont = titleFont.deriveFont(Font.PLAIN);
        final TextTitle subtitle = new TextTitle(location.getDetails(), subtitleFont);
        subtitle.setPaint(title.getPaint());
        addSubtitle(subtitle);
    }
}

From source file:hr.restart.util.chart.ChartXY.java

/**
 * Creates a BAR CHART/*from   w  w  w  . java  2s.c om*/
 * @param dataset The org.jfree.data.CategoryDataset
 * @param title The title
 * @return org.jfree.chart.JFreeChart
 */
private JFreeChart createBarChart(final CategoryDataset dataset, String title, PlotOrientation orientation) {

    final JFreeChart chart = ChartFactory.createBarChart(title, // chart title
            "", // domain axis label
            "", // range axis label
            dataset, // data
            orientation, // the plot orientation
            false, // include legend
            true, false);

    chart.setBackgroundPaint(Color.white);

    // the subtitle from the combobox        
    if (jcb != null)
        chart.addSubtitle(new TextTitle(jcb.getSelectedItem().toString()));

    //subtitles setted by the user.
    if (getSubtitles() != null)
        for (int i = 0; i < getSubtitles().size(); i++) {
            chart.addSubtitle(new TextTitle(getSubtitles().get(i).toString()));
        }

    final Plot plot = chart.getPlot();

    // get a reference to the plot for further customisation...
    final CategoryPlot categoryPlot = (CategoryPlot) plot;

    categoryPlot.setNoDataMessage("NO DATA!");

    categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

    final CategoryItemRenderer renderer = new CustomRenderer(new Paint[] { Color.red, Color.blue, Color.green,
            Color.yellow, Color.orange, Color.cyan, Color.magenta, Color.blue });
    categoryPlot.setRenderer(renderer);

    renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
    renderer.setItemLabelsVisible(true);

    // inside
    //renderer.setBaseItemLabelPaint(Color.white);
    Font font = new Font("SansSerif", Font.PLAIN, 7);
    Font derive = font.deriveFont(Font.BOLD);
    renderer.setBaseItemLabelFont(derive);

    // margin
    final CategoryAxis domainAxis = categoryPlot.getDomainAxis();
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    //domainAxis.setBottomCategoryLabelPosition(new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_CENTER));
    domainAxis.setCategoryLabelPositions(new CategoryLabelPositions(
            new CategoryLabelPosition(RectangleAnchor.TOP, TextBlockAnchor.TOP_CENTER), // TOP
            new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_CENTER), // BOTTOM
            new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT,
                    CategoryLabelWidthType.RANGE, 0.30f), // LEFT
            new CategoryLabelPosition(RectangleAnchor.RIGHT, TextBlockAnchor.CENTER_RIGHT,
                    CategoryLabelWidthType.RANGE, 0.30f) // RIGHT) 
    ));

    final ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER,
            TextAnchor.CENTER, 0.0);
    renderer.setPositiveItemLabelPosition(p);

    if (comboBoxOrientation != null) {
        if (comboBoxOrientation.getSelectedItem() == "Vertikalni") {

            domainAxis.setCategoryLabelPositions(
                    CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4.0));
        }
    }

    return chart;

}

From source file:daylightchart.daylightchart.chart.DaylightChart.java

@SuppressWarnings("unchecked")
private void createTitles(final ChartOptions chartOptions, final Font titleFont) {

    // Clear all titles and subtitles
    setTitle((TextTitle) null);//from www  .j a  v  a 2  s . c o  m
    for (final Title subtitle : (List<Title>) getSubtitles()) {
        if (subtitle instanceof TextTitle) {
            removeSubtitle(subtitle);
        }
    }

    // Build new titles and legend
    final Location location = riseSetData.getLocation();
    final boolean showTitle = chartOptions.getTitleOptions().getShowTitle();
    if (location != null && showTitle) {
        final TextTitle title = new TextTitle(location.toString(), titleFont);
        setTitle(title);

        final Font subtitleFont = titleFont.deriveFont(Font.PLAIN);
        final TextTitle subtitle = new TextTitle(location.getDetails(), subtitleFont);
        subtitle.setPaint(title.getPaint());
        addSubtitle(subtitle);
    }
}

From source file:web.diva.server.model.SomClustering.SomClustImgGenerator.java

private Font getTableFont(int size) {
    Font f;
    f = new Font("Sans Serif", 0, size);
    return f.deriveFont((float) size);
}