/** * @param g Graphics context. * @param ch The character. * @return the width of the character. */ public int charWidth(Graphics g, char ch) { FontMetrics fm; if (g == null) return 0; if (font == null) fm = g.getFontMetrics(); else fm = g.getFontMetrics(font); return fm.charWidth(ch); }
private Dimension getContainerDimensions(Graphics g, steps.array.bucket.Container c) { int spacingWidth = (int) g.getFontMetrics(g.getFont()).getStringBounds(CONTAINER_VALUE_SPACING, g).getHeight(); int totalWidth = spacingWidth; int totalHeight = 0; for (int i = 0; i < c.theValues.length; i++) { totalHeight = (int) g.getFontMetrics(g.getFont()).getStringBounds("" + c.theValues[0], g).getHeight() * 2; totalWidth += (int) g.getFontMetrics(g.getFont()).getStringBounds("" + c.theValues[i], g).getWidth(); totalWidth += spacingWidth; } return new Dimension(totalWidth, totalHeight); }
/** Affichage du logo */ protected void drawLogo(Graphics g) { int x = 5; int y = 2; g.setColor(getBackground()); g.fillRect(0, 0, W, H); // Remplissage fillBG(g, x, y, Color.white); // Dessin drawGrid( g, x, y, !isAvailable() ? Aladin.MYGRAY : isActive() ? Aladin.GREEN : isMouseIn() ? Color.blue : Color.black); // Label g.setColor(isAvailable() ? Color.black : Aladin.MYGRAY); g.setFont(Aladin.SPLAIN); g.drawString(LABEL, W / 2 - g.getFontMetrics().stringWidth(LABEL) / 2, H - 2); }
public void paintComponent(Graphics g) { g.setColor(new Color(96, 96, 96)); image.paintIcon(this, g, 1, 1); FontMetrics fm = g.getFontMetrics(); String[] args = {jEdit.getVersion()}; String version = jEdit.getProperty("about.version", args); g.drawString(version, (getWidth() - fm.stringWidth(version)) / 2, getHeight() - 5); g = g.create((getWidth() - maxWidth) / 2, TOP, maxWidth, getHeight() - TOP - BOTTOM); int height = fm.getHeight(); int firstLine = scrollPosition / height; int firstLineOffset = height - scrollPosition % height; int lines = (getHeight() - TOP - BOTTOM) / height; int y = firstLineOffset; for (int i = 0; i <= lines; i++) { if (i + firstLine >= 0 && i + firstLine < text.size()) { String line = (String) text.get(i + firstLine); g.drawString(line, (maxWidth - fm.stringWidth(line)) / 2, y); } y += fm.getHeight(); } }
void drawRoiLabel(Graphics g, int index, Roi roi) { Rectangle r = roi.getBounds(); int x = screenX(r.x); int y = screenY(r.y); double mag = getMagnification(); int width = (int) (r.width * mag); int height = (int) (r.height * mag); int size = width > 40 && height > 40 ? 12 : 9; if (font != null) { g.setFont(font); size = font.getSize(); } else if (size == 12) g.setFont(largeFont); else g.setFont(smallFont); boolean drawingList = index >= LIST_OFFSET; if (drawingList) index -= LIST_OFFSET; String label = "" + (index + 1); if (drawNames && roi.getName() != null) label = roi.getName(); FontMetrics metrics = g.getFontMetrics(); int w = metrics.stringWidth(label); x = x + width / 2 - w / 2; y = y + height / 2 + Math.max(size / 2, 6); int h = metrics.getAscent() + metrics.getDescent(); if (bgColor != null) { g.setColor(bgColor); g.fillRoundRect(x - 1, y - h + 2, w + 1, h - 3, 5, 5); } if (!drawingList && labelRects != null && index < labelRects.length) labelRects[index] = new Rectangle(x - 1, y - h + 2, w + 1, h); g.setColor(labelColor); g.drawString(label, x, y - 2); g.setColor(defaultColor); }
public void paint(Graphics g) { gRef = (Graphics2D) g; // change size of font gRef.setFont(gRef.getFont().deriveFont(9.0f)); fmRef = g.getFontMetrics(); // Clear background if (Preferences.monochrome) { gRef.setColor(Preferences.whiteColor); } else { gRef.setColor(Preferences.backgroundColor); } gRef.fillRect(0, 0, getWidth(), getHeight()); // set colour to correct drawing colour if (Preferences.monochrome) { gRef.setColor(Preferences.blackColor); } else { gRef.setColor(Preferences.penColor); } gRef.translate(0, margin); // Call c code to draw tree gRef.scale(scale, scale); nativeDrawTree(); }
public void paint(Graphics g) { m_fm = g.getFontMetrics(); g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight()); g.setColor(getForeground()); g.setFont(getFont()); m_insets = getInsets(); int x = m_insets.left; int y = m_insets.top + m_fm.getAscent(); StringTokenizer st = new StringTokenizer(getText(), "\t"); while (st.hasMoreTokens()) { String sNext = st.nextToken(); g.drawString(sNext, x, y); x += m_fm.stringWidth(sNext); if (!st.hasMoreTokens()) break; int index = 0; while (x >= getTab(index)) index++; x = getTab(index); } }
public Foundry(Font f, Color defcol) { font = f; this.defcol = defcol; BufferedImage junk = TexI.mkbuf(new Coord(10, 10)); Graphics tmpl = junk.getGraphics(); tmpl.setFont(f); m = tmpl.getFontMetrics(); }
/** * 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); }
/** * Draws this process as a colored box with a process ID inside. * * @param g The graphics context. * @param x The leftmost x-coordinate of the box. * @param y The topmost y-coordinate of the box. * @param w The width of the box. * @param h The height of the box. */ public void draw(Graphics g, int x, int y, int w, int h) { g.setColor(color); g.fillRect(x, y, w, h); g.setColor(Color.black); g.drawRect(x, y, w, h); g.setFont(font); FontMetrics fm = g.getFontMetrics(font); g.drawString( "" + processId, x + w / 2 - fm.stringWidth("" + processId) / 2, y + h / 2 + fm.getHeight() / 2); }
public Line render(String text, Color c) { text = Translate.get(text); Coord sz = strsize(text); if (sz.x < 1) sz = sz.add(1, 0); BufferedImage img = TexI.mkbuf(sz); Graphics g = img.createGraphics(); if (aa) Utils.AA(g); g.setFont(font); g.setColor(c); FontMetrics m = g.getFontMetrics(); g.drawString(text, 0, m.getAscent()); g.dispose(); return (new Line(text, img, m)); }
public void CalculateSize(Graphics g) { oldwidth = sx2 - sx1; oldheight = sy2 - sy1; Font testfont; testfont = new Font("TimesRoman", Font.PLAIN, 100); g.setFont(testfont); int width = g.getFontMetrics().stringWidth(short_name); int height = g.getFontMetrics().getHeight(); double xratio = ((double) (oldwidth)) / ((double) width); double yratio = ((double) (oldheight)) / ((double) height); double fsize; if (xratio > yratio) { // use yratio to calculate size fsize = 100.0 * yratio; } else fsize = 100.0 * xratio; if (fsize < 1.0) fsize = 1.0; fsize = fsize * Caption.FontScale; currfont = new Font("TimesRoman", Font.PLAIN, (int) fsize); g.setFont(currfont); xoff = (sx2 - sx1 - g.getFontMetrics().stringWidth(short_name)) / 2; yoff = g.getFontMetrics().getDescent(); }
private void drawContainer(steps.array.bucket.Container c, Graphics g, int x, int y) { // calculate the size of all the printed values Dimension dims = getContainerDimensions(g, c); int totalWidth = dims.width, totalHeight = dims.height; String allValues = CONTAINER_VALUE_SPACING; for (int i = 0; i < c.theValues.length; i++) { allValues += c.theValues[i] + CONTAINER_VALUE_SPACING; } int textWidth = (int) g.getFontMetrics(g.getFont()).getStringBounds("" + allValues, g).getWidth(); // draw a rectangle to fit them all g.drawRoundRect(x, y, totalWidth, totalHeight, ARC_RADIUS, ARC_RADIUS); // print the values in it g.drawString(allValues, x + (totalWidth - textWidth) / 2, y + 3 * totalHeight / 4); }
public int getLeading(Graphics g) { if (g == null || f == null) return 0; return g.getFontMetrics(f).getLeading(); }
public int getMaxDescent(Graphics g) { if (g == null || f == null) return 0; return g.getFontMetrics(f).getMaxDescent(); }
public int getHeight(Graphics g) { if (g == null || f == null) return 0; return g.getFontMetrics(f).getHeight(); }
public int getWidth(Graphics g) { if (g == null || f == null || s.length() == 0) return 0; return g.getFontMetrics(f).stringWidth(s.toString()); }
/** * parse the text. When the text is parsed the width, height, leading are all calculated. The text * will only be truly parsed if the graphics context has changed or the text has changed or the * font has changed. Otherwise nothing is done when this method is called. * * @param g Graphics context. */ public void parseText(Graphics g) { FontMetrics fm; TextState current = new TextState(); char ch; Stack state = new Stack(); int w = 0; if (lg != g) parse = true; lg = g; if (!parse) return; parse = false; width = 0; leading = 0; ascent = 0; descent = 0; height = 0; maxAscent = 0; maxDescent = 0; if (text == null || g == null) return; list.removeAllElements(); if (font == null) current.f = g.getFont(); else current.f = font; state.push(current); list.addElement(current); fm = g.getFontMetrics(current.f); for (int i = 0; i < text.length(); i++) { ch = text.charAt(i); switch (ch) { case '$': i++; if (i < text.length()) current.s.append(text.charAt(i)); break; /* ** Push the current state onto the state stack ** and start a new storage string */ case '{': w = current.getWidth(g); if (!current.isEmpty()) { current = current.copyState(); list.addElement(current); } state.push(current); current.x += w; break; /* ** Pop the state off the state stack and set the current ** state to the top of the state stack */ case '}': w = current.x + current.getWidth(g); state.pop(); current = ((TextState) state.peek()).copyState(); list.addElement(current); current.x = w; break; case '^': w = current.getWidth(g); if (!current.isEmpty()) { current = current.copyState(); list.addElement(current); } current.f = getScriptFont(current.f); current.x += w; current.y -= (int) ((double) (current.getAscent(g)) * sup_offset + 0.5); break; case '_': w = current.getWidth(g); if (!current.isEmpty()) { current = current.copyState(); list.addElement(current); } current.f = getScriptFont(current.f); current.x += w; current.y += (int) ((double) (current.getDescent(g)) * sub_offset + 0.5); break; default: current.s.append(ch); break; } } for (int i = 0; i < list.size(); i++) { current = ((TextState) (list.elementAt(i))); if (!current.isEmpty()) { width += current.getWidth(g); ascent = Math.max(ascent, Math.abs(current.y) + current.getAscent(g)); descent = Math.max(descent, Math.abs(current.y) + current.getDescent(g)); leading = Math.max(leading, current.getLeading(g)); maxDescent = Math.max(maxDescent, Math.abs(current.y) + current.getMaxDescent(g)); maxAscent = Math.max(maxAscent, Math.abs(current.y) + current.getMaxAscent(g)); } } height = ascent + descent + leading; return; }
/** * Draw the text and graph. * * @param g */ @Override public void paintComponent(Graphics g) { locations.clear(); // Background g.setColor(background_color); g.fillRect(0, 0, getWidth(), getHeight()); // This color is used for everything until drawing the points g.setColor(foreground_color); // Anti-Aliasing Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Font FontMetrics fontMetrics = g.getFontMetrics(FONT); int fontHeight = fontMetrics.getHeight(); g.setFont(FONT); int topTextY = fontMetrics.getAscent(); // Margins int vMargin = fontHeight + MARGIN; int hMargin = MARGIN; // Calculate actual usable size double width = getWidth() - hMargin * 2; double height = getHeight() - vMargin * 2; boolean drawLowerLine = height > -vMargin; // If there is any data and no hovered entry is shown, draw current // viewercount int nowTextX = 0; if (history != null && hoverEntry == -1) { Integer viewers = history.get(endTime).getViewers(); long ago = System.currentTimeMillis() - endTime; String text; if (ago > CONSIDERED_AS_NOW) { text = "latest: " + Helper.formatViewerCount(viewers); } else { text = "now: " + Helper.formatViewerCount(viewers); } if (viewers == -1) { text = "Stream offline"; } nowTextX = getWidth() - fontMetrics.stringWidth(text); g.drawString(text, nowTextX, topTextY); } // Default text when no data is present if (history == null || history.size() < 2) { String text = "No viewer history yet"; int textWidth = fontMetrics.stringWidth(text); int y = getHeight() / 2 + fontMetrics.getDescent(); int x = (getWidth() - textWidth) / 2; boolean drawInfoText = false; if (history != null && y < topTextY + fontHeight + 4 && x + textWidth + 7 > nowTextX) { if (drawLowerLine || nowTextX > textWidth + 5) { if (drawLowerLine) { y = getHeight() - 2; } else { y = topTextY; } x = 0; drawInfoText = true; } } else { drawInfoText = true; } if (drawInfoText) { g.drawString(text, x, y); } return; } // ---------- // From here only when actual data is to be rendered // Show info on hovered entry String maxValueText = "max: " + Helper.formatViewerCount(maxValue); int maxValueEnd = fontMetrics.stringWidth(maxValueText); boolean displayMaxValue = true; if (hoverEntry != -1) { Integer viewers = history.get(hoverEntry).getViewers(); Date d = new Date(hoverEntry); String text = "Viewers: " + Helper.formatViewerCount(viewers) + " (" + sdf.format(d) + ")"; if (viewers == -1) { text = "Stream offline (" + sdf.format(d) + ")"; } int x = getWidth() - fontMetrics.stringWidth(text); if (maxValueEnd > x) { displayMaxValue = false; } g.drawString(text, x, topTextY); } String minText = "min: " + Helper.formatViewerCount(minValue); int minTextWidth = fontMetrics.stringWidth(minText); // Draw Times if (drawLowerLine) { String timeText = makeTimesText(startTime, endTime); int timeTextWidth = fontMetrics.stringWidth(timeText); int textX = getWidth() - timeTextWidth; g.drawString(timeText, textX, getHeight() - 1); if (minValue >= 1000 && timeTextWidth + minTextWidth > width) { minText = "min: " + minValue / 1000 + "k"; } } // Draw min/max if necessary if (isShowingInfo()) { if (displayMaxValue) { g.drawString(maxValueText, 0, topTextY); } if (drawLowerLine) { g.drawString(minText, 0, getHeight() - 1); } else if (maxValueEnd + minTextWidth + 29 < nowTextX) { g.drawString(minText, maxValueEnd + 10, topTextY); } } // If height available for the graph is too small, don't draw graph if (height < 5) { return; } // Calculation factors for calculating the points location int range = maxValue - minValue; if (showFullRange) { range = maxValue; } if (range == 0) { // Prevent division by zero range = 1; } double pixelPerViewer = height / range; double pixelPerTime = width / duration; // Go through all entries and calculate positions int prevX = -1; int prevY = -1; Iterator<Entry<Long, StreamInfoHistoryItem>> it = history.entrySet().iterator(); while (it.hasNext()) { Entry<Long, StreamInfoHistoryItem> entry = it.next(); // Get time and value to draw next long time = entry.getKey(); if (time < startTime || time > endTime) { continue; } long offsetTime = time - startTime; int viewers = entry.getValue().getViewers(); if (viewers == -1) { viewers = 0; } // Calculate point location int x = (int) (hMargin + offsetTime * pixelPerTime); int y; if (showFullRange) { y = (int) (-vMargin + getHeight() - (viewers) * pixelPerViewer); } else { y = (int) (-vMargin + getHeight() - (viewers - minValue) * pixelPerViewer); } // Draw connecting line if (prevX != -1) { g.drawLine(x, y, prevX, prevY); } // Save point coordinates to be able to draw the line next loop prevX = x; prevY = y; // Save point locations to draw points and to find entries on hover locations.put(new Point(x, y), time); } // Draw points (after lines, so they are in front) for (Point point : locations.keySet()) { int x = point.x; int y = point.y; long seconds = locations.get(point); StreamInfoHistoryItem historyObject = history.get(seconds); // Highlight hovered entry if (seconds == hoverEntry) { g.setColor(HOVER_COLOR); } else { // Draw offline points differently if (!historyObject.isOnline()) { g.setColor(OFFLINE_COLOR); } else { g.setColor(colors.get(seconds)); } } g.fillOval(x - POINT_SIZE / 2, y - POINT_SIZE / 2, POINT_SIZE, POINT_SIZE); } }
/** The method to call when the thread starts */ public void run() { boolean draw = true; FontMetrics fm; Rectangle r; int sw = 0; int sa = 0; int x = 0; int y = 0; setPriority(Thread.MIN_PRIORITY); while (true) { if (newmessage != null && draw) { message = newmessage; newmessage = null; } if (lg == null) { lg = g2d.getGraphics(); if (lg != null) lg = lg.create(); } if (lg != null) { if (f != null) lg.setFont(f); fm = lg.getFontMetrics(lg.getFont()); sw = fm.stringWidth(message); sa = fm.getAscent(); } else { draw = false; } if (draw) { lg.setColor(foreground); r = g2d.bounds(); x = r.x + (r.width - sw) / 2; y = r.y + (r.height + sa) / 2; lg.drawString(message, x, y); g2d.repaint(); try { sleep(visible); } catch (Exception e) { } } else { if (lg != null) { lg.setColor(g2d.getBackground()); lg.drawString(message, x, y); g2d.repaint(); } try { sleep(invisible); } catch (Exception e) { } } draw = !draw; } }
/** * 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); }
/** * @param g Graphics context. * @return the Fontmetrics the class is using. */ public FontMetrics getFM(Graphics g) { if (g == null) return null; if (font == null) return g.getFontMetrics(); else return g.getFontMetrics(font); }
/** * 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]; }
@Override public void paintComponent(Graphics g) { super.paintComponent(g); int width = getWidth() - rightMargin - leftMargin - 10; int height = getHeight() - topMargin - bottomMargin; if (width <= 0 || height <= 0) { // not enough room to paint anything return; } Color oldColor = g.getColor(); Font oldFont = g.getFont(); Color fg = getForeground(); Color bg = getBackground(); boolean bgIsLight = (bg.getRed() > 200 && bg.getGreen() > 200 && bg.getBlue() > 200); ((Graphics2D) g) .setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (smallFont == null) { smallFont = oldFont.deriveFont(9.0F); } r.x = leftMargin - 5; r.y = topMargin - 8; r.width = getWidth() - leftMargin - rightMargin; r.height = getHeight() - topMargin - bottomMargin + 16; if (border == null) { // By setting colors here, we avoid recalculating them // over and over. border = new BevelBorder( BevelBorder.LOWERED, getBackground().brighter().brighter(), getBackground().brighter(), getBackground().darker().darker(), getBackground().darker()); } border.paintBorder(this, g, r.x, r.y, r.width, r.height); // Fill background color g.setColor(bgColor); g.fillRect(r.x + 2, r.y + 2, r.width - 4, r.height - 4); g.setColor(oldColor); long tMin = Long.MAX_VALUE; long tMax = Long.MIN_VALUE; long vMin = Long.MAX_VALUE; long vMax = 1; int w = getWidth() - rightMargin - leftMargin - 10; int h = getHeight() - topMargin - bottomMargin; if (times.size > 1) { tMin = Math.min(tMin, times.time(0)); tMax = Math.max(tMax, times.time(times.size - 1)); } long viewRangeMS; if (viewRange > 0) { viewRangeMS = viewRange * MINUTE; } else { // Display full time range, but no less than a minute viewRangeMS = Math.max(tMax - tMin, 1 * MINUTE); } // Calculate min/max values for (Sequence seq : seqs) { if (seq.size > 0) { for (int i = 0; i < seq.size; i++) { if (seq.size == 1 || times.time(i) >= tMax - viewRangeMS) { long val = seq.value(i); if (val > Long.MIN_VALUE) { vMax = Math.max(vMax, val); vMin = Math.min(vMin, val); } } } } else { vMin = 0L; } if (unit == Unit.BYTES || !seq.isPlotted) { // We'll scale only to the first (main) value set. // TODO: Use a separate property for this. break; } } // Normalize scale vMax = normalizeMax(vMax); if (vMin > 0) { if (vMax / vMin > 4) { vMin = 0; } else { vMin = normalizeMin(vMin); } } g.setColor(fg); // Axes // Draw vertical axis int x = leftMargin - 18; int y = topMargin; FontMetrics fm = g.getFontMetrics(); g.drawLine(x, y, x, y + h); int n = 5; if (("" + vMax).startsWith("2")) { n = 4; } else if (("" + vMax).startsWith("3")) { n = 6; } else if (("" + vMax).startsWith("4")) { n = 4; } else if (("" + vMax).startsWith("6")) { n = 6; } else if (("" + vMax).startsWith("7")) { n = 7; } else if (("" + vMax).startsWith("8")) { n = 8; } else if (("" + vMax).startsWith("9")) { n = 3; } // Ticks ArrayList<Long> tickValues = new ArrayList<Long>(); tickValues.add(vMin); for (int i = 0; i < n; i++) { long v = i * vMax / n; if (v > vMin) { tickValues.add(v); } } tickValues.add(vMax); n = tickValues.size(); String[] tickStrings = new String[n]; for (int i = 0; i < n; i++) { long v = tickValues.get(i); tickStrings[i] = getSizeString(v, vMax); } // Trim trailing decimal zeroes. if (decimals > 0) { boolean trimLast = true; boolean removedDecimalPoint = false; do { for (String str : tickStrings) { if (!(str.endsWith("0") || str.endsWith("."))) { trimLast = false; break; } } if (trimLast) { if (tickStrings[0].endsWith(".")) { removedDecimalPoint = true; } for (int i = 0; i < n; i++) { String str = tickStrings[i]; tickStrings[i] = str.substring(0, str.length() - 1); } } } while (trimLast && !removedDecimalPoint); } // Draw ticks int lastY = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { long v = tickValues.get(i); y = topMargin + h - (int) (h * (v - vMin) / (vMax - vMin)); g.drawLine(x - 2, y, x + 2, y); String s = tickStrings[i]; if (unit == Unit.PERCENT) { s += "%"; } int sx = x - 6 - fm.stringWidth(s); if (y < lastY - 13) { if (checkLeftMargin(sx)) { // Wait for next repaint return; } g.drawString(s, sx, y + 4); } // Draw horizontal grid line g.setColor(Color.lightGray); g.drawLine(r.x + 4, y, r.x + r.width - 4, y); g.setColor(fg); lastY = y; } // Draw horizontal axis x = leftMargin; y = topMargin + h + 15; g.drawLine(x, y, x + w, y); long t1 = tMax; if (t1 <= 0L) { // No data yet, so draw current time t1 = System.currentTimeMillis(); } long tz = timeDF.getTimeZone().getOffset(t1); long tickInterval = calculateTickInterval(w, 40, viewRangeMS); if (tickInterval > 3 * HOUR) { tickInterval = calculateTickInterval(w, 80, viewRangeMS); } long t0 = tickInterval - (t1 - viewRangeMS + tz) % tickInterval; while (t0 < viewRangeMS) { x = leftMargin + (int) (w * t0 / viewRangeMS); g.drawLine(x, y - 2, x, y + 2); long t = t1 - viewRangeMS + t0; String str = formatClockTime(t); g.drawString(str, x, y + 16); // if (tickInterval > (1 * HOUR) && t % (1 * DAY) == 0) { if ((t + tz) % (1 * DAY) == 0) { str = formatDate(t); g.drawString(str, x, y + 27); } // Draw vertical grid line g.setColor(Color.lightGray); g.drawLine(x, topMargin, x, topMargin + h); g.setColor(fg); t0 += tickInterval; } // Plot values int start = 0; int nValues = 0; int nLists = seqs.size(); if (nLists > 0) { nValues = seqs.get(0).size; } if (nValues == 0) { g.setColor(oldColor); return; } else { Sequence seq = seqs.get(0); // Find starting point for (int p = 0; p < seq.size; p++) { if (times.time(p) >= tMax - viewRangeMS) { start = p; break; } } } // Optimization: collapse plot of more than four values per pixel int pointsPerPixel = (nValues - start) / w; if (pointsPerPixel < 4) { pointsPerPixel = 1; } // Draw graphs // Loop backwards over sequences because the first needs to be painted on top for (int i = nLists - 1; i >= 0; i--) { int x0 = leftMargin; int y0 = topMargin + h + 1; Sequence seq = seqs.get(i); if (seq.isPlotted && seq.size > 0) { // Paint twice, with white and with color for (int pass = 0; pass < 2; pass++) { g.setColor((pass == 0) ? Color.white : seq.color); int x1 = -1; long v1 = -1; for (int p = start; p < nValues; p += pointsPerPixel) { // Make sure we get the last value if (pointsPerPixel > 1 && p >= nValues - pointsPerPixel) { p = nValues - 1; } int x2 = (int) (w * (times.time(p) - (t1 - viewRangeMS)) / viewRangeMS); long v2 = seq.value(p); if (v2 >= vMin && v2 <= vMax) { int y2 = (int) (h * (v2 - vMin) / (vMax - vMin)); if (x1 >= 0 && v1 >= vMin && v1 <= vMax) { int y1 = (int) (h * (v1 - vMin) / (vMax - vMin)); if (y1 == y2) { // fillrect is much faster g.fillRect(x0 + x1, y0 - y1 - pass, x2 - x1, 1); } else { Graphics2D g2d = (Graphics2D) g; Stroke oldStroke = null; if (seq.transitionStroke != null) { oldStroke = g2d.getStroke(); g2d.setStroke(seq.transitionStroke); } g.drawLine(x0 + x1, y0 - y1 - pass, x0 + x2, y0 - y2 - pass); if (oldStroke != null) { g2d.setStroke(oldStroke); } } } } x1 = x2; v1 = v2; } } // Current value long v = seq.value(seq.size - 1); if (v >= vMin && v <= vMax) { if (bgIsLight) { g.setColor(seq.color); } else { g.setColor(fg); } x = r.x + r.width + 2; y = topMargin + h - (int) (h * (v - vMin) / (vMax - vMin)); // a small triangle/arrow g.fillPolygon(new int[] {x + 2, x + 6, x + 6}, new int[] {y, y + 3, y - 3}, 3); } g.setColor(fg); } } int[] valueStringSlots = new int[nLists]; for (int i = 0; i < nLists; i++) valueStringSlots[i] = -1; for (int i = 0; i < nLists; i++) { Sequence seq = seqs.get(i); if (seq.isPlotted && seq.size > 0) { // Draw current value // TODO: collapse values if pointsPerPixel >= 4 long v = seq.value(seq.size - 1); if (v >= vMin && v <= vMax) { x = r.x + r.width + 2; y = topMargin + h - (int) (h * (v - vMin) / (vMax - vMin)); int y2 = getValueStringSlot(valueStringSlots, y, 2 * 10, i); g.setFont(smallFont); if (bgIsLight) { g.setColor(seq.color); } else { g.setColor(fg); } String curValue = getFormattedValue(v, true); if (unit == Unit.PERCENT) { curValue += "%"; } int valWidth = fm.stringWidth(curValue); String legend = (displayLegend ? seq.name : ""); int legendWidth = fm.stringWidth(legend); if (checkRightMargin(valWidth) || checkRightMargin(legendWidth)) { // Wait for next repaint return; } g.drawString(legend, x + 17, Math.min(topMargin + h, y2 + 3 - 10)); g.drawString(curValue, x + 17, Math.min(topMargin + h + 10, y2 + 3)); // Maybe draw a short line to value if (y2 > y + 3) { g.drawLine(x + 9, y + 2, x + 14, y2); } else if (y2 < y - 3) { g.drawLine(x + 9, y - 2, x + 14, y2); } } g.setFont(oldFont); g.setColor(fg); } } g.setColor(oldColor); }