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();

}

}

[5191 byte] By [samue-1a] at [2007-10-2 17:23:37]
# 1
What are you trying to do?
Happy2b1a at 2007-7-13 18:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 2

> there is a paint method and it firslt invokes the paint method which I understood nothing about.

As I've told you before, you should be overriding the paintComponent() method, not the paint() method. The tutorial will explain why.

For the last time. Download and read the [url http://java.sun.com/docs/books/tutorial/]Swing Tutorial[/url]. The download link is on the top right.

Just because you are too lazy to do some basic reading on your own doesn't mean that we have time to continually answer the most basic of questions that are all covered in the tutorial.

There is a complete section on Painting that explains everything.

As one final added bonus here is a most basic custom painting program for you to understand:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=594537

camickra at 2007-7-13 18:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 3
Hmmm,I read the tutorial that you sent. As I understood, invoking super.paint(g) in the paint method provide us to paint the content of the main Frame firstly and then paint the panel(Lightwieght component).If I am wrong, please make me correct.Thanks,Mert
samue-1a at 2007-7-13 18:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 4

Where is the section on [url http://java.sun.com/docs/books/tutorial/uiswing/14painting/practice.html]Implementing a Custom Component[/url] does it tell you to override the paint() method of the JFrame?

It specifically states that you should override the paintComponent(..) method of your component. And then the tutorial provides a working example.

camickra at 2007-7-13 18:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 5

Oh, I only wrote paint for my own sake. I did it with paintComponent method as the below code shows.

And one more question: I think there is no real difference between using paint and paintComponent method. So would you please explain the main difference ?

public void paintComponent(Graphics g){

super.paintComponent(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 );

}

}

}

}

samue-1a at 2007-7-13 18:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 6
> I think there is no real difference between using paint and paintComponent method.Read the tutorial. It explains it. What part of "do some reading on your own" and "quit being lazy" do you not understand?
camickra at 2007-7-13 18:39:51 > top of Java-index,Desktop,Core GUI APIs...