3 Replies - 26985 Views - Last Post: 11 January 2009 - 01:59 PM Rate Topic: -----

#1 bastones  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 34
  • Joined: 03-January 09

What does the super.paintComponent(g) do?

Posted 11 January 2009 - 01:07 PM

When I am extending the JPanel I have to super.paintComponent(g) - I know that it is required because a panel like JPanel is opaque but what is it really used for? I don't understand that the code itself paints the panel with the background colour - how does it lose it in the first place? Is there a method in JComponent that the JPanel inherits from JComponent class that makes the JPanel opaque but loses it? If this is true does it lose it as we're using the paintComponent class or as we're extending the JPanel?

Any help is appreciated...

Cheers

Is This A Good Question/Topic? 1
  • +

Replies To: What does the super.paintComponent(g) do?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: What does the super.paintComponent(g) do?

Posted 11 January 2009 - 01:32 PM

View Postbastones, on 11 Jan, 2009 - 12:07 PM, said:

When I am extending the JPanel I have to super.paintComponent(g) - I know that it is required because a panel like JPanel is opaque but what is it really used for? I don't understand that the code itself paints the panel with the background colour - how does it lose it in the first place? Is there a method in JComponent that the JPanel inherits from JComponent class that makes the JPanel opaque but loses it? If this is true does it lose it as we're using the paintComponent class or as we're extending the JPanel?

Any help is appreciated...

Cheers


You do not always have to call super.paint(g)
you only have to do it when you draw something.

Let's have an example.

class MyPanel extends JPanel {
	MyPanel() {
	   add(new JLabel("Hello");
	   add(new JButton("OK");
	   ... addd other JComponent
	}
}


In that case you do not have to overload paint. Panel has a method call paint() that "knows" how to draw JLabel and JButton

Now if you draw something like Oval and Rectangle then you have to overload paint

class MyPanel extends JPanel {
	MyPanel() {
	   add(new JLabel("Hello");
	   add(new JButton("OK");
	   ... addd other JComponent
	}
	// I am overloading piant to do my own stuff
	public void paint(Graphics g) {
		 // now that I have overload paint... do I know how to draw the JLabel and JButton ?
		 // obviously not. So let the super method do the job
		 super.paint(g);
		 // now that the JLabel and JButton are paint les me do my own drawing/animation
		 g.drawOval(......
	}
}



So you have to overload and call super if you have "standard" components (that you don't know how to paint) and other

Now if your code is 100% your own drawing... you do not need to call super, just do your drawing

class MyPanel extends JPanel {
	MyPanel() {
	}
	// I am overloading paint to do my own stuff there are no "standard" GUI elements to draw
	public void paint(Graphics g) {
		 // now that the JLabel and JButton are paint les me do my own drawing/animation
		 g.drawOval(......
	}
}



Hope this help
Was This Post Helpful? 4

#3 bastones  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 34
  • Joined: 03-January 09

Re: What does the super.paintComponent(g) do?

Posted 11 January 2009 - 01:39 PM

Hi, thanks for your reply. So am I right in saying that when I use the paintComponent method it is overloading specific methods that make panels (like JPanel) opaque? I understand that super invokes the methods we've overloaded...

View Postpbl, on 11 Jan, 2009 - 12:32 PM, said:

View Postbastones, on 11 Jan, 2009 - 12:07 PM, said:

When I am extending the JPanel I have to super.paintComponent(g) - I know that it is required because a panel like JPanel is opaque but what is it really used for? I don't understand that the code itself paints the panel with the background colour - how does it lose it in the first place? Is there a method in JComponent that the JPanel inherits from JComponent class that makes the JPanel opaque but loses it? If this is true does it lose it as we're using the paintComponent class or as we're extending the JPanel?

Any help is appreciated...

Cheers


You do not always have to call super.paint(g)
you only have to do it when you draw something.

Let's have an example.

class MyPanel extends JPanel {
	MyPanel() {
	   add(new JLabel("Hello");
	   add(new JButton("OK");
	   ... addd other JComponent
	}
}


In that case you do not have to overload paint. Panel has a method call paint() that "knows" how to draw JLabel and JButton

Now if you draw something like Oval and Rectangle then you have to overload paint

class MyPanel extends JPanel {
	MyPanel() {
	   add(new JLabel("Hello");
	   add(new JButton("OK");
	   ... addd other JComponent
	}
	// I am overloading piant to do my own stuff
	public void paint(Graphics g) {
		 // now that I have overload paint... do I know how to draw the JLabel and JButton ?
		 // obviously not. So let the super method do the job
		 super.paint(g);
		 // now that the JLabel and JButton are paint les me do my own drawing/animation
		 g.drawOval(......
	}
}



So you have to overload and call super if you have "standard" components (that you don't know how to paint) and other

Now if your code is 100% your own drawing... you do not need to call super, just do your drawing

class MyPanel extends JPanel {
	MyPanel() {
	}
	// I am overloading paint to do my own stuff there are no "standard" GUI elements to draw
	public void paint(Graphics g) {
		 // now that the JLabel and JButton are paint les me do my own drawing/animation
		 g.drawOval(......
	}
}



Hope this help

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: What does the super.paintComponent(g) do?

Posted 11 January 2009 - 01:59 PM

View Postbastones, on 11 Jan, 2009 - 12:39 PM, said:

Hi, thanks for your reply. So am I right in saying that when I use the paintComponent method it is overloading specific methods that make panels (like JPanel) opaque? I understand that super invokes the methods we've overloaded...

Opaque has nothing to do here
I just draw the component that it kowns about
It know abot them because you panel.add(component) before
When component repaint() themselve they know if they have to be opaque or not

Actually in the example I gave you you can call super to paint the background

class MyPanel extends JPanel {
	MyPanel() {
	}
	// I am overloading paint to do my own stuff there are no "standard" GUI elements to draw
	public void paint(Graphics g) {
		 // call father to repaint the background
		 super.paint(g);
		 // now that the JLabel and JButton are paint les me do my own drawing/animation
		 g.drawOval(......
	}
}


This post has been edited by pbl: 11 January 2009 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1