Не удается синхронизировать с представлением после заполнения прямоугольника строки в JTextPane java swing - PullRequest
0 голосов
/ 25 июня 2018

У меня проблемы с выделением конкретной строки в jtextpane java swing. У меня есть основной класс с именем Solution и Modified LinePainter, который я нашел в Интернете. Когда вызывается функция sl (), она рисует конкретную строку. это связано с вызовом метода рисования. Я напечатал пустые строки методом paint . эта пустышка часто печатается. Это означает, что метод рисования вызывается после заданного периода. после запуска кода я обнаружил, что всякий раз, когда я максимизирую или минимизирую окно, тогда только оно показывает правильную выделенную строку. Я хочу, чтобы всякий раз, когда я вызываю функцию sl (sl означает, что установленная строка означает выделенный номер строки, который передается). Что должно измениться или узнать? Спасибо за чтение.

Solution.java файл

import javax.swing.*;
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextPane jTextPane=new JTextPane();
        jTextPane.setText("abc\nbcd\nmm\njjjjj");
        LinePainter linePainter=new LinePainter(jTextPane);
        Scanner sc=new Scanner(System.in);
        f.add(jTextPane);
        f.setSize(250,250);
        f.setVisible(true);
        int x=sc.nextInt();
        linePainter.sl(2);
        x=sc.nextInt();
        linePainter.sl(3);
    }
}

Файл LinePainter.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

/*
 *  Track the movement of the Caret by painting a background line at the
 *  current caret position.
 */
public class LinePainter
        implements Highlighter.HighlightPainter, CaretListener, MouseListener, MouseMotionListener
{
    private JTextComponent component;

    private Color color;

    private Rectangle lastView;

    /*
     *  The line color will be calculated automatically by attempting
     *  to make the current selection lighter by a factor of 1.2.
     *
     *  @param component  text component that requires background line painting
     */
    public LinePainter(JTextComponent component)
    {
        this(component, null);
        setLighter(component.getSelectionColor());
    }

    /*
     *  Manually control the line color
     *
     *  @param component  text component that requires background line painting
     *  @param color      the color of the background line
     */
    public LinePainter(JTextComponent component, Color color)
    {
        this.component = component;
        setColor( color );

        //  Add listeners so we know when to change highlighting

        component.addCaretListener( this );
        component.addMouseListener( this );
        component.addMouseMotionListener( this );

        //  Turn highlighting on by adding a dummy highlight

        try
        {
            component.getHighlighter().addHighlight(0, 0, this);
        }
        catch(BadLocationException ble) {}
    }

    /*
     *  You can reset the line color at any time
     *
     *  @param color  the color of the background line
     */
    public void setColor(Color color)
    {
        this.color = color;
    }

    /*
     *  Calculate the line color by making the selection color lighter
     *
     *  @return the color of the background line
     */
    public void setLighter(Color color)
    {
        int red   = Math.min(255, (int)(color.getRed() * 1.2));
        int green = Math.min(255, (int)(color.getGreen() * 1.2));
        int blue  = Math.min(255, (int)(color.getBlue() * 1.2));
        setColor(new Color(red, green, blue));
    }
    public int ln=1;
    public void sl(int l) { ln=l;
        System.out.println("hii"); }
    //  Paint the background highlight

    public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c)
    {
        try
        {
            System.out.println("calling paint");
//            resetHighlight();
            Rectangle r = c.modelToView((c).getDocument().getDefaultRootElement().getElement(ln-1).getStartOffset());
            g.setColor( color );
//            if(lastView != null){
//                g.clearRect(0,lastView.y,c.getWidth(),lastView.height);
//            }
            g.fillRect(0, r.y, c.getWidth(), r.height);

//            if (lastView == null)
//                lastView = r;
        }
        catch(BadLocationException ble) {System.out.println(ble);}
    }

    /*
     *   Caret position has changed, remove the highlight
     */
    private void resetHighlight()
    {
        System.out.println("reset");
        //  Use invokeLater to make sure updates to the Document are completed,
        //  otherwise Undo processing causes the modelToView method to loop.

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {

                    Rectangle currentView = component.modelToView(component.getDocument().getDefaultRootElement().getElement(ln-1).getStartOffset());;

                    //  Remove the highlighting from the previously highlighted line

                    if (lastView.y != currentView.y)
                    {
                        component.repaint(0, lastView.y, component.getWidth(), lastView.height);
                        lastView = currentView;
                    }
                }
                catch(BadLocationException ble) {}
            }
        });
    }

    //  Implement CaretListener

    public void caretUpdate(CaretEvent e)
    {
//        resetHighlight();
    }

    //  Implement MouseListener

    public void mousePressed(MouseEvent e)
    {
//        resetHighlight();
    }

    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}

    //  Implement MouseMotionListener

    public void mouseDragged(MouseEvent e)
    {
//        resetHighlight();
    }

    public void mouseMoved(MouseEvent e) {}
}

UPDATE:

Я обновил свой файл LinePainter следующим образом.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

/*
 *  Track the movement of the Caret by painting a background line at the
 *  current caret position.
 */
