openjdk/jdk · warning · CannotDrawException

ERROR: drawBytes cannot handle characters beyond 0x00FF. Sel

Error message

ERROR: drawBytes cannot handle characters beyond 0x00FF. Select different range or draw methods.

What it means

CannotDrawException thrown by Font2DTest's FontPanel when the draw method is set to 'Draw Bytes' but the current character code exceeds 0xFF. byte-based drawing (Graphics.drawBytes) cannot represent characters above 255, so the panel refuses instead of silently drawing the wrong glyph.

Source

Thrown at src/demo/share/jfc/Font2DTest/FontPanel.java:599

            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() );

View on GitHub (pinned to 88dfb74bbe)

Solutions

  1. Switch the draw method to Draw String, Draw Chars, or Glyph Vector in the Font2DTest UI.
  2. Restrict the displayed range to 0x0000-0x00FF when Draw Bytes is required.

Example fix

// guard before drawing (as the demo does in drawCmd/nextLineRequested)
// before: unconditional
if (drawMethod == DRAW_BYTES) drawBytesRange(charCode);

// after: skip or warn
if (drawMethod == DRAW_BYTES && charCode > 0xff) {
    statusLabel.setText("drawBytes limited to 0x00-0xFF; pick another method");
    return;
}
Defensive patterns

Strategy: type-guard

Validate before calling

// before rendering a range with DRAW_BYTES, clamp the range
if (drawMethod == DrawMethod.DRAW_BYTES) {
    rangeStart = Math.max(rangeStart, 0);
    rangeEnd   = Math.min(rangeEnd, 0xFF);
}

Type guard

// Java has no structural type guards; use a predicate method
private static boolean canDrawBytes(int charCode) {
    return (charCode & ~0xFF) == 0; // true only for 0x00..0xFF
}

// usage
if (drawMethod == DRAW_BYTES && !canDrawBytes(charCode)) {
    showWarning("drawBytes supports only U+0000..U+00FF");
    return;
}

Try / catch

try {
    fontPanel.drawCurrentRange();
} catch (CannotDrawException e) {
    // the panel already renders the message; also switch method automatically
    if (e.getMessage().contains("drawBytes")) setDrawMethod(DRAW_STRING); // graceful fallback
}

Prevention

When it happens

Trigger: Selecting the DRAW_BYTES method in the Font2DTest UI and then navigating/typing a range that includes Unicode codepoints > 0xFF (e.g. CJK, Cyrillic, Greek blocks).

Common situations: Testing a non-Latin font while the method combo box is still on 'Draw Bytes' after previously testing a Latin-1 range.

Related errors


AI-assisted analysis of openjdk/jdk@88dfb74bbe (2026-08-14). Data as JSON: /api/errors/af28eabfa057d029. Report an issue: GitHub.