File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "RepaintManager and painting JPanel question" Watch "RepaintManager and painting JPanel question" New topic
Author

RepaintManager and painting JPanel question

ashish kulkarni
Ranch Hand

Joined: Aug 15, 2002
Posts: 130
Hi,
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
Regards
Ashish
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestRepaint extends JFrame
{
public TestRepaint()
{
super("layered pane");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(new MyPanel());
Container contentPane = this.getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
setSize(new Dimension(800,600));
this.setVisible(true);
}

class MyPanel extends JPanel
{
public MyPanel()
{
setPreferredSize(new Dimension(2000,600) );
this.setBackground(Color.white);
this.setOpaque(true);
}
/** painting is done here
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println ("Painting Back " );
g.setColor(Color.blue);
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();
}
}


A$HI$H
Nathan Pruett
Bartender

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...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RepaintManager and painting JPanel question