• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RepaintManager and painting JPanel question

 
ashish kulkarni
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the How to Use Scroll Panes section of the Swing tutorial... it'll show you what to do...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic