Hi,

I see a change in the font with string created by graphic2D from other labels. Is it possible to obtain a uniform font?

package com.HIPS.biosynML;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

public class WrappedCellRenderer extends DefaultTreeCellRenderer {
static ExpandingPanels test;
        private static final long serialVersionUID = -3452124074088778903L;
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value,
                        boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

                 super.getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, hasFocus);
                 final String stringValue = tree.convertValueToText(value, isSelected,
                                        expanded, leaf, row, hasFocus);
                 setText(stringValue);
                 setBackground(test.p6.getBackground());
                 return this;
        }
         private String text;
        private Insets margin = new Insets(5,5,5,5);
        private int maxWidth = Integer.MAX_VALUE;
        private boolean justify;
        private final FontRenderContext frc = new FontRenderContext(null, false, false);

        private void morph() {
            revalidate();
            repaint();
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            String old = this.text;
            this.text = text;
            firePropertyChange("text", old, this.text);
            if ((old == null) ? text!=null : !old.equals(text))
                morph();
        }

        public int getMaxWidth() {
            return maxWidth;
        }

        public void setMaxWidth(int maxWidth) {

            int old = this.maxWidth;
            this.maxWidth = maxWidth;
            firePropertyChange("maxWidth", old, this.maxWidth);
            if (old !=  this.maxWidth)
                morph();
        }

        public boolean isJustified() {
            return justify;
        }

        public void setJustified(boolean justify) {
            boolean old = this.justify;
            this.justify = justify;
            firePropertyChange("justified", old, this.justify);
            if (old != this.justify)
                repaint();
        }

        public Dimension getPreferredSize() {
            return paintOrGetSize(null, getMaxWidth());
        }

        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        protected void paintComponent(Graphics g) {
            //super.paintComponent(g);



            paintOrGetSize((Graphics2D)g, getWidth());
        }
        public void dimChanged()
        {
             setText(text);
             morph();
        }
        public int getRowHeight()
        {
             return getPreferredSize().height;
        }
        private Dimension paintOrGetSize(Graphics2D g, int width) {
            Insets insets = getInsets();
            Font f = test.p1.getFont();



            width -= insets.left + insets.right + margin.left + margin.right;
            float w = insets.left + insets.right + margin.left + margin.right;
            float x = insets.left + margin.left, y=insets.top + margin.top;
            if (width > 0 && text != null && text.length() > 0) {
                AttributedString as = new AttributedString(getText());

                as.addAttribute(TextAttribute.FONT, f);
         //       as.addAttribute(TextAttribute.FONT,  new Font("Serif", Font.PLAIN, 12));
               // as.addAttribute(TextAttribute.FONT, new Font("Serif", Font.PLAIN, 12));
                as.addAttribute(TextAttribute.FOREGROUND, Color.BLACK.darker());
                AttributedCharacterIterator aci = as.getIterator();
                LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
                float max = 0;
                while (lbm.getPosition() < aci.getEndIndex()) {
                    TextLayout textLayout = lbm.nextLayout(width);

                    if (g != null && isJustified() && textLayout.getVisibleAdvance() > 0.80 * width)

                        textLayout = textLayout.getJustifiedLayout(width);
                    if (g != null)
                    {



                        //g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                        textLayout.draw(g, x, y + textLayout.getAscent());
                    }

                    y += textLayout.getDescent() + textLayout.getLeading() + textLayout.getAscent();
                    max = Math.max(max, textLayout.getVisibleAdvance());
                }
                w += max;
            }
            return new Dimension((int)Math.ceil(w), (int)Math.ceil(y) + insets.bottom + margin.bottom);
        }

}

thanks

This is how it looks

Did you setFont for your Graphics befroe drawing the text?

yes, I did. But I got the same result. The problem is that the font is correct but the thickness or darkness of characters is different

Maybe its because you are (or are not) using anti-aliasing???

I tried using antialising but result doesn't change

g.setRenderingHint(
                                RenderingHints.KEY_TEXT_ANTIALIASING,
                                RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.