Forums Register Login

RepaintManager and painting JPanel question

+Pie Number of slices to send: Send
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();
}
}
+Pie Number of slices to send: Send
Check out the How to Use Scroll Panes section of the Swing tutorial... it'll show you what to do...
this is supposed to be a surprise, but it smells like a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 973 times.
Similar Threads
Problem with custom painting and multiple JPanel
repaint()????
jpane with scrollpane is not displaying
A question about JScrollPane. Thanks in advance!
Drawing with mouse
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:11:40.