java.awt.Graphics2D#drawChars ( )源码实例Demo

下面列出了java.awt.Graphics2D#drawChars ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Exam-Online   文件: CaptchaUtils.java
private static void addCaptcha(Graphics2D g2d, String code) {
    int unitWidth = IMG_WIDTH / code.length();
    char[] codeChars = code.toCharArray();
    ThreadLocalRandom random = ThreadLocalRandom.current();
    for (int i=0; i<code.length(); ++i) {
        int x = MARGIN_HORIZONTAL + i * unitWidth;
        int y = MARGIN_VERTICAL + random.nextInt(FONT_SIZE);
        double theta = Math.toRadians(
                random.nextInt(-MAX_ANGEL_OF_ROTATION, MAX_ANGEL_OF_ROTATION));
        g2d.setColor(getRandomColor(MIN_LETTER_COLOR_VAL, MAX_LETTER_COLOR_VAL));
        g2d.setFont(getRandomFont());
        g2d.rotate(theta, x, y);    // 旋转
        g2d.drawChars(codeChars, i, 1, x, y);
        g2d.rotate(-theta, x, y);   // 复原旋转
    }
}
 
源代码2 项目: lams   文件: CharBox.java
public void draw(Graphics2D g2, float x, float y) {
    drawDebug(g2, x, y);
    AffineTransform at = g2.getTransform();
    g2.translate(x, y);
    Font font = FontInfo.getFont(cf.fontId);

    if (Math.abs(size - TeXFormula.FONT_SCALE_FACTOR) > TeXFormula.PREC) {
        g2.scale(size / TeXFormula.FONT_SCALE_FACTOR,
                 size / TeXFormula.FONT_SCALE_FACTOR);
    }

    if (g2.getFont() != font) {
        g2.setFont(font);
    }

    arr[0] = cf.c;
    g2.drawChars(arr, 0, 1, 0, 0);
    g2.setTransform(at);
}
 
源代码3 项目: amidst   文件: GridDrawer.java
@SuppressWarnings("unused")
private void drawThickTextOutline(Graphics2D g2d) {
	g2d.drawChars(textCache, 0, textBuffer.length(), 12, 15);
	g2d.drawChars(textCache, 0, textBuffer.length(), 12, 19);
	g2d.drawChars(textCache, 0, textBuffer.length(), 8, 15);
	g2d.drawChars(textCache, 0, textBuffer.length(), 8, 19);
}
 
源代码4 项目: PolyGlot   文件: PComboBox.java
@Override
public void paintComponent(Graphics g) {
    final int buttonWidth = 20;
    boolean enabled = this.isEnabled();
    
    // turn on anti-alias mode
    Graphics2D antiAlias = (Graphics2D) g;
    antiAlias.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
    if (enabled) {
        antiAlias.setColor(Color.white);
    } else {
        antiAlias.setColor(Color.decode("#e0e0e4"));
    }
    antiAlias.fillRoundRect(1, 1, getWidth(), getHeight() - 2, 5, 5);
    
    // draw text
    if (enabled) {
        antiAlias.setColor(Color.black);
    } else {
        antiAlias.setColor(Color.decode("#909090"));
    } 
    Object selectedItem = getSelectedItem();
    String text = selectedItem == null ? "" : selectedItem.toString();
    
    // display default text if appropriate
    if (text.isBlank() && this.getSelectedIndex() == 0) {
        text = defaultText;
        antiAlias.setColor(Color.decode("#909090"));
    }
    
    if (!text.isEmpty()) { // 0 length text makes bounding box explode
        FontMetrics fm = antiAlias.getFontMetrics(getFont());
        Rectangle2D rec = fm.getStringBounds(text, antiAlias);
        int stringW = (int) Math.round(rec.getWidth());
        int stringH = (int) Math.round(rec.getHeight());
        antiAlias.drawChars(text.toCharArray(), 0, text.length(), ((getWidth() - buttonWidth)/2) 
                - (stringW/2), (getHeight() - 9)/2 + stringH/2);
    }
    
    if (enabled) {
        antiAlias.setColor(PGTUtil.COLOR_ENABLED_BG);
    } else {
        antiAlias.setColor(Color.decode("#d0d0d0"));
    }
    antiAlias.fillRect(getWidth() - buttonWidth, 1, buttonWidth, getHeight() - 1);
    
    // draw outline
    if ((mouseOver || this.hasFocus()) && enabled) {
        antiAlias.setColor(Color.black);
    } else 
    {
        antiAlias.setColor(Color.lightGray);
    }
    antiAlias.drawRoundRect(1, 1, getWidth() - 2, getHeight() - 2, 5, 5);
}
 
