// ------------------------------ public void scrollToCaret() { // not called - fixed with putting visible scrollbars on JScrollPane // Rectangle rect1 = scroller1.getViewport().getViewRect(); double x1 = rect1.getX(); double y1 = rect1.getY(); double r1height = rect1.getHeight(); double r1width = rect1.getWidth(); Caret caret1 = editor1.getCaret(); Point pt2 = caret1.getMagicCaretPosition(); // the end of the string double x2 = pt2.getX(); double y2 = pt2.getY(); if (((x2 > x1) && (x2 < (x1 + r1width))) && ((y2 > y1) && (y2 < (y1 + r1height)))) { // inview } else { double newheight = r1height / 2; double newwidth = r1width / 2; double x3 = pt2.getX() - newwidth; double y3 = pt2.getY() - newheight; if (x3 < 0) x3 = 0; if (y3 < 0) y3 = 0; Rectangle rect3 = new Rectangle((int) x3, (int) y3, (int) newwidth, (int) newheight); editor1.scrollRectToVisible(rect3); } } // end scrollToCaret
/** {@inheritDoc} */ @Override public int getBaseline(JComponent c, int width, int height) { if (c == null) { throw new NullPointerException("Component must be non-null"); } if (width < 0 || height < 0) { throw new IllegalArgumentException("Width and height must be >= 0"); } AbstractButton b = (AbstractButton) c; String text = b.getText(); if (text == null || "".equals(text)) { return -1; } Insets i = b.getInsets(); Rectangle viewRect = new Rectangle(); Rectangle textRect = new Rectangle(); Rectangle iconRect = new Rectangle(); viewRect.x = i.left; viewRect.y = i.top; viewRect.width = width - (i.right + viewRect.x); viewRect.height = height - (i.bottom + viewRect.y); // layout the text and icon SynthContext context = getContext(b); FontMetrics fm = context.getComponent().getFontMetrics(context.getStyle().getFont(context)); context .getStyle() .getGraphicsUtils(context) .layoutText( context, fm, b.getText(), b.getIcon(), b.getHorizontalAlignment(), b.getVerticalAlignment(), b.getHorizontalTextPosition(), b.getVerticalTextPosition(), viewRect, iconRect, textRect, b.getIconTextGap()); View view = (View) b.getClientProperty(BasicHTML.propertyKey); int baseline; if (view != null) { baseline = BasicHTML.getHTMLBaseline(view, textRect.width, textRect.height); if (baseline >= 0) { baseline += textRect.y; } } else { baseline = textRect.y + fm.getAscent(); } return baseline; }
/** * Returns an point which has been adjusted to take into account of the desktop bounds, taskbar * and multi-monitor configuration. * * <p>This adustment may be cancelled by invoking the application with * -Djavax.swing.adjustPopupLocationToFit=false */ Point adjustPopupLocationToFitScreen(int xPosition, int yPosition) { Point popupLocation = new Point(xPosition, yPosition); if (popupPostionFixDisabled == true || GraphicsEnvironment.isHeadless()) { return popupLocation; } // Get screen bounds Rectangle scrBounds; GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation); Toolkit toolkit = Toolkit.getDefaultToolkit(); if (gc != null) { // If we have GraphicsConfiguration use it to get screen bounds scrBounds = gc.getBounds(); } else { // If we don't have GraphicsConfiguration use primary screen scrBounds = new Rectangle(toolkit.getScreenSize()); } // Calculate the screen size that popup should fit Dimension popupSize = JPopupMenu.this.getPreferredSize(); long popupRightX = (long) popupLocation.x + (long) popupSize.width; long popupBottomY = (long) popupLocation.y + (long) popupSize.height; int scrWidth = scrBounds.width; int scrHeight = scrBounds.height; if (!canPopupOverlapTaskBar()) { // Insets include the task bar. Take them into account. Insets scrInsets = toolkit.getScreenInsets(gc); scrBounds.x += scrInsets.left; scrBounds.y += scrInsets.top; scrWidth -= scrInsets.left + scrInsets.right; scrHeight -= scrInsets.top + scrInsets.bottom; } int scrRightX = scrBounds.x + scrWidth; int scrBottomY = scrBounds.y + scrHeight; // Ensure that popup menu fits the screen if (popupRightX > (long) scrRightX) { popupLocation.x = scrRightX - popupSize.width; if (popupLocation.x < scrBounds.x) { popupLocation.x = scrBounds.x; } } if (popupBottomY > (long) scrBottomY) { popupLocation.y = scrBottomY - popupSize.height; if (popupLocation.y < scrBounds.y) { popupLocation.y = scrBounds.y; } } return popupLocation; }
protected void deliverMoveResizeEvent(int x, int y, int width, int height, boolean byUser) { checkZoom(); final Rectangle oldB = nativeBounds; nativeBounds = new Rectangle(x, y, width, height); if (peer != null) { peer.notifyReshape(x, y, width, height); // System-dependent appearance optimization. if ((byUser && !oldB.getSize().equals(nativeBounds.getSize())) || isFullScreenAnimationOn) { flushBuffers(); } } }
/** * Damages the area surrounding the caret to cause it to be repainted in a new location. If * paint() is reimplemented, this method should also be reimplemented. This method should update * the caret bounds (x, y, width, and height). * * @param r the current location of the caret * @see #paint */ @Override protected synchronized void damage(final Rectangle r) { if (r == null || fPainting) return; x = r.x - 4; y = r.y; width = 10; height = r.height; // Don't damage the border area. We can't paint a partial border, so get the // intersection of the caret rectangle and the component less the border, if any. final Rectangle caretRect = new Rectangle(x, y, width, height); final Border border = getComponent().getBorder(); if (border != null) { final Rectangle alloc = getComponent().getBounds(); alloc.x = alloc.y = 0; final Insets borderInsets = border.getBorderInsets(getComponent()); alloc.x += borderInsets.left; alloc.y += borderInsets.top; alloc.width -= borderInsets.left + borderInsets.right; alloc.height -= borderInsets.top + borderInsets.bottom; Rectangle2D.intersect(caretRect, alloc, caretRect); } x = caretRect.x; y = caretRect.y; width = Math.max(caretRect.width, 1); height = Math.max(caretRect.height, 1); repaint(); }
public void move(int distance, Direction direction, boolean useFuel) { switch (direction) { case UP: location.y -= distance; break; case DOWN: location.y += distance; break; case LEFT: location.x -= distance; break; case RIGHT: location.x += distance; break; default: assert false : "Bad direction: " + location; } if (useFuel) tank.useFuel(1); }
protected void paintFocus( Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) { Rectangle focusRect = new Rectangle(); String text = b.getText(); boolean isIcon = b.getIcon() != null; // If there is text if (text != null && !text.equals("")) { if (!isIcon) { focusRect.setBounds(textRect); } else { focusRect.setBounds(iconRect.union(textRect)); } } // If there is an icon and no text else if (isIcon) { focusRect.setBounds(iconRect); } g.setColor(getFocusColor()); g.drawRect((focusRect.x - 1), (focusRect.y - 1), focusRect.width + 1, focusRect.height + 1); }
void flushBuffers() { if (isVisible() && !nativeBounds.isEmpty() && !isFullScreenMode) { try { LWCToolkit.invokeAndWait( new Runnable() { @Override public void run() { // Posting an empty to flush the EventQueue without blocking the main thread } }, target); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
public void goToLocation() { location.x = storedLocation.x; location.y = storedLocation.y; }
@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); }