File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes need help using the paintComponent method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "need help using the paintComponent method" Watch "need help using the paintComponent method" New topic
Author

need help using the paintComponent method

Todd Swift
Greenhorn

Joined: Apr 11, 2006
Posts: 5
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

Joined: Jan 24, 2006
Posts: 802
in the paintComponent(Graphics g)

add.

super.paintComponent(g);


Justin


You down with OOP? Yeah you know me!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Apr 11, 2006
Posts: 5
Thanks for your help; putting the panel in the frame worked! Thanks to all for your help.
chaitanya ashtekar
Greenhorn

Joined: Dec 05, 2013
Posts: 6
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

Joined: Jun 13, 2009
Posts: 2280
    
    8
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help using the paintComponent method