I have a problem in repainting in a JPanel.


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() {
    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) {
    ResultPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener()        {

        public void adjustmentValueChanged(AdjustmentEvent ae) {

//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;

public void paint(Graphics G) {
    int i = 0, x = 1, y = 12;
    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

Edited by Reverend Jim: Fixed formatting

Attachments Screenshot_1.png 61.89 KB Screenshot_2.png 64.9 KB
6 Years
Discussion Span
Last Post by gokul.raja


i found solution.

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

Now problem solved
Thanks & regards,

Gokul Rangarajan

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.