3

垂直スライスが線ではなくY座標として指定されている、テキスト領域の特定の垂直スライス内にあるテキストを判別しようとするコードがあります。

ちなみに、ライン数学を使用するように変換することは、この問題の優れた回避策です。これを使用しますが、Y座標しかない場合があり、このような場合が考えられます。出てくるのでとにかく聞いてみます。

私は自分の問題をかなりミニマリスト(笑Java)の例に減らしました。テキストを含むフレームを表示してから、テキスト領域の先頭に最も近いテキストの文字オフセットを決定しようとします。私たちは常識からそれが0になることを知っていますが、プログラムでそれを理解することが問題です。

public static void main(String[] args) throws Exception {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new RtlTest().run();
        }
    });
}

JFrame frame;
JTextArea textArea;

public void run() {
    textArea = new JTextArea(
        "\u05D4\u05D5\u05D3\u05E2\u05EA \u05D8\u05D9\u05D9\u05D2\u05E8 " +
        "\u05D8\u05E7\u05E1\u05D8 \u05D1\u05E2\u05D1\u05E8\u05D9\u05EA");

    frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            measure();
        }
    });
}

public void measure() {
    try {
        System.out.println("Either the line is left to right or it's right to left " +
                           "(or a mix), so one of these two values should be 0:");
        System.out.println(textArea.viewToModel(new Point(0, 0)));
        System.out.println(textArea.viewToModel(new Point(textArea.getWidth() - 1, 0)));

        Rectangle firstLetterView = textArea.modelToView(0);
        System.out.println("This one should definitely be 0, right? " +
                           "I mean, we got the coordinates from Swing itself:");
        System.out.println(textArea.viewToModel(new Point(firstLetterView.x,
                                                          firstLetterView.y)));

        frame.dispose();
    } catch (BadLocationException e) {
        throw new IllegalStateException(e);
    }
}

出力はかなり驚くべきものです:

Either the line is left to right or it's right to left (or a mix),
    so one of these two values should be 0:
23
23
This one should definitely be 0, right? I mean, we got the coordinates from Swing itself:
24

驚きのポイント:

  1. 左上隅に最も近い文字は文字0ではありません。
  2. 右上隅に最も近い文字は文字0ではありません。
  3. 文字0の位置に最も近い文字は文字0ではありません。
4

1 に答える 1

5

コードに次の行を追加します。

textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

出力番号は変更されますが、テキストはテキスト領域の右側から表示されます。これがあなたの望むものであることを願っています。

編集:

これによると、ヘブライ語とアラビア語はRT方向である必要があります。

于 2012-09-29T13:34:55.853 に答える