下面列出了java.awt.font.TextHitInfo#trailing ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public void init(TestEnvironment env, Result results) {
super.init(env, results);
ArrayList list = new ArrayList(text.length() * 2 + 2);
TextHitInfo hit = TextHitInfo.trailing(-1);
do {
list.add(hit);
hit = tl.getNextRightHit(hit);
} while (hit != null);
hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);
lb = tl.getBounds();
lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
}
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
// If the bug is present this will throw exception
new InputMethodEvent(component,
InputMethodEvent.CARET_POSITION_CHANGED,
TextHitInfo.leading(0),
TextHitInfo.trailing(0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
// If the bug is present this will throw exception
new InputMethodEvent(component,
InputMethodEvent.CARET_POSITION_CHANGED,
TextHitInfo.leading(0),
TextHitInfo.trailing(0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
// If the bug is present this will throw exception
new InputMethodEvent(component,
InputMethodEvent.CARET_POSITION_CHANGED,
TextHitInfo.leading(0),
TextHitInfo.trailing(0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
// If the bug is present this will throw exception
new InputMethodEvent(component,
InputMethodEvent.CARET_POSITION_CHANGED,
TextHitInfo.leading(0),
TextHitInfo.trailing(0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
// If the bug is present this will throw exception
new InputMethodEvent(component,
InputMethodEvent.CARET_POSITION_CHANGED,
TextHitInfo.leading(0),
TextHitInfo.trailing(0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}
/**
* Draws the string at the specified location underlining the specified
* character.
*
* @param c JComponent that will display the string, may be null
* @param g Graphics to draw the text to
* @param text String to display
* @param underlinedIndex Index of a character in the string to underline
* @param x X coordinate to draw the text at
* @param y Y coordinate to draw the text at
* @param useFPAPI use floating point API
*/
public static void drawStringUnderlineCharAt(JComponent c, Graphics g,
String text, int underlinedIndex,
float x, float y,
boolean useFPAPI) {
if (text == null || text.length() <= 0) {
return;
}
SwingUtilities2.drawString(c, g, text, x, y, useFPAPI);
int textLength = text.length();
if (underlinedIndex >= 0 && underlinedIndex < textLength ) {
float underlineRectY = y;
int underlineRectHeight = 1;
float underlineRectX = 0;
int underlineRectWidth = 0;
boolean isPrinting = isPrinting(g);
boolean needsTextLayout = isPrinting;
if (!needsTextLayout) {
synchronized (charsBufferLock) {
syncCharsBuffer(text);
needsTextLayout =
isComplexLayout(charsBuffer, 0, textLength);
}
}
if (!needsTextLayout) {
FontMetrics fm = g.getFontMetrics();
underlineRectX = x +
SwingUtilities2.stringWidth(c,fm,
text.substring(0,underlinedIndex));
underlineRectWidth = fm.charWidth(text.
charAt(underlinedIndex));
} else {
Graphics2D g2d = getGraphics2D(g);
if (g2d != null) {
TextLayout layout =
createTextLayout(c, text, g2d.getFont(),
g2d.getFontRenderContext());
if (isPrinting) {
float screenWidth = (float)g2d.getFont().
getStringBounds(text, getFontRenderContext(c)).getWidth();
// If text fits the screenWidth, then do not need to justify
if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(),
text) > screenWidth) {
layout = layout.getJustifiedLayout(screenWidth);
}
}
TextHitInfo leading =
TextHitInfo.leading(underlinedIndex);
TextHitInfo trailing =
TextHitInfo.trailing(underlinedIndex);
Shape shape =
layout.getVisualHighlightShape(leading, trailing);
Rectangle rect = shape.getBounds();
underlineRectX = x + rect.x;
underlineRectWidth = rect.width;
}
}
g.fillRect((int) underlineRectX, (int) underlineRectY + 1,
underlineRectWidth, underlineRectHeight);
}
}
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
// If the bug is present this will throw exception
new InputMethodEvent(component,
InputMethodEvent.CARET_POSITION_CHANGED,
TextHitInfo.leading(0),
TextHitInfo.trailing(0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
// If the bug is present this will throw exception
new InputMethodEvent(component,
InputMethodEvent.CARET_POSITION_CHANGED,
TextHitInfo.leading(0),
TextHitInfo.trailing(0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
// If the bug is present this will throw exception
new InputMethodEvent(component,
InputMethodEvent.CARET_POSITION_CHANGED,
TextHitInfo.leading(0),
TextHitInfo.trailing(0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
// If the bug is present this will throw exception
new InputMethodEvent(component,
InputMethodEvent.CARET_POSITION_CHANGED,
TextHitInfo.leading(0),
TextHitInfo.trailing(0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}