源代码5 项目: dragonwell8_jdk   文件: FontPanel.java
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
源代码6 项目: TencentKona-8   文件: FontPanel.java
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
源代码7 项目: openjdk-8   文件: FontPanel.java
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
源代码8 项目: jdk8u-jdk   文件: FontPanel.java
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
源代码9 项目: jdk8u60   文件: FontPanel.java
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
源代码10 项目: openjdk-jdk8u   文件: FontPanel.java
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
源代码11 项目: openjdk-jdk8u   文件: FontPanel.java
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
源代码12 项目: openjdk-jdk8u-backup   文件: FontPanel.java
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
源代码13 项目: jdk8u_jdk   文件: StyledFontLayoutTest.java
private static void runTest() {
    im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = im.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, W, H);
    g2d.setColor(Color.black);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    char[] chs = "Sample Text.".toCharArray();
    int len = chs.length;

    int x = 50, y = 100;

    FontRenderContext frc = g2d.getFontRenderContext();
    Font plain = new Font("Serif", Font.PLAIN, 48);
    GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0);
    g2d.setFont(plain);
    g2d.drawChars(chs, 0, len, x, y); y +=50;

    g2d.drawGlyphVector(pgv, x, y); y += 50;
    Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc);
    Rectangle2D plainGVBounds = pgv.getLogicalBounds();
    Font bold = new Font("Serif", Font.BOLD, 48);
    GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0);
    Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc);
    Rectangle2D boldGVBounds = bgv.getLogicalBounds();
    g2d.setFont(bold);
    g2d.drawChars(chs, 0, len, x, y); y +=50;
    g2d.drawGlyphVector(bgv, x, y);
    System.out.println("Plain String Bounds = " + plainStrBounds);
    System.out.println("Bold String Bounds = " + boldStrBounds);
    System.out.println("Plain GlyphVector Bounds = " + plainGVBounds);
    System.out.println("Bold GlyphVector Bounds = " + boldGVBounds);
    if (!plainStrBounds.equals(boldStrBounds) &&
         plainGVBounds.equals(boldGVBounds))
    {
        System.out.println("Test failed: Plain GV bounds same as Bold");
        if (!interactive) {
            throw new RuntimeException("Plain GV bounds same as Bold");
        }
    }

}
 
源代码14 项目: openjdk-jdk9   文件: FontPanel.java
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
源代码15 项目: PolyGlot   文件: PCheckedComboBox.java
@Override
public void paintComponent(Graphics g) {
    final int buttonWidth = 20;
    boolean enabled = this.isEnabled();
    
    // turn on anti-alias mode
    Graphics2D antiAlias = (Graphics2D) g;
    antiAlias.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
    if (enabled) {
        antiAlias.setColor(Color.white);
    } else {
        antiAlias.setColor(Color.decode("#e0e0e4"));
    }
    
    antiAlias.fillRoundRect(1, 1, getWidth(), getHeight() - 2, 5, 5);
    
    if (enabled) {
        antiAlias.setColor(PGTUtil.COLOR_ENABLED_BG);
    } else {
        antiAlias.setColor(Color.decode("#d0d0d0"));
    }
    antiAlias.fillRect(getWidth() - buttonWidth, 1, buttonWidth, getHeight() - 1);
    
    if ((mouseOver || this.hasFocus()) && enabled) {
        antiAlias.setColor(Color.black);
    } else 
    {
        antiAlias.setColor(Color.lightGray);
    }
    antiAlias.drawRoundRect(1, 1, getWidth() - 2, getHeight() - 2, 5, 5);
    
    if (enabled) {
        antiAlias.setColor(Color.black);
    } else {
        antiAlias.setColor(Color.decode("#909090"));
    }
    
    String text = getCheckedItemString();
    if (!text.isEmpty()) { // 0 length text makes bounding box explode
        FontMetrics fm = antiAlias.getFontMetrics(getFont());
        Rectangle2D rec = fm.getStringBounds(text, antiAlias);
        int stringW = (int) Math.round(rec.getWidth());
        int stringH = (int) Math.round(rec.getHeight());
        antiAlias.drawChars(text.toCharArray(), 0, text.length(), ((getWidth() - buttonWidth)/2) 
                - (stringW/2), (getHeight() - 9)/2 + stringH/2);
    }
}
 
源代码16 项目: amidst   文件: GridDrawer.java
@CalledOnlyBy(AmidstThread.EDT)
private void drawTextOutline(Graphics2D g2d) {
	g2d.setColor(Color.white);
	g2d.drawChars(textCache, 0, textBuffer.length(), 10, 17);
}
 
源代码17 项目: jdk8u-dev-jdk   文件: FontPanel.java
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
源代码18 项目: hottub   文件: FontPanel.java
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
源代码19 项目: hottub   文件: FontPanel.java
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
源代码20 项目: openjdk-8-source   文件: FontPanel.java
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}