sun.swing.PrintColorUIResource Java Examples

The following examples show how to use sun.swing.PrintColorUIResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SwingUtilities2.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static float drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             float x,
                             float y,
                             boolean useFPAPI) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    float nextX = x + getFontCharsWidth(data, offset, length,
                                        getFontMetrics(c, g),
                                        useFPAPI);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    // If text fits the screenWidth, then do not need to justify
                    if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(),
                                            trimmedText) > screenWidth) {
                        layout = layout.getJustifiedLayout(screenWidth);
                    }

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }

    if (!(g instanceof Graphics2D)) {
        g.drawChars(data, offset, length, (int) x, (int) y);
        return nextX;
    }

    Graphics2D g2 = (Graphics2D) g;
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null) {
        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g2.drawString(new String(data, offset, length), x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g2.drawString(new String(data, offset, length), x, y);
    }
    return nextX;
}
 
Example #2
Source File: SwingUtilities2.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The following draw functions have the same semantic as the
 * Graphics methods with the same names.
 *
 * this is used for printing
 */
public static int drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             int x,
                             int y) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    layout = layout.getJustifiedLayout(screenWidth);

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D)g;

        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g.drawChars(data, offset, length, x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
 
Example #3
Source File: SwingUtilities2.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static float drawString(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               int x,
                               int y) {

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator,x,y); //for the cases where advance
                                    //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}
 
Example #4
Source File: sun_swing_PrintColorUIResource.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #5
Source File: SwingUtilities2.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The following draw functions have the same semantic as the
 * Graphics methods with the same names.
 *
 * this is used for printing
 */
public static int drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             int x,
                             int y) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    layout = layout.getJustifiedLayout(screenWidth);

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D)g;

        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g.drawChars(data, offset, length, x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
 
Example #6
Source File: SwingUtilities2.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static float drawString(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               int x,
                               int y) {

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator,x,y); //for the cases where advance
                                    //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}
 
Example #7
Source File: sun_swing_PrintColorUIResource.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #8
Source File: SwingUtilities2.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The following draw functions have the same semantic as the
 * Graphics methods with the same names.
 *
 * this is used for printing
 */
public static int drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             int x,
                             int y) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    layout = layout.getJustifiedLayout(screenWidth);

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D)g;

        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g.drawChars(data, offset, length, x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
 
Example #9
Source File: SwingUtilities2.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static float drawString(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               int x,
                               int y) {

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator,x,y); //for the cases where advance
                                    //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}
 
Example #10
Source File: sun_swing_PrintColorUIResource.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #11
Source File: sun_swing_PrintColorUIResource.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #12
Source File: SwingUtilities2.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static float drawStringImpl(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               float x, float y)
{

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator, (int)x, (int)y); //for the cases where advance
                                                //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}
 
Example #13
Source File: sun_swing_PrintColorUIResource.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #14
Source File: SwingUtilities2.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The following draw functions have the same semantic as the
 * Graphics methods with the same names.
 *
 * this is used for printing
 */
public static int drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             int x,
                             int y) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    layout = layout.getJustifiedLayout(screenWidth);

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D)g;

        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g.drawChars(data, offset, length, x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
 
Example #15
Source File: SwingUtilities2.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static float drawString(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               int x,
                               int y) {

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator,x,y); //for the cases where advance
                                    //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}
 
Example #16
Source File: sun_swing_PrintColorUIResource.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #17
Source File: SwingUtilities2.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The following draw functions have the same semantic as the
 * Graphics methods with the same names.
 *
 * this is used for printing
 */
public static int drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             int x,
                             int y) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    layout = layout.getJustifiedLayout(screenWidth);

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D)g;

        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g.drawChars(data, offset, length, x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
 
Example #18
Source File: SwingUtilities2.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static float drawString(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               int x,
                               int y) {

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator,x,y); //for the cases where advance
                                    //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}
 
Example #19
Source File: sun_swing_PrintColorUIResource.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #20
Source File: SwingUtilities2.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The following draw functions have the same semantic as the
 * Graphics methods with the same names.
 *
 * this is used for printing
 */
public static int drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             int x,
                             int y) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    // If text fits the screenWidth, then do not need to justify
                    if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(),
                                            trimmedText) > screenWidth) {
                        layout = layout.getJustifiedLayout(screenWidth);
                    }

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D)g;

        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g.drawChars(data, offset, length, x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
 
Example #21
Source File: SwingUtilities2.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static float drawString(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               int x,
                               int y) {

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator,x,y); //for the cases where advance
                                    //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}
 
Example #22
Source File: sun_swing_PrintColorUIResource.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #23
Source File: SwingUtilities2.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The following draw functions have the same semantic as the
 * Graphics methods with the same names.
 *
 * this is used for printing
 */
public static int drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             int x,
                             int y) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    // If text fits the screenWidth, then do not need to justify
                    if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(),
                                            trimmedText) > screenWidth) {
                        layout = layout.getJustifiedLayout(screenWidth);
                    }

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D)g;

        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g.drawChars(data, offset, length, x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
 
Example #24
Source File: SwingUtilities2.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static float drawString(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               int x,
                               int y) {

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator,x,y); //for the cases where advance
                                    //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}
 
Example #25
Source File: sun_swing_PrintColorUIResource.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #26
Source File: SwingUtilities2.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The following draw functions have the same semantic as the
 * Graphics methods with the same names.
 *
 * this is used for printing
 */
public static int drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             int x,
                             int y) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    layout = layout.getJustifiedLayout(screenWidth);

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D)g;

        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g.drawChars(data, offset, length, x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
 
Example #27
Source File: SwingUtilities2.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static float drawString(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               int x,
                               int y) {

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator,x,y); //for the cases where advance
                                    //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}
 
Example #28
Source File: sun_swing_PrintColorUIResource.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
protected Color getAnotherObject() {
    return new PrintColorUIResource(0, Color.WHITE);
}
 
Example #29
Source File: SwingUtilities2.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The following draw functions have the same semantic as the
 * Graphics methods with the same names.
 *
 * this is used for printing
 */
public static int drawChars(JComponent c, Graphics g,
                             char[] data,
                             int offset,
                             int length,
                             int x,
                             int y) {
    if ( length <= 0 ) { //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.
                getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null &&
                !isFontRenderContextPrintCompatible
                (deviceFontRenderContext, frc)) {

                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(),
                                deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(trimmedText, frc).getWidth();
                    // If text fits the screenWidth, then do not need to justify
                    if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(),
                                            trimmedText) > screenWidth) {
                        layout = layout.getJustifiedLayout(screenWidth);
                    }

                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
                    }

                    layout.draw(g2d,x,y);

                    g2d.setColor(col);
                }

                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D)g;

        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
                                    info.lcdContrastHint);
            }
        }

        g.drawChars(data, offset, length, x, y);

        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    }
    else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
 
Example #30
Source File: SwingUtilities2.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static float drawString(JComponent c, Graphics g,
                               AttributedCharacterIterator iterator,
                               int x,
                               int y) {

    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();

    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource)col).getPrintColor());
        }
    }

    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        g.drawString(iterator,x,y); //for the cases where advance
                                    //matters it should not happen
        retVal = x;

    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
            /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt =
                        getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).
                            getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }

    if (isPrinting) {
        g.setColor(col);
    }

    return retVal;
}