protected void paintLineHighlight(Graphics gfx, int line, int y) { int height = fm.getHeight(); y += fm.getLeading() + fm.getMaxDescent(); int selectionStart = textArea.getSelectionStart(); int selectionEnd = textArea.getSelectionStop(); if (selectionStart == selectionEnd) { if (lineHighlight) { gfx.setColor(lineHighlightColor); gfx.fillRect(0, y, getWidth(), height); } } else { gfx.setColor(selectionColor); int selectionStartLine = textArea.getSelectionStartLine(); int selectionEndLine = textArea.getSelectionStopLine(); int lineStart = textArea.getLineStartOffset(line); int x1, x2; if (textArea.isSelectionRectangular()) { int lineLen = textArea.getLineLength(line); x1 = textArea._offsetToX( line, Math.min( lineLen, selectionStart - textArea.getLineStartOffset(selectionStartLine))); x2 = textArea._offsetToX( line, Math.min(lineLen, selectionEnd - textArea.getLineStartOffset(selectionEndLine))); if (x1 == x2) x2++; } else if (selectionStartLine == selectionEndLine) { x1 = textArea._offsetToX(line, selectionStart - lineStart); x2 = textArea._offsetToX(line, selectionEnd - lineStart); } else if (line == selectionStartLine) { x1 = textArea._offsetToX(line, selectionStart - lineStart); x2 = getWidth(); } else if (line == selectionEndLine) { // x1 = 0; // hack from stendahl to avoid doing weird side selection thing x1 = textArea._offsetToX(line, 0); // attempt at getting the gutter too, but doesn't seem to work // x1 = textArea._offsetToX(line, -textArea.getHorizontalOffset()); x2 = textArea._offsetToX(line, selectionEnd - lineStart); } else { // x1 = 0; // hack from stendahl to avoid doing weird side selection thing x1 = textArea._offsetToX(line, 0); // attempt at getting the gutter too, but doesn't seem to work // x1 = textArea._offsetToX(line, -textArea.getHorizontalOffset()); x2 = getWidth(); } // "inlined" min/max() gfx.fillRect(x1 > x2 ? x2 : x1, y, x1 > x2 ? (x1 - x2) : (x2 - x1), height); } }
protected void paintCaret(Graphics gfx, int line, int y) { // System.out.println("painting caret " + line + " " + y); if (textArea.isCaretVisible()) { // System.out.println("caret is visible"); int offset = textArea.getCaretPosition() - textArea.getLineStartOffset(line); int caretX = textArea._offsetToX(line, offset); int caretWidth = ((blockCaret || textArea.isOverwriteEnabled()) ? fm.charWidth('w') : 1); y += fm.getLeading() + fm.getMaxDescent(); int height = fm.getHeight(); // System.out.println("caretX, width = " + caretX + " " + caretWidth); gfx.setColor(caretColor); if (textArea.isOverwriteEnabled()) { gfx.fillRect(caretX, y + height - 1, caretWidth, 1); } else { // some machines don't like the drawRect for the single // pixel caret.. this caused a lot of hell because on that // minority of machines, the caret wouldn't show up past // the first column. the fix is to use drawLine() in // those cases, as a workaround. if (caretWidth == 1) { gfx.drawLine(caretX, y, caretX, y + height - 1); } else { gfx.drawRect(caretX, y, caretWidth - 1, height - 1); } // gfx.drawRect(caretX, y, caretWidth, height - 1); } } }
public void doPaint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.black); BufferedImage bimg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB); Graphics ig = bimg.getGraphics(); Color alphared = new Color(255, 0, 0, 128); Color alphagreen = new Color(0, 255, 0, 128); Color alphablue = new Color(0, 0, 255, 128); ig.setColor(alphared); ig.fillRect(0, 0, 200, 200); ig.setColor(alphagreen); ig.fillRect(25, 25, 150, 150); ig.setColor(alphablue); ig.fillRect(75, 75, 125, 125); g.drawImage(bimg, 10, 25, this); GradientPaint gp = new GradientPaint(10.0f, 10.0f, alphablue, 210.0f, 210.0f, alphared, true); g2.setPaint(gp); g2.fillRect(10, 240, 200, 200); }
/** * Repaints the text. * * @param gfx The graphics context */ public void paint(Graphics gfx) { Graphics2D g2 = (Graphics2D) gfx; g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, antialias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); tabSize = fm.charWidth(' ') * ((Integer) textArea.getDocument().getProperty(PlainDocument.tabSizeAttribute)) .intValue(); Rectangle clipRect = gfx.getClipBounds(); gfx.setColor(getBackground()); gfx.fillRect(clipRect.x, clipRect.y, clipRect.width, clipRect.height); // We don't use yToLine() here because that method doesn't // return lines past the end of the document int height = fm.getHeight(); int firstLine = textArea.getFirstLine(); int firstInvalid = firstLine + clipRect.y / height; // Because the clipRect's height is usually an even multiple // of the font height, we subtract 1 from it, otherwise one // too many lines will always be painted. int lastInvalid = firstLine + (clipRect.y + clipRect.height - 1) / height; try { TokenMarker tokenMarker = textArea.getDocument().getTokenMarker(); int x = textArea.getHorizontalOffset(); for (int line = firstInvalid; line <= lastInvalid; line++) { paintLine(gfx, tokenMarker, line, x); } if (tokenMarker != null && tokenMarker.isNextLineRequested()) { int h = clipRect.y + clipRect.height; repaint(0, h, getWidth(), getHeight() - h); } } catch (Exception e) { System.err.println( "Error repainting line" + " range {" + firstInvalid + "," + lastInvalid + "}:"); e.printStackTrace(); } }