下面列出了javax.swing.text.JTextComponent#repaint ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Set new caret bounds while repainting both original and new bounds.
* Clear the caret-painted and update-caret-bounds flags.
*
* @param newCaretBounds non-null new bounds
*/
synchronized Rectangle setCaretBoundsWithRepaint(Rectangle newCaretBounds, JTextComponent c, String logMessage, int logIndex) {
Rectangle oldCaretBounds = this.caretBounds;
boolean repaintOld = (oldCaretBounds != null && (this.statusBits & CARET_PAINTED) != 0);
this.statusBits &= ~(CARET_PAINTED | UPDATE_CARET_BOUNDS);
boolean log = EditorCaret.LOG.isLoggable(Level.FINE);
if (repaintOld) {
if (log) {
logRepaint(logMessage + "-setBoundsRepaint-repaintOld", logIndex, oldCaretBounds);
}
c.repaint(oldCaretBounds); // First schedule repaint of the original bounds (even if new bounds will possibly be the same)
}
if (!repaintOld || !newCaretBounds.equals(oldCaretBounds)) {
if (log) {
logRepaint(logMessage + "-setBoundsRepaint-repaintNew", logIndex, newCaretBounds);
}
c.repaint(newCaretBounds);
}
this.caretBounds = newCaretBounds;
return oldCaretBounds;
}
@Override public void updateUI() {
super.updateUI();
setOpaque(false);
Caret caret = new DefaultCaret() {
// [UnsynchronizedOverridesSynchronized]
// Unsynchronized method damage overrides synchronized method in DefaultCaret
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
@Override protected synchronized void damage(Rectangle r) {
if (Objects.nonNull(r)) {
JTextComponent c = getComponent();
x = 0;
y = r.y;
width = c.getSize().width;
height = r.height;
c.repaint();
}
}
};
// caret.setBlinkRate(getCaret().getBlinkRate());
caret.setBlinkRate(UIManager.getInt("TextArea.caretBlinkRate"));
setCaret(caret);
}
public static void setHighlight(JTextComponent jtc, String pattern, HighlightPainter painter) {
Highlighter highlighter = jtc.getHighlighter();
highlighter.removeAllHighlights();
Document doc = jtc.getDocument();
try {
String text = doc.getText(0, doc.getLength());
Matcher matcher = Pattern.compile(pattern).matcher(text);
int pos = 0;
while (matcher.find(pos) && !matcher.group().isEmpty()) {
int start = matcher.start();
int end = matcher.end();
highlighter.addHighlight(start, end, painter);
pos = end;
}
} catch (BadLocationException | PatternSyntaxException ex) {
UIManager.getLookAndFeel().provideErrorFeedback(jtc);
}
jtc.repaint();
}
@Override public void updateUI() {
super.updateUI();
Caret caret = new DefaultCaret() {
// [UnsynchronizedOverridesSynchronized]
// Unsynchronized method damage overrides synchronized method in DefaultCaret
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
@Override protected synchronized void damage(Rectangle r) {
if (Objects.nonNull(r)) {
JTextComponent c = getComponent();
x = 0;
y = r.y;
width = c.getSize().width;
height = r.height;
c.repaint();
}
}
};
// caret.setBlinkRate(getCaret().getBlinkRate());
caret.setBlinkRate(UIManager.getInt("TextArea.caretBlinkRate"));
setCaret(caret);
}
/**
* Fetches the previous caret location, stores the current caret location,
* If the caret is on another line, repaint the previous line and the current line
*
* @param c the text component
*/
private static void currentLineChanged(JTextComponent c) {
try {
int previousCaret = ((Integer) c.getClientProperty(PREVIOUS_CARET)).intValue();
Rectangle prev = c.modelToView(previousCaret);
Rectangle r = c.modelToView(c.getCaretPosition());
c.putClientProperty(PREVIOUS_CARET, new Integer(c.getCaretPosition()));
if ((prev != null) && (prev.y != r.y)) {
c.repaint(0, prev.y, c.getWidth(), r.height);
c.repaint(0, r.y, c.getWidth(), r.height);
}
} catch (BadLocationException e) {
// ignore
}
}
static void propertyChange( JTextComponent c, PropertyChangeEvent e ) {
switch( e.getPropertyName() ) {
case FlatClientProperties.PLACEHOLDER_TEXT:
case FlatClientProperties.COMPONENT_ROUND_RECT:
c.repaint();
break;
case FlatClientProperties.MINIMUM_WIDTH:
c.revalidate();
break;
}
}
/**
* Scrolls the specified text component so the text between the starting and ending index are visible.
*/
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
try {
Rectangle startingRectangle = textComponent.modelToView(startingIndex);
Rectangle endDingRectangle = textComponent.modelToView(endingIndex);
Rectangle totalBounds = startingRectangle.union(endDingRectangle);
textComponent.scrollRectToVisible(totalBounds);
textComponent.repaint();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Scrolls the specified text component so the text between the starting and ending index are visible.
*/
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
try {
Rectangle startingRectangle = textComponent.modelToView(startingIndex);
Rectangle endDingRectangle = textComponent.modelToView(endingIndex);
Rectangle totalBounds = startingRectangle.union(endDingRectangle);
textComponent.scrollRectToVisible(totalBounds);
textComponent.repaint();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
private void updateRectangularSelectionPaintRect() {
// Repaint current rect
JTextComponent c = component;
Rectangle repaintRect = rsPaintRect;
if (rsDotRect == null || rsMarkRect == null) {
return;
}
Rectangle newRect = new Rectangle();
if (rsDotRect.x < rsMarkRect.x) { // Swap selection to left
newRect.x = rsDotRect.x; // -1 to make the visual selection non-empty
newRect.width = rsMarkRect.x - newRect.x;
} else { // Extend or shrink on right
newRect.x = rsMarkRect.x;
newRect.width = rsDotRect.x - newRect.x;
}
if (rsDotRect.y < rsMarkRect.y) {
newRect.y = rsDotRect.y;
newRect.height = (rsMarkRect.y + rsMarkRect.height) - newRect.y;
} else {
newRect.y = rsMarkRect.y;
newRect.height = (rsDotRect.y + rsDotRect.height) - newRect.y;
}
if (newRect.width < 2) {
newRect.width = 2;
}
rsPaintRect = newRect;
// Repaint merged region with original rect
if (repaintRect == null) {
repaintRect = rsPaintRect;
} else {
repaintRect = repaintRect.union(rsPaintRect);
}
c.repaint(repaintRect);
updateRectangularSelectionPositionBlocks();
}
synchronized Rectangle repaint(JTextComponent c, String logMessage, int logIndex) {
Rectangle bounds = this.caretBounds;
if (bounds != null) {
this.statusBits &= ~CARET_PAINTED;
if (EditorCaret.LOG.isLoggable(Level.FINE)) {
logRepaint(logMessage, logIndex, bounds);
}
c.repaint(bounds);
}
return bounds;
}
/**
* Repaint caret bounds if the caret is showing or do nothing
* @param c
* @return
*/
synchronized Rectangle repaintIfShowing(JTextComponent c, String logMessage, int logIndex) {
Rectangle bounds = this.caretBounds;
if (bounds != null) {
boolean repaint = (this.statusBits & CARET_PAINTED) != 0;
if (repaint) {
this.statusBits &= ~CARET_PAINTED;
if (EditorCaret.LOG.isLoggable(Level.FINE)) {
logRepaint(logMessage + "-repaintIfShowing", logIndex, bounds);
}
c.repaint(bounds);
}
}
return bounds;
}
private void updateRectangularSelectionPaintRect() {
// Repaint current rect
JTextComponent c = component;
Rectangle repaintRect = rsPaintRect;
if (rsDotRect == null || rsMarkRect == null) {
return;
}
Rectangle newRect = new Rectangle();
if (rsDotRect.x < rsMarkRect.x) { // Swap selection to left
newRect.x = rsDotRect.x; // -1 to make the visual selection non-empty
newRect.width = rsMarkRect.x - newRect.x;
} else { // Extend or shrink on right
newRect.x = rsMarkRect.x;
newRect.width = rsDotRect.x - newRect.x;
}
if (rsDotRect.y < rsMarkRect.y) {
newRect.y = rsDotRect.y;
newRect.height = (rsMarkRect.y + rsMarkRect.height) - newRect.y;
} else {
newRect.y = rsMarkRect.y;
newRect.height = (rsDotRect.y + rsDotRect.height) - newRect.y;
}
if (newRect.width < 2) {
newRect.width = 2;
}
rsPaintRect = newRect;
// Repaint merged region with original rect
if (repaintRect == null) {
repaintRect = rsPaintRect;
} else {
repaintRect = repaintRect.union(rsPaintRect);
}
c.repaint(repaintRect);
updateRectangularSelectionPositionBlocks();
}
/**
* Scrolls the specified text component so the text between the starting and ending index are visible.
*/
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
try {
Rectangle startingRectangle = textComponent.modelToView(startingIndex);
Rectangle endDingRectangle = textComponent.modelToView(endingIndex);
Rectangle totalBounds = startingRectangle.union(endDingRectangle);
textComponent.scrollRectToVisible(totalBounds);
textComponent.repaint();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Scrolls the specified text component so the text between the starting and ending index are visible.
*/
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
try {
Rectangle startingRectangle = textComponent.modelToView(startingIndex);
Rectangle endDingRectangle = textComponent.modelToView(endingIndex);
Rectangle totalBounds = startingRectangle.union(endDingRectangle);
textComponent.scrollRectToVisible(totalBounds);
textComponent.repaint();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
@Override protected synchronized void damage(Rectangle r) {
if (Objects.nonNull(r)) {
JTextComponent c = getComponent();
x = r.x;
y = r.y;
// width = c.getFontMetrics(c.getFont()).charWidth('w');
// width = c.getFontMetrics(c.getFont()).charWidth('\u3042');
width = c.getFontMetrics(c.getFont()).charWidth('あ');
height = r.height;
c.repaint();
}
}