{"record":{"id":"af28eabfa057d029","repo":"openjdk/jdk","slug":"error-drawbytes-cannot-handle-characters-beyond-0","errorCode":null,"errorMessage":"ERROR: drawBytes cannot handle characters beyond 0x00FF. Select different range or draw methods.","messagePattern":"ERROR: drawBytes cannot handle characters beyond 0x00FF\\. Select different range or draw methods\\.","errorType":"exception","errorClass":"CannotDrawException","httpStatus":null,"severity":"warning","filePath":"src/demo/share/jfc/Font2DTest/FontPanel.java","lineNumber":599,"sourceCode":"            if ( textToUse == ALL_GLYPHS )\n              g2.drawGlyphVector( gv, 0f, 0f );\n            else {\n                if ( testFont.canDisplay( charCode ))\n                  g2.setColor( Color.black );\n                else {\n                  g2.setColor( Color.lightGray );\n                }\n\n                switch ( drawMethod ) {\n                  case DRAW_STRING:\n                    g2.drawString( new String( charArray ), 0, 0 );\n                    break;\n                  case DRAW_CHARS:\n                    g2.drawChars( charArray, 0, 1, 0, 0 );\n                    break;\n                  case DRAW_BYTES:\n                    if ( charCode > 0xff )\n                      throw new CannotDrawException( DRAW_BYTES_ERROR );\n                    byte[] oneByte = { (byte) charCode };\n                    g2.drawBytes( oneByte, 0, 1, 0, 0 );\n                    break;\n                  case DRAW_GLYPHV:\n                    g2.drawGlyphVector( gv, 0f, 0f );\n                    break;\n                  case TL_DRAW:\n                    TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );\n                    tl.draw( g2, 0f, 0f );\n                    break;\n                  case GV_OUTLINE:\n                    r2d2 = gv.getVisualBounds();\n                    shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );\n                    g2.draw( gv.getOutline( 0f, 0f ));\n                    break;\n                  case TL_OUTLINE:\n                    r2d2 = gv.getVisualBounds();\n                    shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );","sourceCodeStart":581,"sourceCodeEnd":617,"githubUrl":"https://github.com/openjdk/jdk/blob/88dfb74bbeefcf2b0aa11835183bcd949998fc8f/src/demo/share/jfc/Font2DTest/FontPanel.java#L581-L617","documentation":"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.","triggerScenarios":"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).","commonSituations":"Testing a non-Latin font while the method combo box is still on 'Draw Bytes' after previously testing a Latin-1 range.","solutions":["Switch the draw method to Draw String, Draw Chars, or Glyph Vector in the Font2DTest UI.","Restrict the displayed range to 0x0000-0x00FF when Draw Bytes is required."],"exampleFix":"// guard before drawing (as the demo does in drawCmd/nextLineRequested)\n// before: unconditional\nif (drawMethod == DRAW_BYTES) drawBytesRange(charCode);\n\n// after: skip or warn\nif (drawMethod == DRAW_BYTES && charCode > 0xff) {\n    statusLabel.setText(\"drawBytes limited to 0x00-0xFF; pick another method\");\n    return;\n}","handlingStrategy":"type-guard","validationCode":"// before rendering a range with DRAW_BYTES, clamp the range\nif (drawMethod == DrawMethod.DRAW_BYTES) {\n    rangeStart = Math.max(rangeStart, 0);\n    rangeEnd   = Math.min(rangeEnd, 0xFF);\n}","typeGuard":"// Java has no structural type guards; use a predicate method\nprivate static boolean canDrawBytes(int charCode) {\n    return (charCode & ~0xFF) == 0; // true only for 0x00..0xFF\n}\n\n// usage\nif (drawMethod == DRAW_BYTES && !canDrawBytes(charCode)) {\n    showWarning(\"drawBytes supports only U+0000..U+00FF\");\n    return;\n}","tryCatchPattern":"try {\n    fontPanel.drawCurrentRange();\n} catch (CannotDrawException e) {\n    // the panel already renders the message; also switch method automatically\n    if (e.getMessage().contains(\"drawBytes\")) setDrawMethod(DRAW_STRING); // graceful fallback\n}","preventionTips":["Treat drawBytes as Latin-1 only by definition; default UIs to drawString/drawChars.","When the user selects a non-Latin range, auto-switch the draw method away from Draw Bytes."],"tags":["swing","font2dtest","drawbytes","unicode","demo"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}