public class LinePainter
        implements Highlighter.HighlightPainter, CaretListener, MouseListener, MouseMotionListener
{
    private JTextComponent component;

    private Color color;

    private Rectangle lastView;

    /*
     *  The line color will be calculated automatically by attempting
     *  to make the current selection lighter by a factor of 1.2.
     *
     *  @param component  text component that requires background line painting
     */
    public LinePainter(JTextComponent component)
    {
        this(component, null);
        setLighter(component.getSelectionColor());
    }

    /*
     *  Manually control the line color
     *
     *  @param component  text component that requires background line painting
     *  @param color      the color of the background line
     */
    public LinePainter(JTextComponent component, Color color)
    {
        this.component = component;
        setColor( color );

        //  Add listeners so we know when to change highlighting

        component.addCaretListener( this );
        component.addMouseListener( this );
        component.addMouseMotionListener( this );

        //  Turn highlighting on by adding a dummy highlight

        try
        {
            component.getHighlighter().addHighlight(0, 0, this);
        }
        catch(BadLocationException ble) {}
    }

    /*
     *  You can reset the line color at any time
     *
     *  @param color  the color of the background line
     */
    public void setColor(Color color)
    {
        this.color = color;
    }

    /*
     *  Calculate the line color by making the selection color lighter
     *
     *  @return the color of the background line
     */
    public void setLighter(Color color)
    {
        int red   = Math.min(255, (int)(color.getRed() * 1.2));
        int green = Math.min(255, (int)(color.getGreen() * 1.2));
        int blue  = Math.min(255, (int)(color.getBlue() * 1.2));
        setColor(new Color(red, green, blue));
    }
    public int ln=1,prev=1;
    public void sl(int l) { prev=ln; ln=l;
        System.out.println("hii");
        //resetHighlight();
    }
    //  Paint the background highlight

    public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c)
    {
        try
        {
//            System.out.println("calling paint");
//            resetHighlight();
            if(prev != ln){
                Rectangle rect=c.modelToView((c).getDocument().getDefaultRootElement().getElement(prev-1).getStartOffset());
               prev=ln;
                System.out.println(rect.y+" " +c.getWidth()+" "+rect.height);
                g.clearRect(0,rect.y,c.getWidth(),rect.height);
            }
            Rectangle r = c.modelToView((c).getDocument().getDefaultRootElement().getElement(ln-1).getStartOffset());
            g.setColor( color );
//            if(lastView != null){
//                g.clearRect(0,lastView.y,c.getWidth(),lastView.height);
//            }
            System.out.println(r.y+" " +c.getWidth()+" "+r.height);
            g.fillRect(0, r.y, c.getWidth(), r.height);

            if (lastView == null)
                lastView = r;
        }
        catch(BadLocationException ble) {System.out.println(ble);}
    }

    /*
     *   Caret position has changed, remove the highlight
     */
    private void resetHighlight()
    {
        System.out.println("reset");
        //  Use invokeLater to make sure updates to the Document are completed,
        //  otherwise Undo processing causes the modelToView method to loop.

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    System.out.println("prev line "+prev+" "+ln);
                    Rectangle currentView = component.modelToView(component.getDocument().getDefaultRootElement().getElement(prev-1).getStartOffset());
                    //  Remove the highlighting from the previously highlighted line
                    if (lastView.y != currentView.y)
                    {
                        component.repaint(0, lastView.y, component.getWidth(), lastView.height);
                        lastView = currentView;
                    }
                    prev=ln;
                }
                catch(BadLocationException ble) {}
            }
        });
    }

    //  Implement CaretListener

    public void caretUpdate(CaretEvent e)
    {
//        resetHighlight();
    }

    //  Implement MouseListener

    public void mousePressed(MouseEvent e)
    {
//        resetHighlight();
    }

    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}

    //  Implement MouseMotionListener

    public void mouseDragged(MouseEvent e)
    {
//        resetHighlight();
    }

    public void mouseMoved(MouseEvent e) {}
}

все еще я не получаю желаемый результат. выделение не обновляется после вызова sl (). Если какие-либо предложения, пожалуйста, сообщите мне. Спасибо .....

1 Ответ

0 голосов
/ 25 июня 2018

Похоже, вы получили код от Line Painter .

Ключом к этому коду является метод resetHighlight().Этот метод вызывается каждый раз, когда изменяется позиция каретки, поэтому выделение можно нарисовать на новой строке.

Вы закомментировали все вызовы этого метода.

Так что я бы подумалчто вам нужно сделать две вещи:

  1. ваш метод sl (...) должен вызвать метод resetHighlight ().
  2. вам необходимо изменить метод resetHighlight (), чтобы вычислить прямоугольник, который нужно перекрасить, на основе номера строки вместо позиции каретки.

    linePainter.sl(2);
    x=sc.nextInt();
    linePainter.sl(3);
    

Не уверен, в чем смысл этого кода.LinePainter будет рисовать только одну строку за раз, поэтому при двойном вызове метода будет выделена третья строка.

...