All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.parubok.text.multiline.AbstractTextLayout Maven / Gradle / Ivy

There is a newer version: 1.20
Show newest version
package io.github.parubok.text.multiline;

import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Objects;

public abstract class AbstractTextLayout implements TextLayout {

    protected final MultilineLabel label;

    protected AbstractTextLayout(MultilineLabel label) {
        this.label = Objects.requireNonNull(label);
    }

    protected static int getTextPreferredHeight(int lineCount, FontMetrics fm, float lineSpacing) {
        final int yIncrement = MultilineUtils.getHeightIncrement(fm, lineSpacing);
        return fm.getAscent() + yIncrement  * (lineCount - 1) + fm.getDescent();
    }

    protected static void drawString(JComponent c, Graphics g, String str, int x, int y) {
        BasicGraphicsUtils.drawString(c, (Graphics2D) g, str, x, y);
    }

    /**
     * Draws {@code text} in a style of disabled component text at {@link Graphics} context from the point (x,y). Uses
     * {@code color} as a base.
     */
    protected static void drawStringInDisabledStyle(JComponent c, String str, Graphics g, Color color, int x, int y) {
        g.setColor(color.brighter());
        drawString(c, g, str, x + 1, y + 1);
        g.setColor(color.darker());
        drawString(c, g, str, x, y);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy