Java Code Examples for java.awt.font.TextHitInfo#leading()

The following examples show how to use java.awt.font.TextHitInfo#leading() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JTextComponent.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 2
Source File: JTextComponent.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 3
Source File: JTextComponent.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 4
Source File: JTextComponent.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 5
Source File: JTextComponent.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 6
Source File: JTextComponent.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 7
Source File: JTextComponent.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 8
Source File: JTextComponent.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 9
Source File: JTextComponent.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 10
Source File: JTextComponent.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 11
Source File: JTextComponent.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public TextHitInfo getLocationOffset(int x, int y) {
    if (composedTextAttribute == null) {
        return null;
    } else {
        Point p = getLocationOnScreen();
        p.x = x - p.x;
        p.y = y - p.y;
        int pos = viewToModel(p);
        if ((pos >= composedTextStart.getOffset()) &&
            (pos <= composedTextEnd.getOffset())) {
            return TextHitInfo.leading(pos - composedTextStart.getOffset());
        } else {
            return null;
        }
    }
}
 
Example 12
Source File: bug8041990.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 13
Source File: bug8041990.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 14
Source File: bug8041990.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 15
Source File: bug8041990.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 16
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example 17
Source File: bug8041990.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 18
Source File: bug8041990.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 19
Source File: bug8041990.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 20
Source File: bug8041990.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}