It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes RepaintManager and painting JPanel question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "RepaintManager and painting JPanel question" Watch "RepaintManager and painting JPanel question" New topic

RepaintManager and painting JPanel question

ashish kulkarni
Ranch Hand

Joined: Aug 15, 2002
Posts: 131
I have to develop a application where there is a larger amount of painting on the JPanel which is of size like 2000 X 1000 ,
I add this JPanel to a scrollpane and set the visible size to 800 X 600.
Then i have scrollbars so the user can scroll and see all the Panel.
My problem is when ever the user scrolls it paints all the Panel, which makes the scrolling slow and also the screen to flicker..
I came across RepaintManager which allows to paint only perticular area of a component, but did not got much detail to make it work for me
Can anyone provide with some information, tutorial or code about using RepaintManager.
I am pasting some code below it would be great if some can write a code for using RepaintManager for the panel, and also explain how it works
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestRepaint extends JFrame
public TestRepaint()
super("layered pane");
JScrollPane scrollPane = new JScrollPane(new MyPanel());
Container contentPane = this.getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
setSize(new Dimension(800,600));

class MyPanel extends JPanel
public MyPanel()
setPreferredSize(new Dimension(2000,600) );
/** painting is done here
public void paintComponent(Graphics g)
System.out.println ("Painting Back " );
int x = 0;
for(int i = 0; i < 60; i++)
x +=60;
g.drawLine(x, 0, x, 600);
public static void main(String args[])
new TestRepaint();

Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Check out the How to Use Scroll Panes section of the Swing tutorial... it'll show you what to do...

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: RepaintManager and painting JPanel question
It's not a secret anymore!