paint method
Hi,
For the below codes, there is a paint method and it firslt invokes the paint method which I understood nothing about.
Why do we call super.paint(g) method ?
In spite of calling it, when I make my panel go upside , then my graph began to spoil (erase). So how can I prevent it ?
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
class Op2extends JFrame{
// properties
Container c;
JSlider s2;
JSlider s3;
int a1, b1 ;
PanelDraw pn;
JScrollPane scrollPane;
// constructors
public Op2(){
setSize(600,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pn =new PanelDraw();
c = getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
scrollPane =new JScrollPane(pn);
scrollPane.setPreferredSize(new Dimension(450,110));
c.add(scrollPane);
c.add(new Panel2());
c.add(new Panel1());
pack();
setVisible(true);
}
class PanelDrawextends JPanel{
public PanelDraw(){
setPreferredSize(new Dimension(800,800));
}
// methods
publicvoid paint(Graphics g){
super.paint(g);
int b;
g.drawLine(450,0,450,450);
g.drawLine(450,450,250,450);
g.drawLine(450,450,650,450);
double x1,y1,x2,y2;
for(int a = 1; a < 22 ; a++){
b = -a;
if(a1 != 0){
x1 = b * 3 / a1 + 450;
y1 = 450-(b*b / a1*a1) + b1 * 1.10 ;
x2 = b*3 / a1 + 449;
y2 = 450- ( (b - 1)*(b - 1) ) / a1*a1 + b1* 1.10;
g.drawLine( (int)x1,(int)y1 ,(int)x2, (int)y2 );
x1 = a *3 / a1 + 450;
y1 = 450-(a*a / a1*a1) + b1 * 1.10;
x2 = (a + 1)*3 / a1 + 450;
y2 = 450- ( (a-1)*(a - 1) ) / a1*a1 + b1* 1.10 ;
g.drawLine( (int)x1,(int)y1 ,(int)x2, (int)y2 );
}
}
}
}
class Panel1extends JPanel{
public Panel1(){
s3 =new JSlider();
s3.addChangeListener(new ChangePoints());
add(s3);
setVisible(true);
}
}
class Panel2extends JPanel{
public Panel2(){
s2 =new JSlider();
s2.addChangeListener(new ChangePoints());
add(s2);
setVisible(true);
}
}
class ChangePointsimplements ChangeListener{
publicvoid stateChanged(ChangeEvent e){
a1 = s2.getValue();
b1 = s3.getValue();
pn.paint(getGraphics() );
}
}
publicstaticvoid main(String args[]){
new Op2();
}
}

