Preventing a JPanel from repainting
I know that there are many, many posts on these forums saying that repainting is a good thing for components. I have to agree with many of them. However, I have a situation where repainting a JPanel seems overkill.
I have built an applet that will read some values from a pair of files and will draw a graph from these values on a class that extends JPanel. However,due to the length of the files, the graph (and panel) are much, much larger than the size of the applet. Therefore, I am using a JScrollPane to containmy panel. The graph is drawn onto the JPanel using Graphics commands, but none of the data changes after the values are read in from the files. I amusing the JScrollPane to show the entire graph. However, whenever the JScrollPane is scrolled, the JPanel is needlessly redrawn; all the data is the same. All I want JScrollPane to do is expose different parts of the graph based on where the pane is scrolled to. Is it possible to have the data be drawn on the JPanel once (at the beginning) and to never have to draw it again by finalizing the shapes drawn on it?
Some code:
publicclass AppletOfMyEyeextends javax.swing.JApplet{
privateclass GraphPaneextends JPanelimplements Scrollable{
int graphPane1Begin;
int graphPane2Begin;
int graphPane1End;
int graphPane2End;
public GraphPane()
{
super(true);
setAutoscrolls(true);
addMouseMotionListener(this);
}
public Dimension getPreferredScrollableViewportSize()
{
return getPreferredSize();
}
publicint getScrollableBlockIncrement(Rectangle visibleRect,int orientation,int direction)
{
if(orientation == SwingConstants.VERTICAL)
return 0;
else
return 125;
}
publicboolean getScrollableTracksViewportHeight()
{
returntrue;
}
publicboolean getScrollableTracksViewportWidth()
{
returnfalse;
}
publicint getScrollableUnitIncrement(Rectangle visibleRect,int orientation,int direction)
{
if(orientation == SwingConstants.VERTICAL)
return 0;
else
return 25;
}
publicvoid paint(Graphics g)
{
int width = getWidth();
int horizontalLineEnd = width - 1;
graphPane1Begin = 0;
graphPane2Begin = graphHeight+30;
graphPane1End = graphHeight;
graphPane2End = graphPane2Begin + graphHeight;
//drawing vertical minor lines for graphs 1 and 2
for(int x = 0; x <= width; x += pBMiL)
{
g.drawLine(x, graphPane1Begin, x, graphPane1End);
g.drawLine(x, graphPane2Begin, x, graphPane2End);
}
//drawing horizontal minor lines
for(int y = graphPane1Begin; y <= graphPane1End; y += pBMiL)//for graph 1
g.drawLine(0, y, horizontalLineEnd, y);
for(int y = graphPane2Begin; y <= graphPane2End; y += pBMiL)//and graph 2
g.drawLine(0, y, horizontalLineEnd, y);
}
}
publicvoid init(){
setBackground(Color.white);
totalSamples = 20000;
APPLET_WIDTH = 1001;
APPLET_HEIGHT = 726;
resize(APPLET_WIDTH,APPLET_HEIGHT);
pBML = 25;
pBMiL = 5;
font =new Font("system", 0, 16);
scrFactor = 1.0F;
graphPane =new GraphPane();
graphView =new JScrollPane();
graphView.setBackground(new java.awt.Color(255, 255, 255));
graphView.setBorder(null);
graphView.setDoubleBuffered(true);
graphView.setPreferredSize(new java.awt.Dimension(957, 672));
graphPane.setPreferredSize(new Dimension(totalSamples, graphView.getHeight()));
graphView.setViewportView(graphPane);
}
publicvoid paint(Graphics g)
{
graphView.revalidate();
graphView.repaint();
}
privateint APPLET_WIDTH, APPLET_HEIGHT, pBML, pBMiL,totalSamples;
privatefloat scrFactor;
private Font font;
private GraphPane graphPane;
private JScrollPane graphView;
}
Everytime graphView scrolls, all of graphPane is redrawn. As the number of samples goes up (20000 is not an unusual value for totalSamples), this makes scrolling not so smooth. Is there a way to execute the paint function in graphPane once, but have it always visible?
Thanks

