/** * 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); }
@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); }
/** * 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); }