Scrolling gives rubbish on the screen.

I have a dataset that contains more data (e.g. 3000 points) than the horizontal screen resolution (e.g. 1024 pixels).

I am able to create a JPanel with a width of 3000 pixels that is placed on a JScrollPane (with a horizontal scroll-bar).

The graph is draw correctly (first 1024 points), but when I use the horizontal scroll-bar to see the rest of the datapoints, the graph gets corrupted and the screen displays rubbish.

It seems that nothing is draw on the not visable part of the JPanel.

Any idea how to solve this?

[549 byte] By [W@ltera] at [2007-9-23]
# 1
You need to override paintComponent(Graphics g) instead of paint(), or whatever method you've chosen to use.
jvaudrya at 2007-7-10 > top of java,Security,Cryptography...
# 2
I did override the paint(...) method but changing it to paintComponent(...) doesn't change anything.The line's are draw good, but scrolling gives a rubbage screen.How to solve this?
W@ltera at 2007-7-10 > top of java,Security,Cryptography...
# 3

This is a part of my code:

public class Paint1000 extends JPanel {

...

Paint1000() {

this.setPreferredSize(new Dimension(HOR_PIXELS, this.getHeight()));

JScrollPane scrollPane = new JScrollPane(this,ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

JFrame frame = new JFrame();

frame.setBackground(Color.WHITE);

frame.add(scrollPane);

frame.pack();

frame.setSize(getScreenResolution());

frame.setVisible(true);

}

@Override

public void paintComponent(final Graphics graphics) {

Graphics2D graphics2D = (Graphics2D) graphics;

graphics2D.setPaint(Color.BLUE);

...

graphics2D.draw(new Line2D.Float(x0, y0, x1, y1));

...

}

...

W@ltera at 2007-7-10 > top of java,Security,Cryptography...
# 4

Thanks for finally posting some code. Try this:

protected void paintComponent(Graphics graphics) {

super.paintComponent(graphics); //<<--

//your code here...

}

Why did you make graphics final?

DrLaszloJamfa at 2007-7-10 > top of java,Security,Cryptography...
# 5

Thank you for your reply.

I was browsing the Swing and AWT forum here and found code with a call to the super class and just tried it, that did the trick. Thank you.

Wanted to post the solution, but you are quicker ;-)

I made my method protected instead of public.

I want to write code that reflects my intention. So by using final I tell the compiler that I am not going to change the parameter (I know all params are pass by value). And if I made a mistake by changing or assigning the parameter the compiler will them me that.

Oke?

W@ltera at 2007-7-10 > top of java,Security,Cryptography...
# 6

> Oke?

Okie. I thought you might be using an anonymous class:

protected void paintComponent(final Graphics graphics) {

super.paintComponent(graphics);

Runnable r = new Runnable() {

public void run() {

... graphics ...

}

};

SwingUtilities.invokeLater(r);

}

This would cause problems of its own, because the caller is disposing graphics...

DrLaszloJamfa at 2007-7-10 > top of java,Security,Cryptography...