Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8169719

WrappedPlainView.modelToView() should return Rectangle2D

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 9
    • None
    • client-libs

      Steps to reproduce:
      - Run the following sample on HiDPI display or with property -Dsun.java2d.uiScale=2
      ------------------
      import java.awt.FlowLayout;
      import java.awt.geom.Rectangle2D;
      import javax.swing.JEditorPane;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JTextArea;
      import javax.swing.JTextField;
      import javax.swing.SwingUtilities;
      import javax.swing.text.JTextComponent;

      public class JEditorPaneModelToView2DTest {

          public static void main(String[] args) throws Exception {
              SwingUtilities.invokeAndWait(JEditorPaneModelToView2DTest::createAndShowGUI);
          }

          private static void createAndShowGUI() {

              JFrame frame = new JFrame();
              try {
                  frame.setSize(300, 300);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                  JPanel panel = new JPanel(new FlowLayout());
                  JTextField textField = new JTextField();
                  JTextArea textArea = new JTextArea();
                  JEditorPane editorPane = new JEditorPane();

                  panel.add(textField);
                  panel.add(textArea);
                  panel.add(editorPane);

                  frame.getContentPane().add(panel);
                  frame.setVisible(true);

                  String text = "aaaa";

                  testTextComponent(textField, text);
                  testTextComponent(textArea, text);
                  testTextComponent(editorPane, text);
              } catch (Exception e) {
                  throw e;
              } finally {
                  frame.setVisible(false);
              }
          }

          static void testTextComponent(JTextComponent textComponent, String text) {
              textComponent.setText(text);

              for (int i = 0; i < text.length(); i++) {
                  try {
                      Rectangle2D rect = textComponent.modelToView2D(i);
                      System.out.println("text: '" + text.substring(0, i) + "', x: " + rect.getX());
                      if (isFloatingPointValue(rect.getX())) {
                          return;
                      }
                  } catch (Exception e) {
                      throw new RuntimeException(e);
                  }
              }

              throw new RuntimeException(textComponent.getClass() + ".modelToView2D()"
                      + " does not contain floating point coordinates!");
          }

          static boolean isFloatingPointValue(double x) {
              return x != (int) x;
          }
      }
      ------------------

      The result is:

      class javax.swing.JTextField
      text: '', x: 2.0
      text: 'a', x: 8.5
      class javax.swing.JTextArea
      text: '', x: 0.0
      text: 'a', x: 6.5
      class javax.swing.JEditorPane
      text: '', x: 3.0
      text: 'a', x: 10.0
      text: 'aa', x: 16.0
      text: 'aaa', x: 23.0
      Caused by: java.lang.RuntimeException: class javax.swing.JEditorPane.modelToView2D() does not contain floating point coordinates!

            alexsch Alexandr Scherbatiy
            alexsch Alexandr Scherbatiy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: