JPanel Network Problem

Ok, I use the flowchart editor in Java. My goal is to provide the ability to display grid lines on the surface of a drawing. I partially did this to work:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;

    // Scrolling
    g2.transform(transformation);   

    // Step one: draw grid
    if(showGrid)
    {
        // Horizontal grid lines
        for(int i = -1; i < getSize().getWidth(); i += 50)
        {
            g2.drawLine(i, 0, i, (int)getSize().getHeight());
        }
        // Vertical grid lines
        for(int i = -1; i < getSize().getHeight(); i += 50)
        {
            g2.drawLine(0, i, (int)getSize().getWidth(), i);
        }
    }

    // Step two: draw symbols
    // ...
}

The results look like this: alt text

But if I scroll the chart up or down, I get the following: alt text

As you can see, the chart scrolls, but not the grid. I also tried putting the Step one code before the line g2.transform (transform), but after that, if I scroll, the grid lines do not move.

So the question is: is there a way to draw grid lines and avoid the mentioned behavior? The goal is to scroll the grid along with the other elements in the diagram.

  • List item
+3
source share
2 answers

, .

. . . . .

, , , . getWidth getHeight, 50 .

+1

JScrollPane , .

0

Source: https://habr.com/ru/post/1784804/


All Articles