Hi,

I have a problem in repainting in a JPanel.

Situation:

I have a ButPanel which extends JPanel.

In this panel I draw some sequences with some text along with it, by overriding paint() method.

Eventually the screen size exceeds as input increases

I use a CompareFrame which extends JFrame.

I've added a JScrollPane named ResultPanel.

In some event I do setViewPort of ResultPanel to ButPanel.

So what i do here is, I keep counting the width and height size of ButPanel and i update the ButPanel by calling, setSize(width,height) inside paint() method.

But when the output becomes scrollable. The Graphics in ButPanel distorts or dithers over draws and etc, when I minimize and maximize, it automatically redraws[Of course repaint() is called when component minimize and maximize].

So In order to avoid the problem I add AdjustmentListener to both scrollbars of ResultPane in which I call ButPanel's repaint() from adjustmentValueChanged().

And still the problem never solved. I've attached a snapshot of my problem and the code.

please help me with a solution.

Thanks in advance,

Gokul Rangarajan

--Abstract Code Here--

public class CompareFrame extends javax.swing.JFrame {

/** Creates new form CompareFrame */
public CompareFrame() {
    initComponents();
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    setPreferredSize(new Dimension(screen.width, screen.height - 60));
    setSize(screen.width, screen.height - 60);
    BP = new ButPanel2(this);
    ResultPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {

        public void adjustmentValueChanged(AdjustmentEvent ae) {
            BP.repaint();
            BP.validate();
        }
    });
    ResultPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener()        {

        public void adjustmentValueChanged(AdjustmentEvent ae) {
            BP.repaint();
            BP.validate();
        }
    });

//Other constructor code
     }

//Other code
}

class ButPanel2 extends javax.swing.JPanel {

public ButPanel2(CompareFrame parent) {
    setLayout(new FlowLayout());
    sizeX = 900;
    sizeY = 600;
    setPreferredSize(new Dimension(sizeX, sizeY));
    setSize(sizeX, sizeY);
    this.parent = parent;
}

@Override
public void paint(Graphics G) {
    int i = 0, x = 1, y = 12;
    G.setColor(Color.red);
    int tX;
    tX = 5;
    G.setFont(new Font("Nimbus Roman No9 L", Font.PLAIN, 14));
    //SOme more code...
    .
    .
 }

 //Some more code...
}

Kindly see the attachment. It is the snapshot of my problem

Dani AI

Generated

A common root cause for the symptoms described by is an incorrect painting/layout workflow: doing layout or size changes from inside a paint method and not using the Swing painting contract makes partial repaints leave artifacts when the component is scrolled or the window is resized. A more robust pattern fixes the flicker and incorrect redraws reliably.

Key guidelines

  • Do custom drawing in paintComponent(Graphics) and let Swing clear the background first (call the superclass there).
  • Never change component size or layout inside a paint method. Update setPreferredSize(...) when the underlying content size changes, then call revalidate() and repaint() from the event thread.
  • Avoid expensive per-paint calculations. For large or complex drawings, render once into a BufferedImage (or tiled images) and draw that image in paintComponent.
  • Perform all UI changes on the Event Dispatch Thread (use SwingUtilities.invokeLater or similar).

Example pattern

class BigPanel extends JPanel {
    private BufferedImage buffer;
    private Dimension content = new Dimension(800, 1200);

    public BigPanel() {
        setPreferredSize(content);
    }

    public void setContentSize(int w, int h) {
        content.setSize(w, h);
        setPreferredSize(content);
        revalidate();    // request layout update in scrollpane
        buffer = null;   // force buffer rebuild
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (buffer == null || buffer.getWidth() != getWidth() || buffer.getHeight() != getHeight()) {
            buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = buffer.createGraphics();
            // perform expensive drawing into g2 here
            g2.dispose();
        }
        g.drawImage(buffer, 0, 0, null);
    }
}

Additional tips

  • Implement Scrollable when appropriate so the scroll pane sizes and scroll increments behave predictably.
  • Watch memory when buffering huge images; consider tiling or on-demand regions for very large content.
  • Consult the official Swing guides for the painting contract and scroll panes: Swing painting tutorial and Using scroll panes.

Hi,

i found solution.

I didn't add super.paint(G); in paint() method

Now problem solved
Thanks & regards,

Gokul Rangarajan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.