/** * Draws the specified string. * * @param g graphics reference * @param s text * @param x x coordinate * @param y y coordinate * @param w width * @param fs font size */ public static void chopString( final Graphics g, final byte[] s, final int x, final int y, final int w, final int fs) { if (w < 12) return; final int[] cw = fontWidths(g.getFont()); int j = s.length; try { int l = 0; int fw = 0; for (int k = 0; k < j; k += l) { final int ww = width(g, cw, cp(s, k)); if (fw + ww >= w - 4) { j = Math.max(1, k - l); if (k > 1) fw -= width(g, cw, cp(s, k - 1)); g.drawString("..", x + fw, y + fs); break; } fw += ww; l = cl(s, k); } } catch (final Exception ex) { Util.debug(ex); } g.drawString(string(s, 0, j), x, y + fs); }
/** * Draws a visualization tooltip. * * @param g graphics reference * @param tt tooltip label * @param x horizontal position * @param y vertical position * @param w width * @param c color color depth */ public static void drawTooltip( final Graphics g, final String tt, final int x, final int y, final int w, final int c) { final int tw = width(g, tt); final int th = g.getFontMetrics().getHeight(); final int xx = Math.min(w - tw - 8, x); g.setColor(color(c)); g.fillRect(xx - 1, y - th, tw + 4, th); g.setColor(BACK); g.drawString(tt, xx, y - 4); }
/** * Paints the error marker. * * @param g graphics reference */ private void drawError(final Graphics g) { final int ww = wordW != 0 ? wordW : charW(g, ' '); final int s = Math.max(1, fontH / 8); g.setColor(GUIConstants.LRED); g.fillRect(x, y + 2, ww, s); g.setColor(GUIConstants.RED); for (int xp = x; xp < x + ww; xp++) { if ((xp & 1) == 0) g.drawLine(xp, y + 2, xp, y + s + 1); } }
/** * Returns the width of the specified codepoint. * * @param g graphics reference * @param cp character * @return width */ private int charW(final Graphics g, final int cp) { return cp < ' ' || g == null ? cp == '\t' ? fwidth[' '] * BaseXTextTokens.TAB : 0 : cp < 256 ? fwidth[cp] : cp >= 0xD800 && cp <= 0xDC00 ? 0 : g.getFontMetrics().charWidth(cp); }
/** * Initializes the renderer. * * @param g graphics reference * @param pos current text position */ private void init(final Graphics g, final int pos) { font = dfont; color = Color.black; syntax.init(); text.init(); x = off; y = off + fontH - pos - 2; if (g != null) g.setFont(font); }
/** * Returns the width of the specified text. Cached font widths are used to speed up calculation. * * @param g graphics reference * @param s string to be evaluated * @return string width */ public static int width(final Graphics g, final byte[] s) { final int[] cw = fontWidths(g.getFont()); final int l = s.length; int fw = 0; try { // ignore faulty character sets for (int k = 0; k < l; k += cl(s, k)) fw += width(g, cw, cp(s, k)); } catch (final Exception ex) { Util.debug(ex); } return fw; }
/** * Draws a colored cell. * * @param g graphics reference * @param xs horizontal start position * @param xe horizontal end position * @param ys vertical start position * @param ye vertical end position * @param focus highlighting flag */ public static void drawCell( final Graphics g, final int xs, final int xe, final int ys, final int ye, final boolean focus) { g.setColor(gray); g.drawRect(xs, ys, xe - xs - 1, ye - ys - 1); g.setColor(BACK); g.drawRect(xs + 1, ys + 1, xe - xs - 3, ye - ys - 3); g.setColor(focus ? lgray : BACK); g.fillRect(xs + 1, ys + 1, xe - xs - 2, ye - ys - 2); }
@Override public void paintComponent(final Graphics g) { super.paintComponent(g); final Runtime rt = Runtime.getRuntime(); final long max = rt.maxMemory(); final long total = rt.totalMemory(); final long used = total - rt.freeMemory(); final int ww = getWidth(); final int hh = getHeight(); // draw memory box g.setColor(Color.white); g.fillRect(0, 0, ww - 3, hh - 3); g.setColor(GRAY); g.drawLine(0, 0, ww - 4, 0); g.drawLine(0, 0, 0, hh - 4); g.drawLine(ww - 3, 0, ww - 3, hh - 3); g.drawLine(0, hh - 3, ww - 3, hh - 3); // show total memory usage g.setColor(color1); g.fillRect(2, 2, Math.max(1, (int) (total * (ww - 6) / max)), hh - 6); // show current memory usage final boolean full = used * 6 / 5 > max; g.setColor(full ? colormark4 : color3); g.fillRect(2, 2, Math.max(1, (int) (used * (ww - 6) / max)), hh - 6); // print current memory usage final FontMetrics fm = g.getFontMetrics(); final String mem = Performance.format(used, true); final int fw = (ww - fm.stringWidth(mem)) / 2; final int h = fm.getHeight() - 3; g.setColor(full ? colormark3 : DGRAY); g.drawString(mem, fw, h); }
/** * Paints the text cursor. * * @param g graphics reference * @param xx x position */ private void cursor(final Graphics g, final int xx) { g.setColor(Color.black); g.drawLine(xx, y - fontH + 4, xx, y + 3); }
/** * Writes the current string to the graphics reference. * * @param g graphics reference */ private void write(final Graphics g) { if (high) { high = false; } else { color = isEnabled() ? syntax.getColor(text) : Color.gray; } final int ch = text.curr(); if (y > 0 && y < h) { if (ch == TokenBuilder.MARK) { color = GUIConstants.GREEN; high = true; } // mark error if (text.erroneous()) { g.setColor(GUIConstants.LRED); g.fillRect(x, y - fontH + 4, wordW, fontH); } // mark text int xx = x; if (text.markStart()) { final int p = text.pos(); while (text.more()) { final int cw = charW(g, text.curr()); if (text.inMark()) { g.setColor(GUIConstants.color(3)); g.fillRect(xx, y - fontH + 4, cw, fontH); } xx += cw; text.next(); } text.pos(p); } if (found()) { int cw = 0; for (int c = 0; c < keyword.length(); ++c) { cw += charW(g, keyword.charAt(c)); } g.setColor(GUIConstants.color(text.cursor() == text.pos() ? 5 : 2)); g.fillRect(x, y - fontH + 4, cw, fontH); } // don't write whitespaces if (ch > ' ') { g.setColor(color); String n = text.nextWord(); int ww = w - x; if (x + wordW > ww) { // shorten string if it cannot be completely shown (saves memory) int c = 0; for (final int nl = n.length(); c < nl && ww > 0; c++) { ww -= charW(g, n.charAt(c)); } n = n.substring(0, c); } g.drawString(n, x, y); } else if (ch <= TokenBuilder.MARK) { g.setFont(font); } // show cursor if (cursor && text.edited()) { xx = x; final int p = text.pos(); while (text.more()) { if (text.cursor() == text.pos()) { cursor(g, xx); break; } xx += charW(g, text.next()); } text.pos(p); } } next(); }
/** * Returns the character width of the specified character. * * @param g graphics reference * @param cw array with character widths * @param c character * @return character width */ public static int width(final Graphics g, final int[] cw, final int c) { return c >= cw.length ? g.getFontMetrics().charWidth(c) : cw[c]; }
/** * Returns the width of the specified text. * * @param g graphics reference * @param s string to be evaluated * @return string width */ public static int width(final Graphics g, final String s) { return g.getFontMetrics().stringWidth(s); }
/** * Draws a centered string to the panel. * * @param g graphics reference * @param text text to be painted * @param w panel width * @param y vertical position */ public static void drawCenter(final Graphics g, final String text, final int w, final int y) { g.drawString(text, (w - width(g, text)) / 2, y); }
/** * Paints the text cursor. * * @param g graphics reference * @param xx x position */ private void drawCursor(final Graphics g, final int xx) { g.setColor(GUIConstants.DGRAY); g.fillRect(xx, y - fontH * 4 / 5, 2, fontH); }
/** * Writes the current string to the graphics reference. * * @param g graphics reference */ private void write(final Graphics g) { if (high) { high = false; } else { color = isEnabled() ? syntax.getColor(text) : Color.gray; } final int ch = text.curr(); if (y > 0 && y < h) { if (ch == TokenBuilder.MARK) { color = GUIConstants.GREEN; high = true; } // mark selected text final int cp = text.pos(); if (text.selectStart()) { int xx = x, cw = 0; while (!text.inSelect() && text.more()) xx += charW(g, text.next()); while (text.inSelect() && text.more()) cw += charW(g, text.next()); g.setColor(GUIConstants.color(3)); g.fillRect(xx, y - fontH * 4 / 5, cw, fontH); text.pos(cp); } // mark found text int xx = x; while (text.more() && text.searchStart()) { int cw = 0; while (!text.inSearch() && text.more()) xx += charW(g, text.next()); while (text.inSearch() && text.more()) cw += charW(g, text.next()); g.setColor(GUIConstants.color2A); g.fillRect(xx, y - fontH * 4 / 5, cw, fontH); xx += cw; } text.pos(cp); if (text.erroneous()) drawError(g); // don't write whitespaces if (ch > ' ') { g.setColor(color); String n = text.nextString(); int ww = w - x; if (x + wordW > ww) { // shorten string if it cannot be completely shown (saves memory) int c = 0; for (final int nl = n.length(); c < nl && ww > 0; c++) { ww -= charW(g, n.charAt(c)); } n = n.substring(0, c); } g.drawString(n, x, y); } else if (ch <= TokenBuilder.MARK) { g.setFont(font); } // show cursor if (cursor && text.edited()) { xx = x; while (text.more()) { if (text.cursor() == text.pos()) { drawCursor(g, xx); break; } xx += charW(g, text.next()); } text.pos(cp); } } next(); }