Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help using the paintComponent method

 
Todd Swift
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm reading the Head First Java book and am having problems using the example regarding the paintComponent method (pg. 364 chapter 12). I've made a subclass of JPanel but cannot seem to call the paintComponent method. I've pasted my code below; any tips would be appreciated!

-----------------------------------------------------------
package mydrawpaneltest;

import java.awt.*;
import javax.swing.*;

public class MyJPanel extends JPanel {

/** Creates a new instance of MyJPanel */
public MyJPanel() {
}

public void paintComponent(Graphics g) {

g.setColor(Color.orange);
g.fillRect(20, 50, 100, 100);

}
}


----------------------------------------------------------

package mydrawpaneltest;

public class Main {

public Main() {
}

public static void main(String[] args) {

MyJPanel myJPan = new MyJPanel();
myJPan.repaint();
}
}
 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the paintComponent(Graphics g)

add.

super.paintComponent(g);


Justin
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if that is all of your code, the panel needs to be in a frame/contentPane
and for the frmae to be visible.

try this for your Main()

 
Todd Swift
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help; putting the panel in the frame worked! Thanks to all for your help.
 
chaitanya ashtekar
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:if that is all of your code, the panel needs to be in a frame/contentPane
and for the frmae to be visible.

try this for your Main()



what difference is there between

and

i have just started to read head first java.that is why i a asking to clarify my understanding.
 
Rob Camick
Ranch Hand
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't hijack old postings. This posting is about "painting". Your question has nothing to do with painting.

Read the section from the Swing tutorial on Using Top Level Containers which will help explain the structure of a JFrame and its child components. The Swing tutorial is probably a better reference than Head First Java because it is more current.

In the first case you are replacing the content pane with your panel.

In the second case you are adding your panel to the content pane.

Effectively they are the same because you can still add components to the frame however you want.

Look at the examples in the Swing tutorial to see which approach is used most often.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic